1 people like it.
Like the snippet!
Monad Bind 2 of 3
After watching this clip (http://www.youtube.com/watch?v=ZhuHCtR3xq8) on Youtube featuring Brian Beckman I wanted to try to sketch Brian's main argument that Monads' main purpose is function composition.
I will post my sketch to http://rodhern.wordpress.com/2014/02/ .
These snippets are the companion examples to the blog post.
1:
2:
|
// Script example (2 of 3) for Monad blog post
// at http://rodhern.wordpress.com/2014/02/
|
1:
2:
3:
4:
5:
6:
|
type A = int
type S = int
type B = float
type T = DateTime
type C = int * int * int
type U = string
|
1:
2:
3:
|
let f (noWeeks: A): S = 7 * noWeeks
let g (noDays: B): T = today.AddDays noDays
let h ((yyyy, mm, dd): C): U = sprintf "%04d-%02d-%02d" yyyy mm dd
|
1:
2:
3:
4:
5:
|
let composeSB: S * (B -> 'T) -> 'T = fun (x, fn) -> fn (float x)
let composeTC: T * (C -> 'U) -> 'U = fun (x, fn) -> fn (x.Year, x.Month, x.Day)
let fghOne = fun x -> composeTC(composeSB(f x, g), h)
let fghTwo = fun x -> composeSB(f x, (fun y -> composeTC(g y, h)))
|
1:
2:
|
let fghOneResult = fghOne myInputParam
let fghTwoResult = fghTwo myInputParam
|
type A = int
Full name: Script.A
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
type S = int
Full name: Script.S
type B = float
Full name: Script.B
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
type T = DateTime
Full name: Script.T
Multiple items
type DateTime =
struct
new : ticks:int64 -> DateTime + 10 overloads
member Add : value:TimeSpan -> DateTime
member AddDays : value:float -> DateTime
member AddHours : value:float -> DateTime
member AddMilliseconds : value:float -> DateTime
member AddMinutes : value:float -> DateTime
member AddMonths : months:int -> DateTime
member AddSeconds : value:float -> DateTime
member AddTicks : value:int64 -> DateTime
member AddYears : value:int -> DateTime
...
end
Full name: System.DateTime
--------------------
DateTime()
(+0 other overloads)
DateTime(ticks: int64) : unit
(+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
(+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
(+0 other overloads)
type C = int * int * int
Full name: Script.C
type U = string
Full name: Script.U
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = String
Full name: Microsoft.FSharp.Core.string
val f : noWeeks:A -> S
Full name: Script.f
val noWeeks : A
val g : noDays:B -> T
Full name: Script.g
val noDays : B
val today : DateTime
Full name: Script.today
Today's date and time for use in examples below.
DateTime.AddDays(value: float) : DateTime
val h : int * int * int -> U
Full name: Script.h
val yyyy : int
val mm : int
val dd : int
val sprintf : format:Printf.StringFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.sprintf
val composeSB : x:S * fn:(B -> 'T) -> 'T
Full name: Script.composeSB
val x : S
val fn : (B -> 'T)
val composeTC : x:T * fn:(C -> 'U) -> 'U
Full name: Script.composeTC
val x : T
val fn : (C -> 'U)
property DateTime.Year: int
property DateTime.Month: int
property DateTime.Day: int
val fghOne : x:A -> U
Full name: Script.fghOne
val x : A
val fghTwo : x:A -> U
Full name: Script.fghTwo
val y : B
val fghOneResult : U
Full name: Script.fghOneResult
val myInputParam : int
Full name: Script.myInputParam
The test parameter I will use is "2" as in "two weeks from now".
val fghTwoResult : U
Full name: Script.fghTwoResult
More information