[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 6bdcfa5 286/426: Add -split-on, -split-when
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 6bdcfa5 286/426: Add -split-on, -split-when |
Date: |
Tue, 04 Aug 2015 19:38:19 +0000 |
branch: externals/dash
commit 6bdcfa5dbe2935389c361c07d25b31c7d1906de3
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add -split-on, -split-when
---
README.md | 35 +++++++++++++++++++++++++++++++++++
dash.el | 38 ++++++++++++++++++++++++++++++++++++++
dev/examples.el | 19 +++++++++++++++++++
3 files changed, 92 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index f52d46d..7e1318b 100644
--- a/README.md
+++ b/README.md
@@ -85,6 +85,8 @@ Include this in your emacs settings to get syntax
highlighting:
* [-split-at](#-split-at-n-list) `(n list)`
* [-split-with](#-split-with-pred-list) `(pred list)`
+* [-split-on](#-split-on-item-list) `(item list)`
+* [-split-when](#-split-when-fn-list) `(fn list)`
* [-separate](#-separate-pred-list) `(pred list)`
* [-partition](#-partition-n-list) `(n list)`
* [-partition-all-in-steps](#-partition-all-in-steps-n-step-list) `(n step
list)`
@@ -657,6 +659,39 @@ Returns a list of ((-take-while `pred` `list`)
(-drop-while `pred` `list`)), in
(--split-with (< it 4) '(1 2 3 4 3 2 1)) ;; => '((1 2 3) (4 3 2 1))
```
+#### -split-on `(item list)`
+
+Split the `list` each time `item` is found.
+
+Unlike `-partition-by`, the `item` is discarded from the results.
+Empty lists are also removed from the result.
+
+Comparison is done by `equal`.
+
+See also `-split-when`.
+
+```cl
+(-split-on '| '(Nil | Leaf a | Node [Tree a])) ;; => '((Nil) (Leaf a) (Node
[Tree a]))
+(-split-on ':endgroup '("a" "b" :endgroup "c" :endgroup "d" "e")) ;; => '(("a"
"b") ("c") ("d" "e"))
+(-split-on ':endgroup '("a" "b" :endgroup :endgroup "d" "e")) ;; => '(("a"
"b") ("d" "e"))
+```
+
+#### -split-when `(fn list)`
+
+Split the `list` on each element where `fn` returns non-nil.
+
+Unlike `-partition-by`, the "matched" element is discarded from
+the results. Empty lists are also removed from the result.
+
+This function can be thought of as a generalization of
+`split-string`.
+
+```cl
+(-split-when 'even? '(1 2 3 4 5 6)) ;; => '((1) (3) (5))
+(-split-when 'even? '(1 2 3 4 6 8 9)) ;; => '((1) (3) (9))
+(--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest args)) ;;
=> '((a b) (c d) (args))
+```
+
#### -separate `(pred list)`
Returns a list of ((-filter `pred` `list`) (-remove `pred` `list`)), in one
pass through the list.
diff --git a/dash.el b/dash.el
index 4d4ef4e..5fcb15d 100644
--- a/dash.el
+++ b/dash.el
@@ -560,6 +560,41 @@ from INDICES."
"Returns a list of ((-take-while PRED LIST) (-drop-while PRED LIST)), in no
more than one pass through the list."
(--split-with (funcall pred it) list))
+(defmacro -split-on (item list)
+ "Split the LIST each time ITEM is found.
+
+Unlike `-partition-by', the ITEM is discarded from the results.
+Empty lists are also removed from the result.
+
+Comparison is done by `equal'.
+
+See also `-split-when'."
+ (declare (debug (form form)))
+ `(-split-when (lambda (it) (equal it ,item)) ,list))
+
+(defmacro --split-when (form list)
+ "Anaphoric version of `-split-when'."
+ (declare (debug (sexp form)))
+ `(-split-when (lambda (it) ,form) ,list))
+
+(defun -split-when (fn list)
+ "Split the LIST on each element where FN returns non-nil.
+
+Unlike `-partition-by', the \"matched\" element is discarded from
+the results. Empty lists are also removed from the result.
+
+This function can be thought of as a generalization of
+`split-string'."
+ (let (r s)
+ (while list
+ (if (not (funcall fn (car list)))
+ (push (car list) s)
+ (when s (push (nreverse s) r))
+ (setq s nil))
+ (!cdr list))
+ (when s (push (nreverse s) r))
+ (nreverse r)))
+
(defmacro --separate (form list)
"Anaphoric form of `-separate'."
(declare (debug (sexp form)))
@@ -1308,6 +1343,9 @@ structure such as plist or alist."
"-remove-at-indices"
"-split-with"
"--split-with"
+ "-split-on"
+ "-split-when"
+ "--split-when"
"-separate"
"--separate"
"-partition-all-in-steps"
diff --git a/dev/examples.el b/dev/examples.el
index c0ae5bf..e106733 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -245,6 +245,25 @@
(-split-with 'even? '(2 4 5 6)) => '((2 4) (5 6))
(--split-with (< it 4) '(1 2 3 4 3 2 1)) => '((1 2 3) (4 3 2 1)))
+ (defexamples -split-on
+ (-split-on '| '(Nil | Leaf a | Node [Tree a])) => '((Nil) (Leaf a) (Node
[Tree a]))
+ (-split-on ':endgroup '("a" "b" :endgroup "c" :endgroup "d" "e")) =>
'(("a" "b") ("c") ("d" "e"))
+ (-split-on ':endgroup '("a" "b" :endgroup :endgroup "d" "e")) => '(("a"
"b") ("d" "e"))
+ (-split-on ':endgroup '("a" "b" :endgroup "c" :endgroup)) => '(("a" "b")
("c"))
+ (-split-on ':endgroup '("a" "b" :endgroup :endgroup :endgroup "d" "e")) =>
'(("a" "b") ("d" "e"))
+ (-split-on ':endgroup '(:endgroup "c" :endgroup "d" "e")) => '(("c") ("d"
"e"))
+ (-split-on '| '(Nil | | Node [Tree a])) => '((Nil) (Node [Tree a])))
+
+ (defexamples -split-when
+ (-split-when 'even? '(1 2 3 4 5 6)) => '((1) (3) (5))
+ (-split-when 'even? '(1 2 3 4 6 8 9)) => '((1) (3) (9))
+ (--split-when (memq it '(&optional &rest)) '(a b &optional c d &rest
args)) => '((a b) (c d) (args))
+ (-split-when 'even? '(1 2 3 5 6)) => '((1) (3 5))
+ (-split-when 'even? '(1 2 3 5)) => '((1) (3 5))
+ (-split-when 'even? '(1 3 4 5 6)) => '((1 3) (5))
+ (-split-when 'even? '(1 2 3 4 5 6 8 10)) => '((1) (3) (5))
+ (-split-when 'even? '(1 2 3 5 7 6)) => '((1) (3 5 7)))
+
(defexamples -separate
(-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) => '((2 4 6)
(1 3 5 7))
(--separate (< it 5) '(3 7 5 9 3 2 1 4 6)) => '((3 3 2 1 4) (7 5 9 6))
- [elpa] externals/dash 6b64ea7 303/426: Add functions for flexibly zipping uneven lists, (continued)
- [elpa] externals/dash 6b64ea7 303/426: Add functions for flexibly zipping uneven lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash cd137e0 310/426: `-slice` should not fill the returned list with nils if to > length, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5219ac0 422/426: [-let] Eliminate useless re-binding of symbols when sources are immutable, Phillip Lord, 2015/08/04
- [elpa] externals/dash a06e66e 382/426: Add test for clean byte compilation, Phillip Lord, 2015/08/04
- [elpa] externals/dash 43dcd37 295/426: Only eval NUM in --dotimes once, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4ecfea6 296/426: Merge pull request #79 from Fuco1/debug-forms, Phillip Lord, 2015/08/04
- [elpa] externals/dash 624c501 306/426: Add `-table` and `-table-flat`, Phillip Lord, 2015/08/04
- [elpa] externals/dash cf7ca23 415/426: [Fix #97] Add -remove-item, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2eefbec 386/426: Don't use an inline comment on a line of its own, Phillip Lord, 2015/08/04
- [elpa] externals/dash bbc1d9c 309/426: `--each-while` should also expose `it-index`, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6bdcfa5 286/426: Add -split-on, -split-when,
Phillip Lord <=
- [elpa] externals/dash bd85b7c 302/426: Make -zip support infinite (circular) lists, Phillip Lord, 2015/08/04
- [elpa] externals/dash 19fbc24 426/426: Fix anaphoric -spice-list macro, Phillip Lord, 2015/08/04
- [elpa] externals/dash 36b7f49 419/426: [-let] Reuse the ignore-place predicate, Phillip Lord, 2015/08/04
- [elpa] externals/dash 703bb59 297/426: Convert -zip to support variable-length arguments, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3132ae0 314/426: Alias -tail to nthcdr, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4098ea0 285/426: Merge pull request #74 from YoungFrog/dashfontifix, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0c045f2 369/426: Highlight with el instead of cl, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5086d24 293/426: Merge pull request #60 from rejeep/cask, Phillip Lord, 2015/08/04
- [elpa] externals/dash 1fde888 407/426: Merge pull request #125 from wasamasa/feature-some, Phillip Lord, 2015/08/04
- [elpa] externals/dash 51a07b1 408/426: Merge pull request #127 from occidens/fixfn, Phillip Lord, 2015/08/04