Created
August 12, 2016 12:17
-
-
Save jackmott/610d7339952ee09be1b99e43066d4c2b to your computer and use it in GitHub Desktop.
Core lib parallel Array.chose F#
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 choose f (array: 'T[]) = | |
let inputLength = array.Length | |
let isChosen : bool [] = Array.zeroCreate inputLength | |
let results : 'U [] = Array.zeroCreate inputLength | |
Parallel.For(0, inputLength, (fun i -> | |
match f array.[i] with | |
| None -> () | |
| Some v -> | |
isChosen.[i] <- true; | |
results.[i] <- v | |
)) |> ignore | |
let mutable outputLength = 0 | |
for i = 0 to isChosen.Length-1 do | |
if isChosen.[i] then | |
outputLength <- outputLength + 1 | |
let output = Array.zeroCreate outputLength | |
let mutable curr = 0 | |
for i = 0 to isChosen.Length-1 do | |
if isChosen.[i] then | |
output.[curr] <- results.[i] | |
curr <- curr + 1 | |
output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment