Friday, April 26, 2013
Spring short vacation 2013
In Japan, last week of April and first week of May are short vacation weeks. Of course, I take a short vacation, so I suspend posts and comments to this blog for a moment. Resume will be May 7, 2013.
Tuesday, April 23, 2013
Extend module
(In learning from "Programming F# 3.0, 2nd Edition")
Extending existing module is just same syntax to creating new module. For example, add slice function like phosphorescence: The way to add both indexer and slice to F# sequence with extending both Seq and List modules.
Extending existing module is just same syntax to creating new module. For example, add slice function like phosphorescence: The way to add both indexer and slice to F# sequence with extending both Seq and List modules.
> module Seq =
- let slice (lower : int option, upper : int option) aSeq =
- match lower, upper with
- | Some(lower), Some(upper) -> aSeq |> Seq.skip lower |> Seq.take (upper - lower + 1)
- | Some(lower), None -> aSeq |> Seq.skip lower
- | None, Some(upper) -> aSeq |> Seq.take (upper + 1)
- | None, None -> aSeq;;
module Seq = begin
val slice : lower:int option * upper:int option -> aSeq:seq<'a> -> seq<'a>
end
> let seq1To5 = seq {1..5};;
val seq1To5 : seq<int>
> Seq.slice (Some(1), Some(3)) seq1To5;;
val it : seq<int> = seq [2; 3; 4]
> module List =
- let slice (lower : int option, upper : int option) aList =
- match lower, upper with
- | Some(lower), Some(upper) -> aList |> Seq.skip lower |> Seq.take (upper - lower + 1) |> Seq.toList
- | Some(lower), None -> aList |> Seq.skip lower |> Seq.toList
- | None, Some(upper) -> aList |> Seq.take (upper + 1) |> Seq.toList
- | None, None -> aList;;
module List = begin
val slice : lower:int option * upper:int option -> aList:'a list -> 'a list
end
> let list1To5 = [1..5];;
val list1To5 : int list = [1; 2; 3; 4; 5]
> List.slice (Some(1), Some(3)) list1To5;;
val it : int list = [2; 3; 4]
Saturday, April 20, 2013
ConcurrentDictionary on F#
(In learning from "Programming F# 3.0, 2nd Edition")
While Dictionary has syntax sugar for F# (check also phosphorescence: Studying F# : Dictionary (a.k.a. Hash or Map)), System.Collections.Concurrent.ConcurrentDictionary has not. So that we use it manually.
While Dictionary has syntax sugar for F# (check also phosphorescence: Studying F# : Dictionary (a.k.a. Hash or Map)), System.Collections.Concurrent.ConcurrentDictionary has not. So that we use it manually.
> open System.Collections.Concurrent;; > let concurrentDict = new ConcurrentDictionary<int, string>();; val concurrentDict : ConcurrentDictionary<int,string> = dict [] > concurrentDict.TryAdd(1, "one");; val it : bool = true > concurrentDict.TryAdd(2, "two");; val it : bool = true > concurrentDict.TryAdd(3, "three");; val it : bool = true > concurrentDict.TryAdd(4, "four");; val it : bool = true
Thursday, April 18, 2013
Twin prime on Enumerator::Lazy
Labels:
Ruby
About twin prime : http://en.wikipedia.org/wiki/Twin_prime
irb(main):001:0> class Enumerator::Lazy
irb(main):002:1> def at(nth)
irb(main):003:2> self.drop(nth).first
irb(main):004:2> end
irb(main):005:1> def slice(lower, upper)
irb(main):006:2> self.drop(lower).take(upper - lower + 1)
irb(main):007:2> end
irb(main):008:1> end
=> nil
irb(main):009:0> require 'prime'
=> true
irb(main):010:0> twin_prime = Prime.lazy.each_cons(2).select{|p,r| r-p == 2}
=> #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: Prime>:each_cons(2)>:select>
irb(main):011:0> twin_prime.at 0
=> [3, 5]
irb(main):012:0> twin_prime.at 1
=> [5, 7]
irb(main):013:0> twin_prime.at 2
=> [11, 13]
irb(main):014:0> twin_prime.at 3
=> [17, 19]
irb(main):015:0> twin_prime.at 100
=> [3851, 3853]
Monday, April 15, 2013
Getting nth and slice for Enumerator::Lazy
Labels:
Ruby
Ruby 2.0's Enumerator::Lazy does not have a method for getting nth or getting slice, because Enumerator::Lazy has no indexers whileEnumerable has ones. But, we can same thing defining method on Enumerator::Lazy class.
class Enumerator::Lazy
def at(nth)
self.drop(nth).first
end
def slice(lower, upper)
self.drop(lower).take(upper - lower + 1)
end
end
irb(main):009:0> oneToFive = [1,2,3,4,5].lazy => #<Enumerator::Lazy: [1, 2, 3, 4, 5]> irb(main):010:0> oneToFive.at 1 => 2 irb(main):011:0> oneToFive.slice 1, 3 => #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: [1, 2, 3, 4, 5]>:drop(1)>:take(3)> irb(main):012:0> (oneToFive.slice 1, 3).to_a => [2, 3, 4]
Subscribe to:
Posts (Atom)