Created
September 17, 2015 12:12
-
-
Save ascjones/2b7701fd73df87209ecc to your computer and use it in GitHub Desktop.
list of options to option list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let optionList l = | |
let rec bindList xs acc = | |
match xs with | |
| [] -> Some acc | |
| h::t -> h |> Option.bind (fun x -> bindList t (x::acc)) | |
bindList l [] | |
module Option = | |
let map2 f a b = | |
a |> Option.bind (fun a' -> b |> Option.map (fun b' -> f a' b')) | |
let optionList2 = List.fold (Option.map2 (fun acc x -> x::acc)) (Some []) >> Option.map List.rev | |
let res1 : int list option = [Some 1; Some 2] |> optionList2 | |
let res2 : int list option = [Some 1; None] |> optionList2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment