[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 703bb59 297/439: Convert -zip to support variable-
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 703bb59 297/439: Convert -zip to support variable-length arguments |
Date: |
Tue, 04 Aug 2015 20:29:23 +0000 |
branch: externals/dash
commit 703bb599c6cfa48802d272bc1db9786dd2785207
Author: steve <address@hidden>
Commit: steve <address@hidden>
Convert -zip to support variable-length arguments
fixes #82 on github
---
README.md | 15 +++++++++------
dash.el | 27 +++++++++++++++++++++------
dev/examples.el | 4 +++-
readme-template.md | 1 +
4 files changed, 34 insertions(+), 13 deletions(-)
diff --git a/README.md b/README.md
index 445c41e..07b304f 100644
--- a/README.md
+++ b/README.md
@@ -133,7 +133,7 @@ Operations dual to reductions, building lists from seed
value rather than consum
* [-interpose](#-interpose-sep-list) `(sep list)`
* [-interleave](#-interleave-rest-lists) `(&rest lists)`
* [-zip-with](#-zip-with-fn-list1-list2) `(fn list1 list2)`
-* [-zip](#-zip-list1-list2) `(list1 list2)`
+* [-zip](#-zip-rest-lists) `(&rest lists)`
* [-first](#-first-pred-list) `(pred list)`
* [-last](#-last-pred-list) `(pred list)`
* [-first-item](#-first-item-list) `(list)`
@@ -1088,12 +1088,14 @@ and the elements from `list2` as `other`.
(--zip-with (concat it " and " other) '("Batman" "Jekyll") '("Robin" "Hyde"))
;; => '("Batman and Robin" "Jekyll and Hyde")
```
-#### -zip `(list1 list2)`
+#### -zip `(&rest lists)`
-Zip the two lists together. Return the list where elements
-are cons pairs with car being element from `list1` and cdr being
-element from `list2`. The length of the returned list is the
-length of the shorter one.
+Zip `lists` together. Group the head of each list, followed by the
+second elements of each list, and so on. The lengths of the returned
+groupings are equal to the length of the shortest input list.
+
+If two lists are provided as arguments, return the groupings as a list
+of cons cells. Otherwise, return the groupings as a list of lists.
```cl
(-zip '(1 2 3) '(4 5 6)) ;; => '((1 . 4) (2 . 5) (3 . 6))
@@ -1667,6 +1669,7 @@ Change `readme-template.md` or `examples-to-docs.el`
instead.
- [Emanuel Evans](https://github.com/shosti) contributed `-if-let`,
`-when-let` and `-insert-at`.
- [Johan Andersson](https://github.com/rejeep) contributed `-sum`, `-product`
and `-same-items?`
- [Christina Whyte](https://github.com/kurisuwhyte) contributed `-compose`
+ - [Steve Lamb](https://github.com/steventlamb) contributed an n-ary version
of `-zip`
Thanks!
diff --git a/dash.el b/dash.el
index 9c2a928..8667583 100644
--- a/dash.el
+++ b/dash.el
@@ -796,12 +796,27 @@ The anaphoric form `--zip-with' binds the elements from
LIST1 as `it`,
and the elements from LIST2 as `other`."
(--zip-with (funcall fn it other) list1 list2))
-(defun -zip (list1 list2)
- "Zip the two lists together. Return the list where elements
-are cons pairs with car being element from LIST1 and cdr being
-element from LIST2. The length of the returned list is the
-length of the shorter one."
- (-zip-with 'cons list1 list2))
+(defun -zip (&rest lists)
+ "Zip LISTS together. Group the head of each list, followed by the
+second elements of each list, and so on. The lengths of the returned
+groupings are equal to the length of the shortest input list.
+
+If two lists are provided as arguments, return the groupings as a list
+of cons cells. Otherwise, return the groupings as a list of lists. "
+ (let* ((n (-min (-map 'length lists)))
+ (level-lists (-map (-partial '-take n) lists))
+ results)
+ (while (> n 0)
+ (let ((split-lists (-map (-partial '-split-at 1) level-lists)))
+ (setq results (cons (-map 'caar split-lists) results))
+ (setq level-lists (-map 'cadr split-lists))
+ (setq n (1- n))))
+ (setq results (nreverse results))
+ (if (= (length lists) 2)
+ ; to support backward compatability, return
+ ; a cons cell if two lists were provided
+ (--map (cons (car it) (cadr it)) results)
+ results)))
(defun -partial (fn &rest args)
"Takes a function FN and fewer than the normal arguments to FN,
diff --git a/dev/examples.el b/dev/examples.el
index 0c4751a..4042630 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -435,7 +435,9 @@
(defexamples -zip
(-zip '(1 2 3) '(4 5 6)) => '((1 . 4) (2 . 5) (3 . 6))
(-zip '(1 2 3) '(4 5 6 7)) => '((1 . 4) (2 . 5) (3 . 6))
- (-zip '(1 2 3 4) '(4 5 6)) => '((1 . 4) (2 . 5) (3 . 6)))
+ (-zip '(1 2 3 4) '(4 5 6)) => '((1 . 4) (2 . 5) (3 . 6))
+ (-zip '(1 2 3) '(4 5 6) '(7 8 9)) => '((1 4 7) (2 5 8) (3 6 9))
+ (-zip '(1 2) '(3 4 5) '(6)) => '((1 3 6)))
(defexamples -first
(-first 'even? '(1 2 3)) => 2
diff --git a/readme-template.md b/readme-template.md
index 968b2d5..d3eecb0 100644
--- a/readme-template.md
+++ b/readme-template.md
@@ -174,6 +174,7 @@ Change `readme-template.md` or `examples-to-docs.el`
instead.
- [Emanuel Evans](https://github.com/shosti) contributed `-if-let`,
`-when-let` and `-insert-at`.
- [Johan Andersson](https://github.com/rejeep) contributed `-sum`, `-product`
and `-same-items?`
- [Christina Whyte](https://github.com/kurisuwhyte) contributed `-compose`
+ - [Steve Lamb](https://github.com/steventlamb) contributed an n-ary version
of `-zip`
Thanks!
- [elpa] externals/dash b075846 287/439: Add -iterate, -unfold, (continued)
- [elpa] externals/dash b075846 287/439: Add -iterate, -unfold, Phillip Lord, 2015/08/04
- [elpa] externals/dash f96196a 289/439: Merge pull request #75 from Fuco1/prefix, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6bdcfa5 286/439: Add -split-on, -split-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash b44140a 288/439: Add -is-prefix/suffix/infix-p, Phillip Lord, 2015/08/04
- [elpa] externals/dash abff641 290/439: Merge pull request #76 from Fuco1/unfold, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8dfa9b6 292/439: Release 2.6.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash f5264db 291/439: Merge pull request #77 from Fuco1/split-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5086d24 293/439: Merge pull request #60 from rejeep/cask, Phillip Lord, 2015/08/04
- [elpa] externals/dash 43dcd37 295/439: Only eval NUM in --dotimes once, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4e6a965 294/439: Still better debug declarations, Phillip Lord, 2015/08/04
- [elpa] externals/dash 703bb59 297/439: Convert -zip to support variable-length arguments,
Phillip Lord <=
- [elpa] externals/dash 4ecfea6 296/439: Merge pull request #79 from Fuco1/debug-forms, Phillip Lord, 2015/08/04
- [elpa] externals/dash c08fcba 299/439: Fixed documentation of `-last-item'., Phillip Lord, 2015/08/04
- [elpa] externals/dash c952a54 300/439: Merge pull request #90 from tmalsburg/last-item-doc, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2dfd748 298/439: Merge pull request #87 from steventlamb/topic/make_zip_n_ary, Phillip Lord, 2015/08/04
- [elpa] externals/dash bd85b7c 302/439: Make -zip support infinite (circular) lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash f0dd4cc 301/439: Add -annotate, Phillip Lord, 2015/08/04
- [elpa] externals/dash f780322 305/439: Add `-flatten-n`, Phillip Lord, 2015/08/04
- [elpa] externals/dash fb51f8f 304/439: Merge pull request #88 from steventlamb/topic/zip_cycle_pad, Phillip Lord, 2015/08/04
- [elpa] externals/dash 00549e4 307/439: Add missing fontification keywords, Phillip Lord, 2015/08/04
- [elpa] externals/dash cd137e0 310/439: `-slice` should not fill the returned list with nils if to > length, Phillip Lord, 2015/08/04