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.

> 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.
> 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

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

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]

Friday, April 12, 2013

Where will QtWebkit go?

In this week, Google announced they fork browser rendering engine from Webkit to "Blink", and Opera announced they join Google's "Blink". BTW, Which will QtWebkit go to? QtWebkit or QtBlink?

Tuesday, April 9, 2013

The way to add both indexer and slice to F# sequence

(continued from phosphorescence: Sequence has neither indexer nor slice)

The way to add both indexer and slice to F# sequence is: Expand System.Collections.Generic.IEnumerable on F#.
type System.Collections.Generic.IEnumerable<'a> with
    member this.Item(idx : int) =
        this |> Seq.nth idx
    member this.GetSlice(lower : int option, upper : int option) =
        match lower, upper with
        | Some(lower), Some(upper) -> this |> Seq.skip lower |> Seq.take (upper - lower + 1)
        | Some(lower), None -> this |> Seq.skip lower
        | None, Some(upper) -> this |> Seq.take (upper + 1)
        | None, None -> this

Saturday, April 6, 2013

Sequence has neither indexer nor slice

F# sequence is a collection, but not a list. So sequence has neither indexer nor slice (F# list has indexer, but does not have slice while C# List has both).
module UnfoldVerbosely =
    let fibSeed (current, next) =
        let yieldValue = current
        let next'current = next
        let next'next = current + next
        Some(yieldValue, (next'current, next'next))
    let fib = Seq.unfold fibSeed (1L, 1L)
> UnfoldVerbosely.fib.[0];;

  UnfoldVerbosely.fib.[0];;
  ^^^^^^^^^^^^^^^^^^^^^^^

stdin(8,1): error FS0039: The field, constructor or member 'Item' is not defined
> UnfoldVerbosely.fib.[0..2];;

  UnfoldVerbosely.fib.[0..2];;
  ^^^^^^^^^^^^^^^^^^^^^^^^^^

stdin(9,1): error FS0039: The field, constructor or member 'GetSlice' is not defined
There are alternative ways to do as indexer or slice.
> // Instead of indexer
- UnfoldVerbosely.fib |> Seq.nth 0;;
val it : int64 = 1L
> // Instead of slice
- UnfoldVerbosely.fib |> Seq.skip 0 |> Seq.take (2-0+1);;
val it : seq<int64> = seq [1L; 1L; 2L]

Wednesday, April 3, 2013

openSUSE 12.3

Two weeks ago, openSUSE 12.3 had been released.
  • Ruby's version is 1.9.3
  • JDK's version is OpenJDK 1.7
  • And, MariaDB 5.5 is adopted instead of MySQL

Monday, April 1, 2013

Sailfish OS is renamed its brand to "Selfish OS"

Sailfish OS – a Maemo based mobile OS - is renamed its brand to "Selfish OS". Devices with a brand-new "Selfish OS" take many advantages like below:
  • Speedy power-on and Suddnly power-off, automatically and randomly, for battery-life themselves.
  • Mulit-No-touch display runs, automatically and randomly, for devices themselves.
  • Calling and texting to friends, automatically and randomly, for carrier-billing itself.
  • NOT aggregating and updating geo-locations, photos, documents and any usage histories to the cloud, and NOT accessing these data for your security itself.