Thursday, May 30, 2013

The way to install sqlite3 gem on Ruby 2.0 on Windows

Until Ruby 1.9.3, the way to install sqlite3 gem on Windows is easy - just do gem install. But, since Ruby 2.0, it becomes a difficult way. The solution is written in this article, and this is the only one article at this moment.

Monday, May 27, 2013

F# Sequence for Collatz Problem

F# Sequence for Collatz Problem is:
let collatzSeq n =
  let collatzFunction n =
    match n % 2L with
    | 0L -> n / 2L
    | _ -> 3L * n + 1L
  let rec collatzCalc n accum =
    let accum'next = Seq.append accum <| Seq.singleton n
    if n = 1L then
      accum'next
    else
      collatzCalc <| collatzFunction n <| accum'next
  match n > 0L with
  | true -> collatzCalc n Seq.empty<int64>
  | _ -> failwith "The argument should be a natural number."
> collatzSeq 1L;;
val it : seq<int64> = seq [1L]
> collatzSeq 2L;;
val it : seq<int64> = seq [2L; 1L]
> collatzSeq 3L |> Seq.toList;;
val it : int64 list = [3L; 10L; 5L; 16L; 8L; 4L; 2L; 1L]
> collatzSeq 4L |> Seq.toList;;
val it : int64 list = [4L; 2L; 1L]
> collatzSeq 5L |> Seq.toList;;
val it : int64 list = [5L; 16L; 8L; 4L; 2L; 1L]
> collatzSeq 6L |> Seq.toList;;
val it : int64 list = [6L; 3L; 10L; 5L; 16L; 8L; 4L; 2L; 1L]
> collatzSeq 7L |> Seq.toList;;
val it : int64 list =
  [7L; 22L; 11L; 34L; 17L; 52L; 26L; 13L; 40L; 20L; 10L; 5L; 16L; 8L; 4L; 2L; 1L]

Friday, May 24, 2013

FreeBSD Study #18

(Write it later)

Tuesday, May 21, 2013

Harmonic number on F#

Harmonic number in F#:
let harmonicNumber n =
  let harmonicSeed (current, next) =
    Some(current, (next, 1.0m / (1.0m + (1.0m / next))))
  match n > 0 with
  | true -> Seq.reduce (+) (Seq.take n <| Seq.unfold harmonicSeed (1.0m, 1.0m / 2.0m))
  | _ -> failwith "The argument should be a natural number."
> harmonicNumber 1;;
val it : decimal = 1.0M
> harmonicNumber 2;;
val it : decimal = 1.5M
> harmonicNumber 10;;
val it : decimal = 2.9289682539682539682539682540M
> harmonicNumber 100;;
val it : decimal = 5.1873775176396202608051176738M
> harmonicNumber 1000;;
val it : decimal = 7.4854708605503449126565180835M
> harmonicNumber 10000;;
val it : decimal = 9.787606036044382264178479457M
> harmonicNumber 100000;;
val it : decimal = 12.090146129863427947363117950M
> harmonicNumber 1000000;;
val it : decimal = 14.392726722865723631388218824M

Saturday, May 18, 2013

Ruby 1.9.3-p429, Ruby 2.0.0-p195, JRuby 1.7.4 had been released

In this week, Ruby 1.9.3-p429, Ruby 2.0.0-p195 and JRuby 1.7.4 had been released. Ruby 2.0.0-p195 is the first release that is stable 2.0.x release on Windows (from here), and JRuby 1.7.4 us the first release that bundles "2.0 mode".
>%JRUBY_HOME%\bin\jruby --2.0 --version
jruby 1.7.4 (2.0.0) 2013-05-16 2390d3b on Java HotSpot(TM) 64-Bit Server VM 1.7.0_21-b11 [Windows 7-amd64]

Thursday, May 16, 2013

Java Day Tokyo 2013


