Skip to content

Instantly share code, notes, and snippets.

@whitetigle
Created October 6, 2016 10:25
Show Gist options
  • Save whitetigle/abcdbabbe12a684ebb982bab49e61d60 to your computer and use it in GitHub Desktop.
Save whitetigle/abcdbabbe12a684ebb982bab49e61d60 to your computer and use it in GitHub Desktop.
filter problem on array
// 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