[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 174747e 323/426: Add -splice and -splice-list
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 174747e 323/426: Add -splice and -splice-list |
Date: |
Tue, 04 Aug 2015 19:38:36 +0000 |
branch: externals/dash
commit 174747ea978b49bd3791b67edc1fd09455c32c11
Author: Matus Goljer <address@hidden>
Commit: Matus Goljer <address@hidden>
Add -splice and -splice-list
---
README.md | 29 +++++++++++++++++++++++++++++
dash.el | 34 ++++++++++++++++++++++++++++++++++
dev/examples.el | 9 +++++++++
3 files changed, 72 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index 534edaa..fe383ae 100644
--- a/README.md
+++ b/README.md
@@ -44,6 +44,8 @@ Include this in your emacs settings to get syntax
highlighting:
* [-map-when](#-map-when-pred-rep-list) `(pred rep list)`
* [-replace](#-replace-old-new-list) `(old new list)`
* [-map-indexed](#-map-indexed-fn-list) `(fn list)`
+* [-splice](#-splice-pred-fun-list) `(pred fun list)`
+* [-splice-list](#-splice-list-pred-new-list-list) `(pred new-list list)`
* [-flatten](#-flatten-l) `(l)`
* [-flatten-n](#-flatten-n-num-list) `(num list)`
* [-concat](#-concat-rest-lists) `(&rest lists)`
@@ -309,6 +311,33 @@ In the anaphoric form `--map-indexed`, the index is
exposed as `it-index`.
(--map-indexed (- it it-index) '(1 2 3 4)) ;; => '(1 1 1 1)
```
+#### -splice `(pred fun list)`
+
+Splice lists generated by `fun` in place of elements matching `pred` in `list`.
+
+`fun` takes the element matching `pred` as input.
+
+This function can be used as replacement for `,@` in case you
+need to splice several lists at marked positions (for example
+with keywords).
+
+```cl
+(-splice 'even? (lambda (x) (list x x)) '(1 2 3 4)) ;; => '(1 2 2 3 4 4)
+(--splice 't (list it it) '(1 2 3 4)) ;; => '(1 1 2 2 3 3 4 4)
+(--splice (equal it :magic) '((list of) (magical) (code)) '((foo) (bar) :magic
(baz))) ;; => '((foo) (bar) (list of) (magical) (code) (baz))
+```
+
+#### -splice-list `(pred new-list list)`
+
+Splice `new-list` in place of elements matching `pred` in `list`.
+
+See also more general version: `-splice`.
+
+```cl
+(-splice-list 'keywordp '(a b c) '(1 :foo 2)) ;; => '(1 a b c 2)
+(-splice-list 'keywordp nil '(1 :foo 2)) ;; => '(1 2)
+```
+
#### -flatten `(l)`
Takes a nested list `l` and returns its contents as a single, flat list.
diff --git a/dash.el b/dash.el
index d987ef8..775e799 100644
--- a/dash.el
+++ b/dash.el
@@ -301,6 +301,36 @@ Elements are compared using `equal'."
Thus function FN should return a list."
(--mapcat (funcall fn it) list))
+(defun -splice (pred fun list)
+ "Splice lists generated by FUN in place of elements matching PRED in LIST.
+
+FUN takes the element matching PRED as input.
+
+This function can be used as replacement for `,@' in case you
+need to splice several lists at marked positions (for example
+with keywords)."
+ (let (r)
+ (--each list
+ (if (funcall pred it)
+ (let ((new (funcall fun it)))
+ (--each new (!cons it r)))
+ (!cons it r)))
+ (nreverse r)))
+
+(defmacro --splice (pred form list)
+ "Anaphoric form of `-splice'."
+ `(-splice (lambda (it) ,pred) (lambda (it) ,form) ,list))
+
+(defun -splice-list (pred new-list list)
+ "Splice NEW-LIST in place of elements matching PRED in LIST.
+
+See also more general version: `-splice'."
+ (-splice pred (lambda (_) new-list) list))
+
+(defun --splice-list (pred new-list list)
+ "Anaphoric form of `-splice-list'."
+ `(-splice-list (lambda (it) ,pred) ,new-list ,list))
+
(defun -cons* (&rest args)
"Makes a new list from the elements of ARGS.
@@ -1471,6 +1501,10 @@ structure such as plist or alist."
"--keep"
"-map-indexed"
"--map-indexed"
+ "-splice"
+ "--splice"
+ "-splice-list"
+ "--splice-list"
"-map-when"
"--map-when"
"-replace-where"
diff --git a/dev/examples.el b/dev/examples.el
index 2f71f99..23697c2 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -48,6 +48,15 @@
(-map-indexed (lambda (index item) (- item index)) '(1 2 3 4)) => '(1 1 1
1)
(--map-indexed (- it it-index) '(1 2 3 4)) => '(1 1 1 1))
+ (defexamples -splice
+ (-splice 'even? (lambda (x) (list x x)) '(1 2 3 4)) => '(1 2 2 3 4 4)
+ (--splice 't (list it it) '(1 2 3 4)) => '(1 1 2 2 3 3 4 4)
+ (--splice (equal it :magic) '((list of) (magical) (code)) '((foo) (bar)
:magic (baz))) => '((foo) (bar) (list of) (magical) (code) (baz)))
+
+ (defexamples -splice-list
+ (-splice-list 'keywordp '(a b c) '(1 :foo 2)) => '(1 a b c 2)
+ (-splice-list 'keywordp nil '(1 :foo 2)) => '(1 2))
+
(defexamples -flatten
(-flatten '((1))) => '(1)
(-flatten '((1 (2 3) (((4 (5))))))) => '(1 2 3 4 5)
- [elpa] externals/dash a09a4b2 318/426: Add code markup for docstrings and fix erroneous whitespace in docstring, (continued)
- [elpa] externals/dash a09a4b2 318/426: Add code markup for docstrings and fix erroneous whitespace in docstring, Phillip Lord, 2015/08/04
- [elpa] externals/dash 38307e3 223/426: Partition docs, Phillip Lord, 2015/08/04
- [elpa] externals/dash f3b0a55 321/426: Change -predicate-p examples to -predicate? to maintain consistency, Phillip Lord, 2015/08/04
- [elpa] externals/dash 7e4adb5 319/426: Add missing keywords to highlight list, Phillip Lord, 2015/08/04
- [elpa] externals/dash 2ecc073 316/426: Reorder the partition examples to follow more logical order, add tests, Phillip Lord, 2015/08/04
- [elpa] externals/dash eea928a 320/426: Add -replace, Phillip Lord, 2015/08/04
- [elpa] externals/dash d9879e9 329/426: Release 2.8.0, Phillip Lord, 2015/08/04
- [elpa] externals/dash f8873a0 327/426: Update readme-template with changes from last release, Phillip Lord, 2015/08/04
- [elpa] externals/dash 25c114c 330/426: Implementing a shallow copy counterpart to -clone., Phillip Lord, 2015/08/04
- [elpa] externals/dash f257fb9 328/426: Add -butlast, Phillip Lord, 2015/08/04
- [elpa] externals/dash 174747e 323/426: Add -splice and -splice-list,
Phillip Lord <=
- [elpa] externals/dash ab99be6 335/426: Add -fix, Phillip Lord, 2015/08/04
- [elpa] externals/dash b1e585d 337/426: Update copyright notice, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3adad97 334/426: Add -non-nil, Phillip Lord, 2015/08/04
- [elpa] externals/dash 166bccd 333/426: Add -tree-map-nodes, Phillip Lord, 2015/08/04
- [elpa] externals/dash 05fa92f 342/426: [-let] Transpose nreverse/flatten, Phillip Lord, 2015/08/04
- [elpa] externals/dash f0831d3 339/426: Add -lambda, Phillip Lord, 2015/08/04
- [elpa] externals/dash 9ebcce6 325/426: Add some niceties to font-locking of examples, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6fc652d 331/426: Merge pull request #95 from Wilfred/shallow-copy-function, Phillip Lord, 2015/08/04
- [elpa] externals/dash 5637bd6 322/426: Add alias from -find to -first, Phillip Lord, 2015/08/04
- [elpa] externals/dash a4be872 338/426: Add `-let` and `-let*`, Phillip Lord, 2015/08/04