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