Created
October 2, 2017 11:23
-
-
Save nojaf/e082382d485af72cbe1d5bcd94d11e84 to your computer and use it in GitHub Desktop.
Split Result list into oks and errors
This file contains 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 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