Skip to content

Commit

Permalink
Merge pull request dotnet#989 from PatrickMcDonald/allPairs
Browse files Browse the repository at this point in the history
Implement allPairs on List, Seq and Array modules
  • Loading branch information
KevinRansom committed Mar 8, 2016
2 parents 22ba027 + 4f76b0e commit 2ffb0b6
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ type ArrayModule() =
()


[<Test>]
member this.AllPairs() =
// integer array
let resultInt = Array.allPairs [|1..3|] [|2..2..6|]
if resultInt <> [|(1,2);(1,4);(1,6)
(2,2);(2,4);(2,6)
(3,2);(3,4);(3,6)|] then Assert.Fail()

// string array
let resultStr = Array.allPairs [|"A"; "B"; "C" ; "D" |] [|"a";"b";"c";"d"|]
if resultStr <> [|("A","a");("A","b");("A","c");("A","d")
("B","a");("B","b");("B","c");("B","d")
("C","a");("C","b");("C","c");("C","d")
("D","a");("D","b");("D","c");("D","d")|] then Assert.Fail()

// empty array
if Array.allPairs [||] [||] <> [||] then Assert.Fail()
if Array.allPairs [|1..3|] [||] <> [||] then Assert.Fail()
if Array.allPairs [||] [|1..3|] <> [||] then Assert.Fail()

// null array
let nullArr = null:string[]
CheckThrowsArgumentNullException (fun () -> Array.allPairs nullArr nullArr |> ignore)
CheckThrowsArgumentNullException (fun () -> Array.allPairs [||] nullArr |> ignore)
CheckThrowsArgumentNullException (fun () -> Array.allPairs nullArr [||] |> ignore)

()

[<Test>]
member this.Append() =
// integer array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ open NUnit.Framework
open FsCheck
open Utils

let allPairs<'a when 'a : equality> (xs : list<'a>) (xs2 : list<'a>) =
let s = xs |> Seq.allPairs xs2
let l = xs |> List.allPairs xs2
let a = xs |> Seq.toArray |> Array.allPairs (Seq.toArray xs2)
Seq.toArray s = a && List.toArray l = a

[<Test>]
let ``allPairs is consistent`` () =
Check.QuickThrowOnFailure allPairs<int>
Check.QuickThrowOnFailure allPairs<string>
Check.QuickThrowOnFailure allPairs<NormalFloat>

let append<'a when 'a : equality> (xs : list<'a>) (xs2 : list<'a>) =
let s = xs |> Seq.append xs2
let l = xs |> List.append xs2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ type ListModule() =

()

[<Test>]
member this.AllPairs() =
// integer List
let resultInt = List.allPairs [1..3] [2..2..6]
Assert.AreEqual([(1,2);(1,4);(1,6)
(2,2);(2,4);(2,6)
(3,2);(3,4);(3,6)], resultInt)

// string List
let resultStr = List.allPairs [2;3;4;5] ["b";"c";"d";"e"]
Assert.AreEqual([(2,"b");(2,"c");(2,"d");(2,"e")
(3,"b");(3,"c");(3,"d");(3,"e")
(4,"b");(4,"c");(4,"d");(4,"e")
(5,"b");(5,"c");(5,"d");(5,"e")] , resultStr)

// empty List
let resultEpt = List.allPairs [] []
let empTuple:(obj*obj) list = []
Assert.AreEqual(empTuple, resultEpt)

()

[<Test>]
member this.Append() =
// integer List
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -823,3 +823,35 @@ let ``List.sumBy calculates the sum of the mapped list`` () =
Check.QuickThrowOnFailure sumBy<int>
Check.QuickThrowOnFailure sumBy<string>
Check.QuickThrowOnFailure sumBy<float>

let allPairsCount<'a, 'b> (xs : 'a list) (ys : 'b list) =
let pairs = List.allPairs xs ys
pairs.Length = xs.Length * ys.Length

[<Test>]
let ``List.allPairs produces the correct number of pairs`` () =
Check.QuickThrowOnFailure allPairsCount<int, int>
Check.QuickThrowOnFailure allPairsCount<string, string>
Check.QuickThrowOnFailure allPairsCount<float, float>