大きな地図で見る
Java Day Tokyo 2013 was held in Akihabara UDX.
  • Keynote
    • Java SE strategy
      • The most improvement : lambda expression
        • Collection becomes more useful
        • Advantage for parallel execution of JVM
      • Other improvements
        • Virtual Extension Methods : Extending existing Interface
        • Project Nashron : Brand-new JavaScript engine
      • Java9 development is going on
      • Java8 : 2014/02 (Delayed from 2013 4Q)
      • Java9 : 2 year after from releasing Java8
    • JavaFX Strategy
      • Plan : Deliverying JavaFX application with JavaFX runtime on Mac AppStore
      • JavaFX is completely OSS-ed : integrated into the openJDK
      • Scene Builder : JavaFX GUI tool
        • Now supports Win and Mac
        • And, supports Linux in 2013 4Q
      • Demo : JavaFX 3D on Microsoft Surface
    • Java Embedded Strategy
      • The most advantege of using Java Embedded : Standarlized
      • Recently, JavaME Embedded 3.3 has been released
    • Java EE Strategy
      • JavaEE 6 has 18 implementations (including Glassfish)
      • JavaEE 7
        • Release plan : 2013/06/13
        • feature closed
        • Batch process
        • JSON
        • WebSocket
        • concurrency
      • Demo : WebSocket
    • Java Community
      • How to join Java community
      • What about Java Community Process
      • Introduction of JJUG
  • WebSocket
    • HTTP is half-duplex
    • HTTP is verbose
    • Last technologies for server push (e.g. polling, comet) are complex and inefficient.
    • WebSocket
      • bi-directional full-duplex messaging
      • IETF-defined RFC6455
      • W3C-defined JavaScript API
      • No headers, cookies authentication
    • Procedure
      1. handshake as HTTP
      2. Upgrade from HTTP to WebSocket
      3. handshake as WebSocket
    • Protocol is ws://
    • HTTP is client-server / WebSocket is peer-peer
    • To survey supporting Browser : caniuse.com/websockets
    • IE starts supporting WebSocket since IE10
    • JSR356
    • WebSocket and Glassfish 4
      • details of JSR356 annotations
      • details of TextDecoder / TextEncoder
      • details of Dependency Injection
    • Servlet API 3.1 adds new API HttpServletRequest.upgrade
    • How about secutiry
      • Do secure-releated procedures and authentications on HTTP, before handshaking WebSocket
      • WebSocket with SSL (wss://)
  • Lambda Expression
    • Multicore processor is current evolution of CPU
    • JavaSE should adapt multicore and parallel
    • external iteration is serial, and hard to be parallel
    • internal iteration is NOT serial, and easy to be parallel
    • a good way of internal iteration : iambda expression
    • Lambda in JavaSE 8
      • Not only internal iteration, target typing
      • It is Not closure
      • this indicates enclosing class, does not indicate inner lambda
      • Parameters of lmabda can do type inference
      • New accessor notation :: : (single method call)
    • When updating collections for lambda in JavaSE class library
      • Collection is defined as Interface
      • changing interface means changing all of concrete classes
      • But, JavaSE class library is required backward compatibility
      • So that JavaSE 8 also adapts also Virtual Extension Method
    • Virtual Extension Method
      • A new syntax to extend existing interface
      • Defining both new API and default behavior for it
    • JavaSE 8's lambda is optimized by using invokedynamic when compiled : so that javac uses invokedynamic since JavaSE 8
  • Batch Applications
    • JSR352 : IBM leads standarlization for Java Batch Processes
    • A part of JavaEE 7
    • But it can us stand-alone on Java SE : java.net/projects/jbatch
    • Details of JSR352 architecture
      • Job, Step
      • ItemReader, ItemProcessor, ItemWriter
      • Partitioning : Mapper, Reducer, Collector, Analyzer
    • Job Supecification Language(JSL) : XML for Java Batch DSL
      • How to handle/not-handle exceptions : skippable-exception, retryable-exception, no-rollback-exception
      • Flow : a serial set of Steps
      • Split : a parallel set of Flows
    • Demo
  • invokedynamic and Project Nashorn
    • invokedynamic : one of bytecode instruction since Java SE 7
    • In Java SE 7 : used for dynamic languages running on JVM
    • In Java SE 8 : used also for optimizing lambda expression when compiled
    • Details of invokedynamic
    • dynamic languages on JVM : JRuby, Jython, Groovy and so forth
    • difficulties of creating dynamic language
      • Type is not determined at some time
      • members of class and object are changable
      • Ablilty to re-define existing members
    • Project Nashorn
      • A JavaScript engine for JVM using invokedynamic
      • JavaSE6, JavaSE7 bundles Rhino
        • invokedynamic is not used
        • It's slow
      • It also roles as reference implementation for using invokedynamic
      • Target : running node.js
      • ECMAScript based
      • Faster
      • Size is well small for using in Java Embedded
And, in this day, Oralce Japan has announced that they release Java Documentation Japanese Edition.
Java Platform Standard Edition 7 Document (Japanese)
Java Platform Standard Edition 7 API Document (Japanese)

Monday, May 13, 2013

Get prime numbers on F# (infinite)

(continued from phosphorescence: Get prime numbers on F# (finite))
Rewrite to get prime numbers on F# using Sieve of Eratosthenes algorithm with infinite sequence.
module Prime =
  let primeTable = ref (Seq.empty<int64>)
  let takeSmallerThanSquare n sequence =
    Seq.takeWhile (fun elem -> elem * elem <= n) sequence
  let existsMultiple n sequence =
    Seq.exists (fun elem -> n % elem = 0L) sequence
  let isPrimeElement elem =
    match (existsMultiple elem <| takeSmallerThanSquare elem !primeTable) with
    | true -> None
    | false -> primeTable := Seq.append !primeTable (Seq.singleton elem);Some(elem)
  let seedSeq = Seq.append
                <| Seq.ofList [2L; 3L; 5L; 7L]
                <| Seq.unfold
                     (fun (current, next) ->
                       match next % 10L with
                       | 3L -> Some(current, (next, next + 4L))
                       | _ -> Some(current, (next, next + 2L))
                     ) (11L, 13L)
  let sieveOfEratosthenes = Seq.choose isPrimeElement seedSeq


And I put this in github.

It takes little faster than finite one.
> Seq.last <| Seq.take 1000 Prime.sieveOfEratosthenes;;
val it : int64 = 7919L

Friday, May 10, 2013

Get prime numbers on F# (finite)

Get prime numbers on F# using Sieve of Eratosthenes algorithm.
module Prime =
  let sieveOfEratosthenes = seq {
    let primeTable = ref (Seq.ofList [2L ;3L ;5L ;7L])
    let sieve'sSeed = [11L; 13L; 17L; 19L]
    let takeSmallerThanSquare n sequence =
      Seq.takeWhile (fun elem -> elem * elem <= n) sequence
    let existsMultiple n sequence =
      Seq.exists (fun elem -> n % elem = 0L) sequence
    let sieve digit =
      let isPrimeElement seed =
        let elem = digit * 10L + seed
        match (existsMultiple elem <| takeSmallerThanSquare elem !primeTable) with
        | true -> None
        | false -> Some(elem)
      primeTable := Seq.append !primeTable (Seq.choose isPrimeElement sieve'sSeed)
    List.iter (fun i -> sieve i) [0L..500L]
    yield! !primeTable
  };;

It's finite, and it takes long time (so I stop the number around 5000).
> Seq.last Prime.sieveOfEratosthenes;;
val it : int64 = 5011L
(continue to phosphorescence: Get prime numbers on F# (infinite))

Tuesday, May 7, 2013

Difference between typeof and typedefof

(In learning from "Programming F# 3.0, 2nd Edition")

There are two functions to know type information - typeof and typedefof. typeof shows information both class-itself and its type parameter while typedefof shows only class-itself and shows generics as-is.
> let typeOfSeqInt = typeof<seq<int>>;;

val typeOfSeqInt : System.Type =
  System.Collections.Generic.IEnumerable`1[System.Int32]

> let typeOfSeqGeneric = typeof<seq<'a>>;;

  let typeOfSeqGeneric = typeof<seq<'a>>;;
  ----------------------------------^^

stdin(7,35): warning FS0064: This construct causes code to be less generic than
indicated by the type annotations. The type variable 'a has been constrained to
be type 'obj'.

val typeOfSeqGeneric : System.Type =
  System.Collections.Generic.IEnumerable`1[System.Object]

> let typeDefOfSeqInt = typedefof<seq<int>>;;

val typeDefOfSeqInt : System.Type =
  System.Collections.Generic.IEnumerable`1[T]

> let typeDefOfSeqGeneric = typedefof<seq<'a>>;;

  let typeDefOfSeqGeneric = typedefof<seq<'a>>;;
  ----------------------------------------^^

stdin(9,41): warning FS0064: This construct causes code to be less generic than
indicated by the type annotations. The type variable 'a has been constrained to
be type 'obj'.

val typeDefOfSeqGeneric : System.Type =
  System.Collections.Generic.IEnumerable`1[T]