Skip to content

Commit

Permalink
map2 looks at every element exactly once and in order - consistenly o…
Browse files Browse the repository at this point in the history
…ver all collections when size is equal
  • Loading branch information
forki committed Jul 29, 2015
1 parent d276384 commit f5b0022
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,28 @@ let ``map is consistent`` () =
Check.QuickThrowOnFailure map<string>
Check.QuickThrowOnFailure map<float>

let map2<'a when 'a : equality> (xs' : ('a*'a) []) f' =
let xs = xs' |> Array.map fst
let xs2 = xs' |> Array.map snd
let list = System.Collections.Generic.List<'a*'a>()
let f x y =
list.Add <| (x,y)
f' x y

let s = Seq.map2 f xs xs2
let l = List.map2 f (xs |> List.ofArray) (xs2 |> List.ofArray)
let a = Array.map2 f xs xs2

let xs = Seq.toList xs'
Seq.toArray s = a && List.toArray l = a &&
list |> Seq.toList = (xs @ xs @ xs)

[<Test>]
let ``map2 looks at every element exactly once and in order - consistenly over all collections when size is equal`` () =
Check.QuickThrowOnFailure map2<int>
Check.QuickThrowOnFailure map2<string>
Check.QuickThrowOnFailure map2<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 f5b0022

Please sign in to comment.