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

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



reply via email to

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