Skip to content

Commit

Permalink
iteri looks at every element exactly once and in order - consistenly …
Browse files Browse the repository at this point in the history
…over all collections
  • Loading branch information
forki committed Jul 29, 2015
1 parent bfdc74e commit 40dc013
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,28 @@ let ``iter2 looks at every element exactly once and in order - consistenly over
Check.QuickThrowOnFailure iter2<string>
Check.QuickThrowOnFailure iter2<NormalFloat>

let iteri<'a when 'a : equality> (xs : 'a []) f' =
let list = System.Collections.Generic.List<'a>()
let indices = System.Collections.Generic.List<int>()
let f i x =
list.Add x
indices.Add i
f' i x

let s = xs |> Seq.iteri f
let l = xs |> List.ofArray |> List.iteri f
let a = xs |> Array.iteri f

let xs = Seq.toList xs
list |> Seq.toList = (xs @ xs @ xs) &&
indices |> Seq.toList = ([0..xs.Length-1] @ [0..xs.Length-1] @ [0..xs.Length-1])

[<Test>]
let ``iteri looks at every element exactly once and in order - consistenly over all collections`` () =
Check.QuickThrowOnFailure iteri<int>
Check.QuickThrowOnFailure iteri<string>
Check.QuickThrowOnFailure iteri<NormalFloat>

let sort<'a when 'a : comparison> (xs : 'a []) =
let s = xs |> Seq.sort
let l = xs |> List.ofArray |> List.sort
Expand Down

0 comments on commit 40dc013

Please sign in to comment.