Skip to content

Instantly share code, notes, and snippets.

@jackmott jackmott/filter.fs
Created Aug 10, 2016

Embed
What would you like to do?
Faster Filter Take 9,000
let filter f (array: 'T[]) =
checkNonNull array
if sizeof<'T> < 4 then
let tmp = Array.zeroCreate array.Length
let mutable count = 0
for i = 0 to array.Length - 1 do
let x = array.[i]
if f x then
tmp.[count] <- x
count <- count + 1
Array.sub tmp 0 count
else
let temp = Array.zeroCreate array.Length
let mutable count = 0
for i = 0 to array.Length-1 do
if f array.[i] then
temp.[i] <- true
count <- count + 1
let res = Array.zeroCreate count
count <- 0
let mutable i = 0
while count < res.Length do
if temp.[i] then
res.[count] <- array.[i]
count <- count + 1
i <- i + 1
res
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.