[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash adfafcb 152/426: Add -partition-by-header
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash adfafcb 152/426: Add -partition-by-header |
Date: |
Tue, 04 Aug 2015 19:37:21 +0000 |
branch: externals/dash
commit adfafcb468b75c90775d1a74e5f7a802f9cd6377
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add -partition-by-header
---
README.md | 16 +++++++++++++++-
dash.el | 39 ++++++++++++++++++++++++++++++++++++++-
dev/examples.el | 5 +++++
3 files changed, 58 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index a710f64..8e2680d 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-partition](#-partition-n-list) `(n list)`
* [-partition-all](#-partition-all-n-list) `(n list)`
* [-partition-by](#-partition-by-fn-list) `(fn list)`
+* [-partition-by-header](#-partition-by-header-fn-list) `(fn list)`
* [-group-by](#-group-by-fn-list) `(fn list)`
* [-interpose](#-interpose-sep-list) `(sep list)`
* [-interleave](#-interleave-rest-lists) `(&rest lists)`
@@ -420,7 +421,7 @@ The last group may contain less than `n` items.
### -partition-by `(fn list)`
-Applies `fn` to each value in `list`, splitting it each time `fn` returns a
new value.
+Applies `fn` to each item in `list`, splitting it each time `fn` returns a new
value.
```cl
(-partition-by 'even? '()) ;; => '()
@@ -428,6 +429,19 @@ Applies `fn` to each value in `list`, splitting it each
time `fn` returns a new
(--partition-by (< it 3) '(1 2 3 4 3 2 1)) ;; => '((1 2) (3 4 3) (2 1))
```
+### -partition-by-header `(fn list)`
+
+Applies `fn` to the first item in `list`. That is the header
+ value. Applies `fn` to each item in `list`, splitting it each time
+ `fn` returns the header value, but only after seeing at least one
+ other value (the body).
+
+```cl
+(--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4)) ;; => '((1 2 3) (1 2) (1
2 3 4))
+(--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0)) ;; => '((1 2 0) (1 0) (1
2 3 0))
+(-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1)) ;; => '((2 1 1 1) (4 1
3 5) (6 6 1))
+```
+
### -group-by `(fn list)`
Separate `list` into an alist whose keys are `fn` applied to the
diff --git a/dash.el b/dash.el
index 4c40dc8..a924424 100644
--- a/dash.el
+++ b/dash.el
@@ -462,9 +462,46 @@ The last group may contain less than N items."
(nreverse ,r))))))
(defun -partition-by (fn list)
- "Applies FN to each value in LIST, splitting it each time FN returns a new
value."
+ "Applies FN to each item in LIST, splitting it each time FN returns a new
value."
(--partition-by (funcall fn it) list))
+(defmacro --partition-by-header (form list)
+ "Anaphoric form of `-partition-by-header'."
+ (let ((r (make-symbol "result"))
+ (s (make-symbol "sublist"))
+ (h (make-symbol "header-value"))
+ (b (make-symbol "seen-body?"))
+ (n (make-symbol "new-value"))
+ (l (make-symbol "list")))
+ `(let ((,l ,list))
+ (when ,l
+ (let* ((,r nil)
+ (it (car ,l))
+ (,s (list it))
+ (,h ,form)
+ (,b nil)
+ (,l (cdr ,l)))
+ (while ,l
+ (let* ((it (car ,l))
+ (,n ,form))
+ (if (equal ,h, n)
+ (when ,b
+ (!cons (nreverse ,s) ,r)
+ (setq ,s nil)
+ (setq ,b nil))
+ (setq ,b t))
+ (!cons it ,s)
+ (!cdr ,l)))
+ (!cons (nreverse ,s) ,r)
+ (nreverse ,r))))))
+
+(defun -partition-by-header (fn list)
+ "Applies FN to the first item in LIST. That is the header
+ value. Applies FN to each item in LIST, splitting it each time
+ FN returns the header value, but only after seeing at least one
+ other value (the body)."
+ (--partition-by-header (funcall fn it) list))
+
(defmacro --group-by (form list)
"Anaphoric form of `-group-by'."
(let ((l (make-symbol "list"))
diff --git a/dev/examples.el b/dev/examples.el
index 9dbb09a..2fb37d9 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -169,6 +169,11 @@
(-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 -partition-by-header
+ (--partition-by-header (= it 1) '(1 2 3 1 2 1 2 3 4)) => '((1 2 3) (1 2) (1
2 3 4))
+ (--partition-by-header (> it 0) '(1 2 0 1 0 1 2 3 0)) => '((1 2 0) (1 0) (1
2 3 0))
+ (-partition-by-header 'even? '(2 1 1 1 4 1 3 5 6 6 1)) => '((2 1 1 1) (4 1 3
5) (6 6 1)))
+
(defexamples -group-by
(-group-by 'even? '()) => '()
(-group-by 'even? '(1 1 2 2 2 3 4 6 8)) => '((nil . (1 1 3)) (t . (2 2 2 4 6
8)))
- [elpa] externals/dash 8fe15ed 141/426: Group -repeat with -dotimes, (continued)
- [elpa] externals/dash 8fe15ed 141/426: Group -repeat with -dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 98a82ca 142/426: DRY up -repeat with --dotimes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 50e9f46 144/426: -cons* for making improper lists., Phillip Lord, 2015/08/04
- [elpa] externals/dash e083087 145/426: Merge pull request #13 from nicferrier/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5161361 148/426: Clarify use of anaphoric form of -zip-with, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0731333 140/426: Add @vemv to list of contributors, Phillip Lord, 2015/08/04
- [elpa] externals/dash a7ee06e 149/426: Remove extra --separate definition, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3f604cd 151/426: Add -count, Phillip Lord, 2015/08/04
- [elpa] externals/dash fb75a7f 146/426: Add @nicferrier to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash cc33c2f 153/426: Stop integration tests for Emacs 23, Phillip Lord, 2015/08/04
- [elpa] externals/dash adfafcb 152/426: Add -partition-by-header,
Phillip Lord <=
- [elpa] externals/dash 0351ca3 156/426: Add Wilfred Hughes to list of contributors., Phillip Lord, 2015/08/04
- [elpa] externals/dash a4b70f9 154/426: [magnars/dash.el#18] implementing -slice, Phillip Lord, 2015/08/04
- [elpa] externals/dash bd942e1 158/426: inhibit dev from being added to the load-path, Phillip Lord, 2015/08/04
- [elpa] externals/dash c218e9f 150/426: -split-at/with now makes just one pass over list, Phillip Lord, 2015/08/04
- [elpa] externals/dash d7174ed 160/426: Handle cons-cells properly in -flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash a559a80 161/426: Added -last, Phillip Lord, 2015/08/04
- [elpa] externals/dash f7a828f 147/426: Add -zip and -zip-with, Phillip Lord, 2015/08/04
- [elpa] externals/dash dac28b0 155/426: Merge pull request #19 from Wilfred/master, Phillip Lord, 2015/08/04
- [elpa] externals/dash a40fc00 162/426: Merge pull request #23 from Fuco1/last, Phillip Lord, 2015/08/04
- [elpa] externals/dash a1b04d8 163/426: Less implementation specific docs for -mapcat, Phillip Lord, 2015/08/04