2 people like it.

MonadPlus operation for option type.

Taken from FSharpx.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
let inline flip f a b = f b a
let inline orElse v =
    function
    | Some x -> Some x
    | None -> v
    
let mplus x = flip orElse x

// Some 1
let a = mplus (Some 1) (Some 3)
// Some 1
let b = mplus (Some 1) (None)
// Some 2
let c = mplus (None) (Some 2)
// None
let d : int option = mplus (None) (None)
val flip : f:('a -> 'b -> 'c) -> a:'b -> b:'a -> 'c

Full name: Script.flip
val f : ('a -> 'b -> 'c)
val a : 'b
val b : 'a
val orElse : v:'a option -> _arg1:'a option -> 'a option

Full name: Script.orElse
val v : 'a option
union case Option.Some: Value: 'T -> Option<'T>
val x : 'a
union case Option.None: Option<'T>
val mplus : x:'a option -> ('a option -> 'a option)

Full name: Script.mplus
val x : 'a option
val a : int option

Full name: Script.a
val b : int option

Full name: Script.b
val c : int option

Full name: Script.c
val d : int option

Full name: Script.d
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 'T option = Option<'T>

Full name: Microsoft.FSharp.Core.option<_>
Raw view Test code New version

More information

Link:http://fssnip.net/pw
Posted:9 years ago
Author:ane
Tags: option monadplus