Skip to content

Instantly share code, notes, and snippets.

@jackmott
Created August 12, 2016 12:17
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 jackmott/610d7339952ee09be1b99e43066d4c2b to your computer and use it in GitHub Desktop.
Save jackmott/610d7339952ee09be1b99e43066d4c2b to your computer and use it in GitHub Desktop.
Core lib parallel Array.chose F#
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