Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement zipWith3 #163

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Data/Array.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ---------------------------------------------------------------------
//------------------------------------------------------------------------------
Expand Down
19 changes: 19 additions & 0 deletions src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ module Data.Array
, intersectBy

, zipWith
, zipWith3
, zipWithA
, zip
, unzip
Expand Down Expand Up @@ -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.
-- |
Expand Down
3 changes: 3 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down