on master help sign in
Add zipN and zipWithN for N in {3,4,5}
commit20ccc834c1a597d1ef356c14073670b62f90d875 authorEvan Czaplicki <info@elm-lang.org> dateThu 22 May 2014, 15:46 (12 years ago) parenta474531
2 files changed+79−4
libraries/List.elm+27 libraries/Native/List.js+52−4
libraries/List.elm+27
@@ -23,6 +23,15 @@ list must have the same type.
2323
2424# Sorting
2525@docs sort, sortBy, sortWith
26+
27+# Additional Zips
28+@docs zip3, zip4, zip5, zipWith3, zipWith4, zipWith5
29+
30+If you can think of a legitimate use of `zipN` or `zipWithN` where `N` is 6 or
31+more, please let us know on [the
32+list](https://groups.google.com/forum/#!forum/elm-discuss). The current
33+sentiment is that it is already quite error prone once you get to 4 and
34+possibly should be approached another way.
2635-}
2736
2837import Basics (..)
@@ -186,6 +195,15 @@ If one list is longer, the extra elements are dropped.
186195zip : [a] -> [b] -> [(a,b)]
187196zip = Native.List.zip
188197
198+zip3 : [a] -> [b] -> [c] -> [(a,b,c)]
199+zip3 = Native.List.zipWith3 (,,)
200+
201+zip4 : [a] -> [b] -> [c] -> [d] -> [(a,b,c,d)]
202+zip4 = Native.List.zipWith4 (,,,)
203+
204+zip5 : [a] -> [b] -> [c] -> [d] -> [e] -> [(a,b,c,d,e)]
205+zip5 = Native.List.zipWith5 (,,,,)
206+
189207{-| Combine two lists, combining them with the given function.
190208If one list is longer, the extra elements are dropped.
191209
@@ -194,6 +212,15 @@ If one list is longer, the extra elements are dropped.
194212zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
195213zipWith = Native.List.zipWith
196214
215+zipWith3 : (a -> b -> c -> x) -> [a] -> [b] -> [c] -> [x]
216+zipWith3 = Native.List.zipWith3
217+
218+zipWith4 : (a -> b -> c -> d -> x) -> [a] -> [b] -> [c] -> [d] -> [x]
219+zipWith4 = Native.List.zipWith4
220+
221+zipWith5 : (a -> b -> c -> d -> e -> x) -> [a] -> [b] -> [c] -> [d] -> [e] -> [x]
222+zipWith5 = Native.List.zipWith5
223+
197224{-| Decompose a list of tuples.
198225
199226 unzip [(0, True), (17, False), (1337, True)] == ([0,17,1337], [True,False,True])
libraries/Native/List.js+52−4
@@ -199,6 +199,16 @@ Elm.Native.List.make = function(elm) {
199199 return false;
200200 }
201201
202+ function zip(xs, ys) {
203+ var arr = [];
204+ while (xs.ctor !== '[]' && ys.ctor !== '[]') {
205+ arr.push(Utils.Tuple2(xs._0, ys._0));
206+ xs = xs._1;
207+ ys = ys._1;
208+ }
209+ return fromArray(arr);
210+ }
211+
202212 function zipWith(f, xs, ys) {
203213 var arr = [];
204214 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
@@ -209,12 +219,47 @@ Elm.Native.List.make = function(elm) {
209219 return fromArray(arr);
210220 }
211221
212− function zip(xs, ys) {
222+ function zipWith3(f, xs, ys, zs) {
213223 var arr = [];
214− while (xs.ctor !== '[]' && ys.ctor !== '[]') {
215− arr.push(Utils.Tuple2(xs._0, ys._0));
224+ while (xs.ctor !== '[]' && ys.ctor !== '[]' && zs.ctor !== '[]') {
225+ arr.push(A3(f, xs._0, ys._0, zs._0));
226+ xs = xs._1;
227+ ys = ys._1;
228+ zs = zs._1;
229+ }
230+ return fromArray(arr);
231+ }
232+
233+ function zipWith4(f, ws, xs, ys, zs) {
234+ var arr = [];
235+ while ( ws.ctor !== '[]'
236+ && xs.ctor !== '[]'
237+ && ys.ctor !== '[]'
238+ && zs.ctor !== '[]')
239+ {
240+ arr.push(A4(f, ws._0, xs._0, ys._0, zs._0));
241+ ws = ws._1;
242+ xs = xs._1;
243+ ys = ys._1;
244+ zs = zs._1;
245+ }
246+ return fromArray(arr);
247+ }
248+
249+ function zipWith5(f, vs, ws, xs, ys, zs) {
250+ var arr = [];
251+ while ( vs.ctor !== '[]'
252+ && ws.ctor !== '[]'
253+ && xs.ctor !== '[]'
254+ && ys.ctor !== '[]'
255+ && zs.ctor !== '[]')
256+ {
257+ arr.push(A5(f, vs._0, ws._0, xs._0, ys._0, zs._0));
258+ vs = vs._1;
259+ ws = ws._1;
216260 xs = xs._1;
217261 ys = ys._1;
262+ zs = zs._1;
218263 }
219264 return fromArray(arr);
220265 }
@@ -328,7 +373,10 @@ Elm.Native.List.make = function(elm) {
328373
329374 all:F2(all),
330375 any:F2(any),
331− zipWith:F3(zipWith),
376+ zipWith :F3(zipWith ),
377+ zipWith3:F4(zipWith3),
378+ zipWith4:F5(zipWith4),
379+ zipWith5:F6(zipWith5),
332380 zip:F2(zip),
333381 sort:sort,
334382 sortBy:F2(sortBy),