From 37bfde86438bbac5c7aa7e495967b467baec8e64 Mon Sep 17 00:00:00 2001 From: TOTBWF Date: Mon, 13 Jan 2020 15:04:55 -0800 Subject: [PATCH 1/3] Implement zipWith3 --- src/Data/Array.js | 15 +++++++++++++++ src/Data/Array.purs | 19 +++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/Data/Array.js b/src/Data/Array.js index 1b66a893..d01cfb0f 100644 --- a/src/Data/Array.js +++ b/src/Data/Array.js @@ -293,6 +293,21 @@ exports.zipWith = function (f) { }; }; +exports.zipWith3 = function (f) { + return function (xs) { + return function (ys) { + return function (zs) { + var l = Math.min(xs.length, ys.length, zs.length); + var result = new Array(l); + for (var i = 0; i < l; i++) { + result[i] = f(xs[i])(ys[i])(zs[i]) + } + return result; + }; + }; + }; +}; + //------------------------------------------------------------------------------ // Partial --------------------------------------------------------------------- //------------------------------------------------------------------------------ diff --git a/src/Data/Array.purs b/src/Data/Array.purs index 6349418a..27f580b0 100644 --- a/src/Data/Array.purs +++ b/src/Data/Array.purs @@ -103,6 +103,7 @@ module Data.Array , intersectBy , zipWith + , zipWith3 , zipWithA , zip , unzip @@ -1037,6 +1038,24 @@ foreign import zipWith -> Array b -> Array c +-- | Apply a function to pairs of elements at the same index in three arrays, +-- | collecting the results in a new array. +-- | +-- | If one of the arrays is longer, elements will be discarded from the longer arrays. +-- | +-- | For example +-- | +-- | ```purescript +-- | zipWith (\x y z -> (x + y) * z) [1, 2, 3] [4, 5, 6, 7] [1, 2, 3, 4] == [4, 14, 27] +-- | ``` +foreign import zipWith3 + :: forall a b c d + . (a -> b -> c -> d) + -> Array a + -> Array b + -> Array c + -> Array d + -- | A generalization of `zipWith` which accumulates results in some -- | `Applicative` functor. -- | From 7b7a43038f22a93f7508cb06013bd5e4b306517a Mon Sep 17 00:00:00 2001 From: TOTBWF Date: Mon, 13 Jan 2020 15:12:03 -0800 Subject: [PATCH 2/3] Add test for zipWith3 --- test/Test/Data/Array.purs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/Test/Data/Array.purs b/test/Test/Data/Array.purs index d1c59f6a..479960aa 100644 --- a/test/Test/Data/Array.purs +++ b/test/Test/Data/Array.purs @@ -355,6 +355,9 @@ testArray = do log "zipWith should use the specified function to zip two lists together" assert $ A.zipWith (\x y -> [show x, y]) [1, 2, 3] ["a", "b", "c"] == [["1", "a"], ["2", "b"], ["3", "c"]] + log "zipWith3 should use the specified function to zip three lists together" + assert $ A.zipWith3 (\x y z -> [show x, y, show z]) [1, 2, 3] ["a", "b", "c"] [4, 5, 6] == [["1", "a", "4"], ["2", "b", "5"], ["3", "c", "6"]] + log "zipWithA should use the specified function to zip two lists together" assert $ A.zipWithA (\x y -> Just $ Tuple x y) [1, 2, 3] ["a", "b", "c"] == Just [Tuple 1 "a", Tuple 2 "b", Tuple 3 "c"] From e89eab3d6651f93e29d47fe96e8a8979bd4c9ac0 Mon Sep 17 00:00:00 2001 From: TOTBWF Date: Mon, 13 Jan 2020 15:20:40 -0800 Subject: [PATCH 3/3] Fix formatting error --- src/Data/Array.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Data/Array.js b/src/Data/Array.js index d01cfb0f..c130ed7d 100644 --- a/src/Data/Array.js +++ b/src/Data/Array.js @@ -300,7 +300,7 @@ exports.zipWith3 = function (f) { var l = Math.min(xs.length, ys.length, zs.length); var result = new Array(l); for (var i = 0; i < l; i++) { - result[i] = f(xs[i])(ys[i])(zs[i]) + result[i] = f(xs[i])(ys[i])(zs[i]); } return result; };