Skip to content

Instantly share code, notes, and snippets.

@jackmott
Created August 10, 2016 19:13
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/503faf010fbd3e798e64e48ddb6ede64 to your computer and use it in GitHub Desktop.
Save jackmott/503faf010fbd3e798e64e48ddb6ede64 to your computer and use it in GitHub Desktop.
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