emacs-elpa-diffs
[Top][All Lists]
Advanced

[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)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]