[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 2dfd748 298/426: Merge pull request #87 from steve
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 2dfd748 298/426: Merge pull request #87 from steventlamb/topic/make_zip_n_ary |
Date: |
Tue, 04 Aug 2015 19:38:25 +0000 |
branch: externals/dash
commit 2dfd7488047593ebb24c5124e86ca3ad9115183f
Merge: 4ecfea6 703bb59
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Merge pull request #87 from steventlamb/topic/make_zip_n_ary
Convert -zip to support variable-length arguments
---
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 c134705 377/426: Fix compile warning, (continued)
- [elpa] externals/dash c134705 377/426: Fix compile warning, Phillip Lord, 2015/08/04
- [elpa] externals/dash 40896b8 418/426: Fix the -let debug declaration, Phillip Lord, 2015/08/04
- [elpa] externals/dash 79c23bb 282/426: Merge pull request #70 from Fuco1/debug-decl, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5d25ed2 275/426: Release 2.5.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2cc124b 423/426: [-let] Generate better code when skipping conses, Phillip Lord, 2015/08/04
- [elpa] externals/dash b7c2f9a 381/426: Work around byte compiler in -if-let*, Phillip Lord, 2015/08/04
- [elpa] externals/dash b201f0c 281/426: Add debug declarations and move indent declarations into `declare`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4e6a965 294/426: Still better debug declarations, Phillip Lord, 2015/08/04
- [elpa] externals/dash dab0d1c 384/426: Merge pull request #107 from fbergroth/workaround-byte-compiler, Phillip Lord, 2015/08/04
- [elpa] externals/dash 446c522 375/426: Release 2.10.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2dfd748 298/426: Merge pull request #87 from steventlamb/topic/make_zip_n_ary,
Phillip Lord <=
- [elpa] externals/dash a3021eb 313/426: Merge pull request #93 from lunaryorn/patch-1, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8d2227d 380/426: Silence unused variable warning in -table, Phillip Lord, 2015/08/04
- [elpa] externals/dash f5264db 291/426: Merge pull request #77 from Fuco1/split-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash c40fa44 383/426: Exclude byte compilation test on emacs23, Phillip Lord, 2015/08/04
- [elpa] externals/dash f0dd4cc 301/426: Add -annotate, Phillip Lord, 2015/08/04
- [elpa] externals/dash b44140a 288/426: Add -is-prefix/suffix/infix-p, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38ef86e 376/426: Update debug forms for -if-let and -when-let, Phillip Lord, 2015/08/04
- [elpa] externals/dash b308794 416/426: [Fix #73] Add a mention of -filter to -keep docstring, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9642c4a 385/426: [Fix #108] Sync indentation of threading forms with Clojure, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0384eee 312/426: Improve Travis CI configuration, Phillip Lord, 2015/08/04