let allPairsFst<'a, 'b when 'a : equality> (xs : 'a list) (ys : 'b list) =
let pairsFst = List.allPairs xs ys |> List.map fst
let check = xs |> List.collect (List.replicate ys.Length)
pairsFst = check

[<Test>]
let ``List.allPairs first elements are correct`` () =
Check.QuickThrowOnFailure allPairsFst<int, int>
Check.QuickThrowOnFailure allPairsFst<string, string>
Check.QuickThrowOnFailure allPairsFst<NormalFloat, NormalFloat>

let allPairsSnd<'a, 'b when 'b : equality> (xs : 'a list) (ys : 'b list) =
let pairsSnd = List.allPairs xs ys |> List.map snd
let check = [ for i in 1 .. xs.Length do yield! ys ]
pairsSnd = check

[<Test>]
let ``List.allPairs second elements are correct`` () =
Check.QuickThrowOnFailure allPairsFst<int, int>
Check.QuickThrowOnFailure allPairsFst<string, string>
Check.QuickThrowOnFailure allPairsFst<NormalFloat, NormalFloat>
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,33 @@ Make sure each method works on:
[<TestFixture>]
type SeqModule() =

[<Test>]
member this.AllPairs() =

// integer Seq
let resultInt = Seq.allPairs (seq [1..7]) (seq [11..17])
let expectedInt =
seq { for i in 1..7 do
for j in 11..17 do
yield i, j }
VerifySeqsEqual expectedInt resultInt

// string Seq
let resultStr = Seq.allPairs (seq ["str3";"str4"]) (seq ["str1";"str2"])
let expectedStr = seq ["str3","str1";"str3","str2";"str4","str1";"str4","str2"]
VerifySeqsEqual expectedStr resultStr

// empty Seq
VerifySeqsEqual Seq.empty <| Seq.allPairs Seq.empty Seq.empty
VerifySeqsEqual Seq.empty <| Seq.allPairs { 1..7 } Seq.empty
VerifySeqsEqual Seq.empty <| Seq.allPairs Seq.empty { 1..7 }

// null Seq
CheckThrowsArgumentNullException(fun() -> Seq.allPairs null null |> ignore)
CheckThrowsArgumentNullException(fun() -> Seq.allPairs null (seq [1..7]) |> ignore)
CheckThrowsArgumentNullException(fun() -> Seq.allPairs (seq [1..7]) null |> ignore)
()

[<Test>]
member this.CachedSeq_Clear() =

Expand Down
3 changes: 3 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.net40.fs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[System.Int32,T][] Indexed[T](T[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] AllPairs[T1,T2](T1[], T2[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
Expand Down Expand Up @@ -296,6 +297,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[Microsoft.FSharp.Collections.FSharpList`1[T]] Windowed[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[System.Int32,T]] Indexed[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] AllPairs[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] Zip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,Microsoft.FSharp.Collections.FSharpList`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
Expand Down Expand Up @@ -432,6 +434,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.Int32,T]] Indexed[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] AllPairs[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] Zip[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Collections.Generic.IEnumerable`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
Expand Down
3 changes: 3 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable259.fs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[System.Int32,T][] Indexed[T](T[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] AllPairs[T1,T2](T1[], T2[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
Expand Down Expand Up @@ -283,6 +284,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[Microsoft.FSharp.Collections.FSharpList`1[T]] Windowed[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[System.Int32,T]] Indexed[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] AllPairs[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] Zip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,Microsoft.FSharp.Collections.FSharpList`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
Expand Down Expand Up @@ -419,6 +421,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.Int32,T]] Indexed[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] AllPairs[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] Zip[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Collections.Generic.IEnumerable`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
Expand Down
3 changes: 3 additions & 0 deletions src/fsharp/FSharp.Core.Unittests/SurfaceArea.portable47.fs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Microsoft.FSharp.Collections.ArrayModule: System.Collections.Generic.IEnumerable
Microsoft.FSharp.Collections.ArrayModule: System.String ToString()
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[System.Int32,T][] Indexed[T](T[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T,T][] Pairwise[T](T[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] AllPairs[T1,T2](T1[], T2[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1,T2][] Zip[T1,T2](T1[], T2[])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[T1[],T2[]] Unzip[T1,T2](System.Tuple`2[T1,T2][])
Microsoft.FSharp.Collections.ArrayModule: System.Tuple`2[TKey,System.Int32][] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], T[])
Expand Down Expand Up @@ -280,6 +281,7 @@ Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[Microsoft.FSharp.Collections.FSharpList`1[T]] Windowed[T](Int32, Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[System.Int32,T]] Indexed[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T,T]] Pairwise[T](Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] AllPairs[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[T1,T2]] Zip[T1,T2](Microsoft.FSharp.Collections.FSharpList`1[T1], Microsoft.FSharp.Collections.FSharpList`1[T2])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,Microsoft.FSharp.Collections.FSharpList`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
Microsoft.FSharp.Collections.ListModule: Microsoft.FSharp.Collections.FSharpList`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], Microsoft.FSharp.Collections.FSharpList`1[T])
Expand Down Expand Up @@ -416,6 +418,7 @@ Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T]
Microsoft.FSharp.Collections.SeqModule: Microsoft.FSharp.Core.FSharpOption`1[T] TryLast[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[System.Int32,T]] Indexed[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T,T]] Pairwise[T](System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] AllPairs[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[T1,T2]] Zip[T1,T2](System.Collections.Generic.IEnumerable`1[T1], System.Collections.Generic.IEnumerable`1[T2])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Collections.Generic.IEnumerable`1[T]]] GroupBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
Microsoft.FSharp.Collections.SeqModule: System.Collections.Generic.IEnumerable`1[System.Tuple`2[TKey,System.Int32]] CountBy[T,TKey](Microsoft.FSharp.Core.FSharpFunc`2[T,TKey], System.Collections.Generic.IEnumerable`1[T])
Expand Down
Loading

0 comments on commit 2ffb0b6

Please sign in to comment.