3 people like it.

Simple builder example: Nullable

Simple Computational expressions / monad / builder -example, using .NET Nullable as demo.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
module BuilderExample
open System

//Example: nullable { ... } with combining functionality

type NullableBuilder() =
    let hasValue (a:Nullable<'a>) = a.HasValue 
    member t.Return(x) = Nullable(x)
    member t.Bind(x, rest) = 
        match hasValue x with 
        | false -> System.Nullable() //.NET null object
        | true -> rest(x.Value)

let nullable = NullableBuilder()

let test =
    nullable{
        let! a = System.Nullable(3) //"let!" calls builders Bind
        let! b = System.Nullable(5) //"let!" calls builders Bind

        //Inside computation expression(/monad): easy programming without Nullable-worries:
        let mult = a * b 
        let sum = mult + 1 

        return sum //"return" calls builders Return(x)
    }
//val test : Nullable<int> = 16


//------------------------------------------------------------------------------
// using e.g.
//     let! b = System.Nullable() 
// would cause to return:
//     val test : System.Nullable()

// own { ...} syntax is made with implementing Builder-class
// and (some of) the "interface" members

//------------------------------------------------
// Further reading:
// "Interface" described in: http://msdn.microsoft.com/en-us/library/dd233182.aspx
// More info: http://blogs.msdn.com/b/dsyme/archive/2007/09/22/some-details-on-f-computation-expressions-aka-monadic-or-workflow-syntax.aspx

// Check also Reactive Extensions 2.0 with F# observe { ... }:
// https://github.com/panesofglass/FSharp.Reactive/blob/master/src/Observable.fs

// and: http://fssnip.net/tags/computation+builder and http://fssnip.net/tags/monad
module BuilderExample
namespace System
Multiple items
type NullableBuilder =
  new : unit -> NullableBuilder
  member Bind : x:Nullable<'a> * rest:('a -> Nullable<'b>) -> Nullable<'b> (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
  member Return : x:'c -> Nullable<'c> (requires default constructor and value type and 'c :> ValueType)

Full name: BuilderExample.NullableBuilder

--------------------
new : unit -> NullableBuilder
val hasValue : (Nullable<'a> -> bool) (requires default constructor and value type and 'a :> ValueType)
val a : Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
Multiple items
type Nullable =
  static member Compare<'T> : n1:Nullable<'T> * n2:Nullable<'T> -> int
  static member Equals<'T> : n1:Nullable<'T> * n2:Nullable<'T> -> bool
  static member GetUnderlyingType : nullableType:Type -> Type

Full name: System.Nullable

--------------------
type Nullable<'T (requires default constructor and value type and 'T :> ValueType)> =
  struct
    new : value:'T -> Nullable<'T>
    member Equals : other:obj -> bool
    member GetHashCode : unit -> int
    member GetValueOrDefault : unit -> 'T + 1 overload
    member HasValue : bool
    member ToString : unit -> string
    member Value : 'T
  end

Full name: System.Nullable<_>

--------------------
Nullable()
Nullable(value: 'T) : unit
property Nullable.HasValue: bool
val t : NullableBuilder
member NullableBuilder.Return : x:'c -> Nullable<'c> (requires default constructor and value type and 'c :> ValueType)

Full name: BuilderExample.NullableBuilder.Return
val x : 'c (requires default constructor and value type and 'c :> ValueType)
member NullableBuilder.Bind : x:Nullable<'a> * rest:('a -> Nullable<'b>) -> Nullable<'b> (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)

Full name: BuilderExample.NullableBuilder.Bind
val x : Nullable<'a> (requires default constructor and value type and 'a :> ValueType)
val rest : ('a -> Nullable<'b>) (requires default constructor and value type and 'a :> ValueType and default constructor and value type and 'b :> ValueType)
property Nullable.Value: 'a
val nullable : NullableBuilder

Full name: BuilderExample.nullable
val test : Nullable<int>

Full name: BuilderExample.test
val a : int
val b : int
val mult : int
val sum : int

More information

Link:http://fssnip.net/bE
Posted:12 years ago
Author:Tuomas Hietanen
Tags: builder , monad , computation builder , nullable