Skip to content

Instantly share code, notes, and snippets.

@nojaf
Created October 2, 2017 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nojaf/e082382d485af72cbe1d5bcd94d11e84 to your computer and use it in GitHub Desktop.
Save nojaf/e082382d485af72cbe1d5bcd94d11e84 to your computer and use it in GitHub Desktop.
Split Result list into oks and errors
let mapPartition (partitioner : 'T -> Result<'U1, 'U2>) list : 'U1 list * 'U2 list =
// OPTIMIZATION : If the input list is empty, immediately return empty results.
if List.isEmpty list then
[], []
else
// Mutable variables are used here instead of List.fold for maximum performance.
let mutable list = list
let mutable resultList1 = []
let mutable resultList2 = []
// Partition the list, consing the elements onto the list
// specified by the partition function.
while not <| List.isEmpty list do
match partitioner <| List.head list with
| Ok element ->
resultList1 <- element :: resultList1
| Error element ->
resultList2 <- element :: resultList2
// Remove the first element from the input list.
list <- List.tail list
// Reverse the result lists and return them.
List.rev resultList1, List.rev resultList2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment