on master help sign in
Add zipN and zipWithN for N in {3,4,5}
20ccc83Evan Czaplicki
libraries/List.elm+27
@@ -23,6 +23,15 @@ list must have the same type.
23
23
24# Sorting
24# Sorting
25@docs sort, sortBy, sortWith
25@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.
26-}
35-}
27
36
28import Basics (..)
37import Basics (..)
@@ -186,6 +195,15 @@ If one list is longer, the extra elements are dropped.
186zip : [a] -> [b] -> [(a,b)]
195zip : [a] -> [b] -> [(a,b)]
187zip = Native.List.zip
196zip = Native.List.zip
188
197
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+
189{-| Combine two lists, combining them with the given function.
207{-| Combine two lists, combining them with the given function.
190If one list is longer, the extra elements are dropped.
208If one list is longer, the extra elements are dropped.
191
209
@@ -194,6 +212,15 @@ If one list is longer, the extra elements are dropped.
194zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
212zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
195zipWith = Native.List.zipWith
213zipWith = Native.List.zipWith
196
214
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+
197{-| Decompose a list of tuples.
224{-| Decompose a list of tuples.
198
225
199 unzip [(0, True), (17, False), (1337, True)] == ([0,17,1337], [True,False,True])
226 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) {
199 return false;
199 return false;
200 }
200 }
201
201
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+
202 function zipWith(f, xs, ys) {
212 function zipWith(f, xs, ys) {
203 var arr = [];
213 var arr = [];
204 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
214 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
@@ -209,12 +219,47 @@ Elm.Native.List.make = function(elm) {
209 return fromArray(arr);
219 return fromArray(arr);
210 }
220 }
211
221
212− function zip(xs, ys) {
222+ function zipWith3(f, xs, ys, zs) {
213 var arr = [];
223 var arr = [];
214− while (xs.ctor !== '[]' && ys.ctor !== '[]') {
224+ while (xs.ctor !== '[]' && ys.ctor !== '[]' && zs.ctor !== '[]') {
215− arr.push(Utils.Tuple2(xs._0, ys._0));
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;
216 xs = xs._1;
260 xs = xs._1;
217 ys = ys._1;
261 ys = ys._1;
262+ zs = zs._1;
218 }
263 }
219 return fromArray(arr);
264 return fromArray(arr);
220 }
265 }
@@ -328,7 +373,10 @@ Elm.Native.List.make = function(elm) {
328
373
329 all:F2(all),
374 all:F2(all),
330 any:F2(any),
375 any:F2(any),
331− zipWith:F3(zipWith),
376+ zipWith :F3(zipWith ),
377+ zipWith3:F4(zipWith3),
378+ zipWith4:F5(zipWith4),
379+ zipWith5:F6(zipWith5),
332 zip:F2(zip),
380 zip:F2(zip),
333 sort:sort,
381 sort:sort,
334 sortBy:F2(sortBy),
382 sortBy:F2(sortBy),