Thursday, March 14, 2013

Active Pattern (2) : Partial Active Pattern

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

If your "Single-case Active Pattern" should be considered for dealing option type (e.g. phosphorescence: Active Pattern (1) : Single-case Active Pattern: this sample is NOT considering invalid date), you should define as "Partial Active Pattern" like below:
> open System;;
> let (|WhatDayOfWeek|_|) (year, month, day) =
-     try
-         Some(System.DateTime(year,month,day).DayOfWeek)
-     with
-     | :? System.ArgumentOutOfRangeException -> None;;

val ( |WhatDayOfWeek|_| ) : year:int * month:int * day:int -> DayOfWeek option

> let weekEnd year month day =
-     match (year, month, day) with
-     | WhatDayOfWeek System.DayOfWeek.Sunday
-     | WhatDayOfWeek System.DayOfWeek.Saturday
-         -> "Week End !!"
-     | WhatDayOfWeek _
-         -> "Not Week End..."
-     | _ -> "invalid date";;

val weekEnd : year:int -> month:int -> day:int -> string

> weekEnd 2013 3 15;;
val it : string = "Not Week End..."
> weekEnd 2013 3 16;;
val it : string = "Week End !!"
> weekEnd 2013 2 29;;
val it : string = "invalid date"

Monday, March 11, 2013

Active Pattern (1) : Single-case Active Pattern

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

Single-case Active Pattern is defined as a special function enclosed (| |).
> open System;;
> let (|WhatDayOfWeek|) (year, month, day) =
-     System.DateTime(year,month,day).DayOfWeek;;

val ( |WhatDayOfWeek| ) : year:int * month:int * day:int -> DayOfWeek

> let isWeekEnd year month day =
-     match (year, month, day) with
-     | WhatDayOfWeek System.DayOfWeek.Sunday
-     | WhatDayOfWeek System.DayOfWeek.Saturday
-         -> true
-     | WhatDayOfWeek _
-         -> false;;

val isWeekEnd : year:int -> month:int -> day:int -> bool

> isWeekEnd 2013 3 15;;
val it : bool = false
> isWeekEnd 2013 3 16;;
val it : bool = true

Friday, March 8, 2013

pkgng on PC-BSD rolling release is not stable.

The package management tool of PC-BSD rolling release is pkg(8)(a.k.a. pkgng). But, IMHO, this is not stable yet.
  • XScreensaver for pkgng is 5.12, but default version of PC-BSD rolling release is 5.20. When executing pkg upgrade, Xscreensaver and its releated GUI libraries are degraded.
  • As written in this manual, when accepting to use pkgng, it is NOT reversible to any existing package management tools.
So I decide not to use pkgng for a while, until FreeBSD/PC-BSD 10.0 will be released.

(/etc/make.conf)
WITHOUT_PKGNG=yes

Tuesday, March 5, 2013

Saturday, March 2, 2013

Dispose pattern in F#

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

If we want to access to unmanaged resources from C#,
  1. The class that indicates unmanaged resource should implement IDisposable interface and override Dispose method.
  2. When using this class, we should write the code with using () {} clause.

In F#, almost same.
  1. The class that indicates unmanaged resource should implement IDisposable interface and override Dispose method.
  2. When using this class, we should write the code with use binding.
> open System;;
> type SomeUnmanagedResource() =
-     interface IDisposable with
-         member this.Dispose() =
-             printfn "Some unmanaged resource is diposed.";;

type SomeUnmanagedResource =
  class
    interface IDisposable
    new : unit -> SomeUnmanagedResource
  end

> let greetings =
-     use res = new SomeUnmanagedResource()
-     printfn "Hello World";;
Hello World
Some unmanaged resource is diposed.

val greetings : unit = ()