[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 703bb59 297/426: Convert -zip to support variable-
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 703bb59 297/426: Convert -zip to support variable-length arguments |
Date: |
Tue, 04 Aug 2015 19:38:24 +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 43dcd37 295/426: Only eval NUM in --dotimes once, (continued)
- [elpa] externals/dash 43dcd37 295/426: Only eval NUM in --dotimes once, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4ecfea6 296/426: Merge pull request #79 from Fuco1/debug-forms, Phillip Lord, 2015/08/04
- [elpa] externals/dash 624c501 306/426: Add `-table` and `-table-flat`, Phillip Lord, 2015/08/04
- [elpa] externals/dash cf7ca23 415/426: [Fix #97] Add -remove-item, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2eefbec 386/426: Don't use an inline comment on a line of its own, Phillip Lord, 2015/08/04
- [elpa] externals/dash bbc1d9c 309/426: `--each-while` should also expose `it-index`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6bdcfa5 286/426: Add -split-on, -split-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash bd85b7c 302/426: Make -zip support infinite (circular) lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash 19fbc24 426/426: Fix anaphoric -spice-list macro, Phillip Lord, 2015/08/04
- [elpa] externals/dash 36b7f49 419/426: [-let] Reuse the ignore-place predicate, Phillip Lord, 2015/08/04
- [elpa] externals/dash 703bb59 297/426: Convert -zip to support variable-length arguments,
Phillip Lord <=
- [elpa] externals/dash 3132ae0 314/426: Alias -tail to nthcdr, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4098ea0 285/426: Merge pull request #74 from YoungFrog/dashfontifix, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0c045f2 369/426: Highlight with el instead of cl, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5086d24 293/426: Merge pull request #60 from rejeep/cask, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1fde888 407/426: Merge pull request #125 from wasamasa/feature-some, Phillip Lord, 2015/08/04
- [elpa] externals/dash 51a07b1 408/426: Merge pull request #127 from occidens/fixfn, Phillip Lord, 2015/08/04
- [elpa] externals/dash ffa37bf 425/426: Merge pull request #129 from holomorph/docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash 63ec298 308/426: Merge pull request #92 from Fuco1/outer-product, Phillip Lord, 2015/08/04
- [elpa] externals/dash 00549e4 307/426: Add missing fontification keywords, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9ebd172 421/426: [-let] Generate differently named symbols for temporary sources (makes, Phillip Lord, 2015/08/04