Created
October 6, 2016 10:25
-
-
Save whitetigle/abcdbabbe12a684ebb982bab49e61d60 to your computer and use it in GitHub Desktop.
filter problem on array
This file contains hidden or 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
// Encountered problem: Seq.filter may never stops if condition is never met. | |
let testA = | |
let max = 1000000 | |
let a = [| for i in 1 .. max -> 100 |> int |] | |
console.log(a.Length) | |
let b = a |> Seq.filter( fun x -> x > 10) |> Seq.toArray | |
console.log(b.Length) | |
let testB = | |
let max = 1000000 | |
let a = [| for i in 1 .. max -> (Math.random() * 100.) |> int |] | |
console.log(a.Length) | |
let b = a |> Seq.filter( fun x -> x > 10) |> Seq.toArray | |
console.log(b.Length) | |
let testC = | |
let max = 1000000 | |
let a = [| for i in 1 .. max -> 0 |] // init with 0 | |
console.log(a.Length) | |
let b = a |> Seq.filter( fun x -> x > 10) |> Seq.toArray | |
console.log(b.Length) | |
testA // will never fail, condition will be met | |
testB // may fail if condition is never met | |
testC // fails with a maximum call stack size error since there aren't any value superior to 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment