8 people like it.

ResultBuilder Computational Expression

Computational Expression for Result<'a, 'b>

 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: 
open System

type ResultBuilder() =
    member __.Return(x) = Ok x

    member __.ReturnFrom(m: 'T option) = m

    member __.Bind(m, f) = Result.bind f m

    member __.Zero() = None

    member __.Combine(m, f) = Option.bind f m

    member __.Delay(f: unit -> _) = f

    member __.Run(f) = f()

    member __.TryWith(m, h) =
        try __.ReturnFrom(m)
        with e -> h e

    member __.TryFinally(m, compensation) =
        try __.ReturnFrom(m)
        finally compensation()

    member __.Using(res:#IDisposable, body) =
        __.TryFinally(body res, fun () -> match res with null -> () | disp -> disp.Dispose())

    member __.While(guard, f) =
        if not (guard()) then Some () else
        do f() |> ignore
        __.While(guard, f)

    member __.For(sequence:seq<_>, body) =
        __.Using(sequence.GetEnumerator(), fun enum -> __.While(enum.MoveNext, __.Delay(fun () -> body enum.Current)))

let result = new ResultBuilder()

type MyErr = Err1 | Err2

let aa = result {
  let! a = Ok "aaa"
  let! b = Error Err1 
  let c = if true then a else b
  return c
}
namespace System
Multiple items
type ResultBuilder =
  new : unit -> ResultBuilder
  member Bind : m:'m * f:'n -> 'o
  member Combine : m:'j option * f:('j -> 'k option) -> 'k option
  member Delay : f:(unit -> 'i) -> (unit -> 'i)
  member For : sequence:seq<'a> * body:('a -> 'b) -> unit option
  member Return : x:'p -> 'q
  member ReturnFrom : m:'T option -> 'T option
  member Run : f:(unit -> 'h) -> 'h
  member TryFinally : m:'f option * compensation:(unit -> unit) -> 'f option
  member TryWith : m:'g option * h:(exn -> 'g option) -> 'g option
  ...

Full name: Script.ResultBuilder

--------------------
new : unit -> ResultBuilder
member ResultBuilder.Return : x:'p -> 'q

Full name: Script.ResultBuilder.Return
val x : 'p
val __ : ResultBuilder
member ResultBuilder.ReturnFrom : m:'T option -> 'T option

Full name: Script.ResultBuilder.ReturnFrom
val m : 'T option
type 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
member ResultBuilder.Bind : m:'m * f:'n -> 'o

Full name: Script.ResultBuilder.Bind
val m : 'm
val f : 'n
member ResultBuilder.Zero : unit -> 'l option

Full name: Script.ResultBuilder.Zero
union case Option.None: Option<'T>
member ResultBuilder.Combine : m:'j option * f:('j -> 'k option) -> 'k option

Full name: Script.ResultBuilder.Combine
val m : 'j option
val f : ('j -> 'k option)
module Option

from Microsoft.FSharp.Core
val bind : binder:('T -> 'U option) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.bind
member ResultBuilder.Delay : f:(unit -> 'i) -> (unit -> 'i)

Full name: Script.ResultBuilder.Delay
val f : (unit -> 'i)
type unit = Unit

Full name: Microsoft.FSharp.Core.unit
member ResultBuilder.Run : f:(unit -> 'h) -> 'h

Full name: Script.ResultBuilder.Run
val f : (unit -> 'h)
member ResultBuilder.TryWith : m:'g option * h:(exn -> 'g option) -> 'g option

Full name: Script.ResultBuilder.TryWith
val m : 'g option
val h : (exn -> 'g option)
member ResultBuilder.ReturnFrom : m:'T option -> 'T option
val e : exn
member ResultBuilder.TryFinally : m:'f option * compensation:(unit -> unit) -> 'f option

Full name: Script.ResultBuilder.TryFinally
val m : 'f option
val compensation : (unit -> unit)
member ResultBuilder.Using : res:'d * body:('d -> 'e option) -> 'e option (requires 'd :> IDisposable and 'd : null)

Full name: Script.ResultBuilder.Using
val res : 'd (requires 'd :> IDisposable and 'd : null)
type IDisposable =
  member Dispose : unit -> unit

Full name: System.IDisposable
val body : ('d -> 'e option) (requires 'd :> IDisposable and 'd : null)
member ResultBuilder.TryFinally : m:'f option * compensation:(unit -> unit) -> 'f option
val disp : 'd (requires 'd :> IDisposable and 'd : null)
IDisposable.Dispose() : unit
member ResultBuilder.While : guard:(unit -> bool) * f:(unit -> 'c) -> unit option

Full name: Script.ResultBuilder.While
val guard : (unit -> bool)
val f : (unit -> 'c)
val not : value:bool -> bool

Full name: Microsoft.FSharp.Core.Operators.not
union case Option.Some: Value: 'T -> Option<'T>
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
member ResultBuilder.While : guard:(unit -> bool) * f:(unit -> 'c) -> unit option
member ResultBuilder.For : sequence:seq<'a> * body:('a -> 'b) -> unit option

Full name: Script.ResultBuilder.For
val sequence : seq<'a>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val body : ('a -> 'b)
member ResultBuilder.Using : res:'d * body:('d -> 'e option) -> 'e option (requires 'd :> IDisposable and 'd : null)
Collections.Generic.IEnumerable.GetEnumerator() : Collections.Generic.IEnumerator<'a>
val enum : Collections.Generic.IEnumerator<'a>
Collections.IEnumerator.MoveNext() : bool
member ResultBuilder.Delay : f:(unit -> 'i) -> (unit -> 'i)
property Collections.Generic.IEnumerator.Current: 'a
val result : ResultBuilder

Full name: Script.result
type MyErr =
  | Err1
  | Err2

Full name: Script.MyErr
union case MyErr.Err1: MyErr
union case MyErr.Err2: MyErr
val aa : obj

Full name: Script.aa
val a : obj
val b : obj
val c : obj
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7UJ
Posted:6 years ago
Author:Yuriy Habarov
Tags: computation expression , #result , #resultbuilder