Last active
August 29, 2015 14:17
-
-
Save Porges/489230e3760404129359 to your computer and use it in GitHub Desktop.
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
| // direct-ish translation | |
| let allOrNone (xs : 'a option list) : 'a list option = | |
| List.foldBack | |
| (fun x st -> x |> Option.bind (fun x -> st |> Option.bind (fun st -> Some (x::st)))) | |
| xs | |
| (Some []) | |
| // shorter | |
| let allOrNone2 (xs : 'a option list) : 'a list option = | |
| List.foldBack | |
| (fun x -> Option.bind (fun st -> x |> Option.map (fun x -> x::st))) | |
| xs | |
| (Some []) | |
| // bails out early | |
| let allOrNone3 xs = | |
| let rec recAllOrNone cont = function | |
| | (Some x :: xs) -> recAllOrNone (fun r -> cont (x :: r)) xs | |
| | (None :: _) -> None | |
| | _ -> cont [] | |
| recAllOrNone Some xs | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment