Skip to content

Instantly share code, notes, and snippets.

@jackmott jackmott/choose.fs
Created Aug 12, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.