2 people like it.

Option.map2

Simplification of 'Apply operators to Options' http://fssnip.net/ee using higher order functions. Should also be part of the Option module.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
module Option = 
  let map2 f a b =
     a |> Option.bind (fun a' -> b |> Option.map (fun b' -> f a' b'))

//test
Option.map2 (+) (Some "a") None
Option.map2 (+) (Some "a") (Some "b")

Option.map2 (-) (Some 2) (Some 1)
module Option

from Microsoft.FSharp.Core
val map2 : f:('a -> 'b -> 'c) -> a:'a option -> b:'b option -> 'c option

Full name: Script.Option.map2
val f : ('a -> 'b -> 'c)
val a : 'a option
val b : 'b option
val bind : binder:('T -> 'U option) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.bind
val a' : 'a
val map : mapping:('T -> 'U) -> option:'T option -> 'U option

Full name: Microsoft.FSharp.Core.Option.map
val b' : 'b
Multiple items
module Option

from Script

--------------------
module Option

from Microsoft.FSharp.Core
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Raw view New version

More information

Link:http://fssnip.net/ef
Posted:6 years ago
Author:@rojepp
Tags: operators options