Skip to content

Commit

Permalink
Merge pull request #738 from atadi96/master
Browse files Browse the repository at this point in the history
Add proxy for .AllPairs of collections
  • Loading branch information
Jand42 authored Aug 14, 2017
2 parents 11ea409 + 01bd0cb commit a165ce4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/stdlib/WebSharper.Main/Proxy/ArrayModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ let push (x: obj) (y: obj) = ()
[<Inline "$arr1.concat($arr2)">]
let Append<'T> (arr1: 'T []) (arr2: 'T []) : 'T [] = arr1

[<Name "allPairs">]
let AllPairs (array1: 'T1 []) (array2: 'T2 []) =
let len1 = Array.length array1
let len2 = Array.length array2
let res = JavaScript.Array (len1 * len2)
for i = 0 to len1-1 do
for j = 0 to len2-1 do
res.[i * len2 + j] <- (array1.JS.[i],array2.JS.[j])
res |> As<('T1 * 'T2) []>

[<Name "average">]
let inline Average (arr: 'T []): 'T = As (float (Array.sum arr) / float (Array.length arr))

Expand Down
7 changes: 7 additions & 0 deletions src/stdlib/WebSharper.Main/Proxy/ListModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ let freshTail (l: list<'T>) =
[<Inline "$l.$ == 1">]
let notEmpty (l: list<_>) = X<bool>

[<Name "allPairs">]
let AllPairs (l1: list<_>) (l2: list<_>) =
let arr1 = Array.ofList l1
let arr2 = Array.ofList l2
let res = Array.allPairs arr1 arr2
List.ofArray res

[<Name "append">]
let Append (x: list<'T>) (y: list<'T>) =
if List.isEmpty x then y
Expand Down
5 changes: 5 additions & 0 deletions src/stdlib/WebSharper.Main/Proxy/SeqModule.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ open WebSharper.CollectionInternals
let safeDispose (x: System.IDisposable) =
if x <> null then x.Dispose()

[<Name "allPairs">]
let AllPairs (source1: seq<_>) (source2: seq<_>) =
let cached = Seq.cache source2
source1 |> Seq.collect (fun x -> cached |> Seq.map (fun y -> x,y))

[<Name "append">]
let Append (s1: seq<'T>) (s2: seq<'T>) : seq<'T> =
Enumerable.Of (fun () ->
Expand Down
11 changes: 11 additions & 0 deletions tests/WebSharper.Tests/Array.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ let Tests =
equalMsg fibonacci [| 2; 3; 5; 8; 13 |] "fibs"
}

Test "Array.allPairs" {
let (arr1, arr2) = ([| 1; 2; 3 |], [| 'a'; 'b' |])
equal (Array.allPairs [||] [||]) [||]
equal (Array.allPairs [|1|] [||]) [||]
equal (Array.allPairs [||] [|1|]) [||]
equal (Array.allPairs [|1|] [|2|]) [|(1,2)|]
equal (Array.allPairs arr1 arr2) [|(1,'a');(1,'b');(2,'a');(2,'b');(3,'a');(3,'b')|]
equal arr1 [| 1; 2; 3 |]
equal arr2 [| 'a'; 'b'|]
}

Test "Array.append" {
let (arr1, arr2) = ([| 1; 2; 3 |], [| 4; 5 |])
equal (Array.append arr1 arr2) [| 1; 2; 3; 4; 5 |]
Expand Down
8 changes: 8 additions & 0 deletions tests/WebSharper.Tests/List.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ let Tests =
equal (1 :: 2 :: 3 :: []) [1; 2; 3]
}

Test "List.allPairs" {
equal (List.allPairs [] []) []
equal (List.allPairs [1] []) []
equal (List.allPairs [] [1]) []
equal (List.allPairs [1] [2]) [(1,2)]
equal (List.allPairs [1; 2; 3] ['a';'b']) [(1,'a');(1,'b');(2,'a');(2,'b');(3,'a');(3,'b')]
}

Test "List.append" {
equal (List.append [1; 2; 3] [4; 5]) [1..5]
equal (List.append [] []) []
Expand Down
13 changes: 13 additions & 0 deletions tests/WebSharper.Tests/Seq.fs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ let Tests =

TestCategory "Seq" {

Test "Seq.allPairs" {
let empty = Seq.toArray Seq.empty
equal (Seq.allPairs Seq.empty Seq.empty |> Seq.toArray) empty
equal (Seq.allPairs (seq { yield 1 }) Seq.empty |> Seq.toArray) empty
equal (Seq.allPairs Seq.empty (seq { yield 1 }) |> Seq.toArray) empty
equal (Seq.allPairs (seq { yield 1 }) (seq { yield 2 }) |> Seq.toArray) ((seq { yield (1,2) }) |> Seq.toArray)
equal (Seq.allPairs
(seq { 1 .. 3})
(seq { 'a' .. 'b' })
|> Seq.toArray
) ([|(1,'a');(1,'b');(2,'a');(2,'b');(3,'a');(3,'b')|])
}

Test "Seq.append" {
equal (Seq.append (seq { 1 .. 5 }) (seq { 6 .. 10 }) |> Seq.toArray)
[| 1 .. 10 |]
Expand Down

0 comments on commit a165ce4

Please sign in to comment.