Skip to content

Instantly share code, notes, and snippets.

@ascjones
Created September 17, 2015 12:12
Show Gist options
  • Save ascjones/2b7701fd73df87209ecc to your computer and use it in GitHub Desktop.
Save ascjones/2b7701fd73df87209ecc to your computer and use it in GitHub Desktop.
list of options to option list
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