[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 9faa422 115/439: Add -partition-by
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 9faa422 115/439: Add -partition-by |
Date: |
Tue, 04 Aug 2015 20:27:01 +0000 |
branch: externals/dash
commit 9faa4228ab7a706cba8b390d9a4b0214269d8185
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add -partition-by
---
README.md | 11 +++++++++++
dash.el | 30 ++++++++++++++++++++++++++++++
examples.el | 5 +++++
3 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 0e6dc28..95cb741 100644
--- a/README.md
+++ b/README.md
@@ -36,6 +36,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-split-with](#-split-with-pred-list) `(pred list)`
* [-partition](#-partition-n-list) `(n list)`
* [-partition-all](#-partition-all-n-list) `(n list)`
+* [-partition-by](#-partition-by-fn-list) `(fn list)`
* [-interpose](#-interpose-sep-list) `(sep list)`
* [-interleave](#-interleave-rest-lists) `(&rest lists)`
* [-replace-where](#-replace-where-pred-rep-list) `(pred rep list)`
@@ -340,6 +341,16 @@ The last group may contain less than `n` items.
(-partition-all 3 '(1 2 3 4 5 6 7)) ;; => '((1 2 3) (4 5 6) (7))
```
+### -partition-by `(fn list)`
+
+Applies `fn` to each value in `list`, splitting it each time `fn` returns a
new value.
+
+```cl
+(-partition-by 'even? '()) ;; => '()
+(-partition-by 'even? '(1 1 2 2 2 3 4 6 8)) ;; => '((1 1) (2 2 2) (3) (4 6 8))
+(--partition-by (< it 3) '(1 2 3 4 3 2 1)) ;; => '((1 2) (3 4 3) (2 1))
+```
+
### -interpose `(sep list)`
Returns a new list of all elements in `list` separated by `sep`.
diff --git a/dash.el b/dash.el
index 55074c7..ebd6681 100644
--- a/dash.el
+++ b/dash.el
@@ -345,6 +345,36 @@ The last group may contain less than N items."
(setq list (-drop n list)))
(nreverse result)))
+(defmacro --partition-by (form list)
+ "Anaphoric form of `-partition-by'."
+ (let ((r (make-symbol "result"))
+ (s (make-symbol "sublist"))
+ (v (make-symbol "value"))
+ (n (make-symbol "new-value"))
+ (l (make-symbol "list")))
+ `(let ((,l ,list))
+ (when ,l
+ (let* ((,r nil)
+ (it (car ,l))
+ (,s (list it))
+ (,v ,form)
+ (,l (cdr ,l)))
+ (while ,l
+ (let* ((it (car ,l))
+ (,n ,form))
+ (unless (equal ,v ,n)
+ (!cons (nreverse ,s) ,r)
+ (setq ,s nil)
+ (setq ,v ,n))
+ (!cons it ,s)
+ (!cdr ,l)))
+ (!cons (nreverse ,s) ,r)
+ (nreverse ,r))))))
+
+(defun -partition-by (fn list)
+ "Applies FN to each value in LIST, splitting it each time FN returns a new
value."
+ (--partition-by (funcall fn it) list))
+
(defun -interpose (sep list)
"Returns a new list of all elements in LIST separated by SEP."
(let (result)
diff --git a/examples.el b/examples.el
index 9d4518b..8b444bc 100644
--- a/examples.el
+++ b/examples.el
@@ -134,6 +134,11 @@
(-partition-all 2 '(1 2 3 4 5 6 7)) => '((1 2) (3 4) (5 6) (7))
(-partition-all 3 '(1 2 3 4 5 6 7)) => '((1 2 3) (4 5 6) (7)))
+(defexamples -partition-by
+ (-partition-by 'even? '()) => '()
+ (-partition-by 'even? '(1 1 2 2 2 3 4 6 8)) => '((1 1) (2 2 2) (3) (4 6 8))
+ (--partition-by (< it 3) '(1 2 3 4 3 2 1)) => '((1 2) (3 4 3) (2 1)))
+
(defexamples -interpose
(-interpose "-" '()) => '()
(-interpose "-" '("a")) => '("a")
- [elpa] externals/dash 38eed45 095/439: Add rainbow-dash, (continued)
- [elpa] externals/dash 38eed45 095/439: Add rainbow-dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash 49512b9 107/439: Simplify -take and -drop with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash ed47814 105/439: Add -dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash a08fc14 102/439: Add -each-while, Phillip Lord, 2015/08/04
- [elpa] externals/dash c56925a 111/439: Add font-lock for new functions and the `it` token., Phillip Lord, 2015/08/04
- [elpa] externals/dash a336d78 112/439: Add -only-some?, Phillip Lord, 2015/08/04
- [elpa] externals/dash 03370ea 108/439: Simplify examples-to-docs and examples-to-tests with dash, Phillip Lord, 2015/08/04
- [elpa] externals/dash ba80875 109/439: Nice indentation for --each, --each-while and --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9a3dfdd 113/439: Name parameter PRED when function expects predicate., Phillip Lord, 2015/08/04
- [elpa] externals/dash a081c72 114/439: Fix tests., Phillip Lord, 2015/08/04
- [elpa] externals/dash 9faa422 115/439: Add -partition-by,
Phillip Lord <=
- [elpa] externals/dash 51ddc13 110/439: Nice indentation for threading macros., Phillip Lord, 2015/08/04
- [elpa] externals/dash 2c5b86e 117/439: Add -join as alias to -distinct., Phillip Lord, 2015/08/04
- [elpa] externals/dash e9de223 116/439: Rename -replace-where to -map-when, Phillip Lord, 2015/08/04
- [elpa] externals/dash cca9b1b 121/439: Also run tests on Emacs24, Phillip Lord, 2015/08/04
- [elpa] externals/dash d857f73 118/439: Merge pull request #3 from rejeep/join-alias, Phillip Lord, 2015/08/04
- [elpa] externals/dash c025efb 122/439: Add -separate (thanks @Fuco1), Phillip Lord, 2015/08/04
- [elpa] externals/dash 69e054e 124/439: Switch around order of Contribute and Contributors parts in readme., Phillip Lord, 2015/08/04
- [elpa] externals/dash 8f91ec8 126/439: 1.0.2, Phillip Lord, 2015/08/04
- [elpa] externals/dash f034c16 119/439: Add -union (thanks to @Fuco1), Phillip Lord, 2015/08/04
- [elpa] externals/dash 5e72ff9 120/439: Include numbers in github ids., Phillip Lord, 2015/08/04