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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash 13a4718 059/316: Add new partition-before, -after


From: ELPA Syncer
Subject: [elpa] externals/dash 13a4718 059/316: Add new partition-before, -after methods.
Date: Mon, 15 Feb 2021 15:57:25 -0500 (EST)

branch: externals/dash
commit 13a4718b3d207976a21df08008e7b78000218385
Author: Zachary Kanfer <zkanfer@gmail.com>
Commit: Zachary Kanfer <zkanfer@gmail.com>

    Add new partition-before, -after methods.
    
    The new methods are -partition-before-pred, -partition-after-pred,
    -partition-before-item, and -partition-after-item.
---
 dash.el         | 30 ++++++++++++++++++++++++++++++
 dev/examples.el | 31 +++++++++++++++++++++++++++++++
 2 files changed, 61 insertions(+)

diff --git a/dash.el b/dash.el
index 10b1f05..2e34cbe 100644
--- a/dash.el
+++ b/dash.el
@@ -1032,6 +1032,36 @@ returns the header value, but only after seeing at least 
one
 other value (the body)."
   (--partition-by-header (funcall fn it) list))
 
+(defun -partition-after-pred (pred list)
+  "Partition directly after each time PRED is true on an element of LIST."
+  (when list
+    (let ((rest (-partition-after-pred pred
+                                       (cdr list))))
+      (if (funcall pred (car list))
+          ;;split after (car list)
+          (cons (list (car list))
+                rest)
+
+        ;;don't split after (car list)
+        (cons (cons (car list)
+                    (car rest))
+              (cdr rest))))))
+
+(defun -partition-before-pred (pred list)
+  "Partition directly before each time PRED is true on an element of LIST."
+  (nreverse (-map #'reverse
+                  (-partition-after-pred pred (reverse list)))))
+
+(defun -partition-after-item (item list)
+  "Partition directly after each time ITEM appears in LIST."
+  (-partition-after-pred (lambda (ele) (equal ele item))
+                         list))
+
+(defun -partition-before-item (item list)
+  "Partition directly before each time ITEM appears in LIST."
+  (-partition-before-pred (lambda (ele) (equal ele item))
+                          list))
+
 (defmacro --group-by (form list)
   "Anaphoric form of `-group-by'."
   (declare (debug t))
diff --git a/dev/examples.el b/dev/examples.el
index 7487292..aaa0bed 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -506,6 +506,37 @@ new list."
     (--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 -partition-after-pred
+    (-partition-after-pred #'oddp '()) => '()
+    (-partition-after-pred #'oddp '(1)) => '((1))
+    (-partition-after-pred #'oddp '(0 1)) => '((0 1))
+    (-partition-after-pred #'oddp '(1 1)) => '((1) (1))
+    (-partition-after-pred #'oddp '(0 0 0 1 0 1 1 0 1)) => '((0 0 0 1) (0 1) 
(1) (0 1)))
+
+  (defexamples -partition-before-pred
+    (-partition-before-pred #'oddp '()) => '()
+    (-partition-before-pred #'oddp '(1)) => '((1))
+    (-partition-before-pred #'oddp '(0 1)) => '((0) (1))
+    (-partition-before-pred #'oddp '(1 1)) => '((1) (1))
+    (-partition-before-pred #'oddp '(0 1 0)) => '((0) (1 0))
+    (-partition-before-pred #'oddp '(0 0 0 1 0 1 1 0 1)) => '((0 0 0) (1 0) 
(1) (1 0) (1)))
+
+  (defexamples -partition-before-item
+    (-partition-before-item 3 '()) => '()
+    (-partition-before-item 3 '(1)) => '((1))
+    (-partition-before-item 3 '(3)) => '((3))
+    (-partition-before-item 3 '(1 3)) => '((1) (3))
+    (-partition-before-item 3 '(1 3 4)) => '((1) (3 4))
+    (-partition-before-item 3 '(1 2 3 2 3 3 4)) => '((1 2) (3 2) (3) (3 4)))
+
+  (defexamples -partition-after-item
+    (-partition-after-item 3 '()) => '()
+    (-partition-after-item 3 '(1)) => '((1))
+    (-partition-after-item 3 '(3)) => '((3))
+    (-partition-after-item 3 '(3 1)) => '((3) (1))
+    (-partition-after-item 3 '(3 1 3)) => '((3) (1 3))
+    (-partition-after-item 3 '(3 2 3 3 4 5 3 2)) => '((3) (2 3) (3) (4 5 3) 
(2)))
+
   (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]