[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/dash 9410f7e 073/439: Add !replace-where
From: |
Phillip Lord |
Subject: |
[elpa] externals/dash 9410f7e 073/439: Add !replace-where |
Date: |
Tue, 04 Aug 2015 20:26:32 +0000 |
branch: externals/dash
commit 9410f7e5a60e4c1aea69e38d78a7ad5710b3707d
Author: Magnar Sveen <address@hidden>
Commit: Magnar Sveen <address@hidden>
Add !replace-where
---
README.md | 13 +++++++++++++
bang.el | 20 ++++++++++++++++++++
examples.el | 5 +++++
3 files changed, 38 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
index f488889..8cbfadd 100644
--- a/README.md
+++ b/README.md
@@ -21,6 +21,7 @@ Or you can just dump `bang.el` in your load path somewhere.
* [!concat](#concat-rest-lists) `(&rest lists)`
* [!mapcat](#mapcat-fn-list) `(fn list)`
* [!interpose](#interpose-sep-list) `(sep list)`
+* [!replace-where](#replace-where-pred-rep-list) `(pred rep list)`
* [!first](#first-fn-list) `(fn list)`
* [!partial](#partial-fn-rest-args) `(fn &rest args)`
* [!rpartial](#rpartial-fn-rest-args) `(fn &rest args)`
@@ -170,6 +171,18 @@ Returns a new list of all elements in `list` separated by
`sep`.
(!interpose "-" '("a" "b" "c")) ;; => '("a" "-" "b" "-" "c")
```
+### !replace-where `(pred rep list)`
+
+Returns a new list where the elements in `list` that does not match the `pred`
function
+are unchanged, and where the elements in `list` that do match the `pred`
function are mapped
+through the `rep` function.
+
+```cl
+(!replace-where 'even? 'square '(1 2 3 4)) ;; => '(1 4 3 16)
+(!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) ;; => '(1 2 0
4)
+(!!replace-where (> it 2) (* it it) '(1 2 3 4)) ;; => '(1 2 9 16)
+```
+
### !first `(fn list)`
Returns the first x in `list` where (`fn` x) is non-nil, else nil.
diff --git a/bang.el b/bang.el
index 78c21a5..065662d 100644
--- a/bang.el
+++ b/bang.el
@@ -155,6 +155,26 @@ Thus function FN should return a collection."
(setq list (cdr list)))
(nreverse result)))
+(defmacro !!replace-where (pred rep list)
+ "Returns a new list where the elements in LIST that does not match the PRED
function
+are unchanged, and where the elements in LIST that do match the PRED function
are mapped
+through the REP function."
+ (let ((l (make-symbol "list"))
+ (r (make-symbol "result")))
+ `(let ((,l ,list)
+ (,r '()))
+ (while ,l
+ (let ((it (car ,l)))
+ (setq ,r (cons (if ,pred ,rep it) ,r)))
+ (setq ,l (cdr ,l)))
+ (nreverse ,r))))
+
+(defun !replace-where (pred rep list)
+ "Returns a new list where the elements in LIST that does not match the PRED
function
+are unchanged, and where the elements in LIST that do match the PRED function
are mapped
+through the REP function."
+ (!!replace-where (funcall pred it) (funcall rep it) list))
+
(defun !partial (fn &rest args)
"Takes a function FN and fewer than the normal arguments to FN,
and returns a fn that takes a variable number of additional ARGS.
diff --git a/examples.el b/examples.el
index 970953d..6bfec6f 100644
--- a/examples.el
+++ b/examples.el
@@ -63,6 +63,11 @@
(!interpose "-" '("a")) => '("a")
(!interpose "-" '("a" "b" "c")) => '("a" "-" "b" "-" "c"))
+(defexamples !replace-where
+ (!replace-where 'even? 'square '(1 2 3 4)) => '(1 4 3 16)
+ (!replace-where (lambda (n) (= n 3)) (lambda (n) 0) '(1 2 3 4)) => '(1 2 0 4)
+ (!!replace-where (> it 2) (* it it) '(1 2 3 4)) => '(1 2 9 16))
+
(defexamples !first
(!first 'even? '(1 2 3)) => 2
(!first 'even? '(1 3 5)) => nil
- [elpa] externals/dash 047eeea 066/439: Run tests on Emacs 24., (continued)
- [elpa] externals/dash 047eeea 066/439: Run tests on Emacs 24., Phillip Lord, 2015/08/04
- [elpa] externals/dash 5bd4593 067/439: Add note about !rpartial only working on Emacs 24+, Phillip Lord, 2015/08/04
- [elpa] externals/dash 05dec7a 062/439: Fix example., Phillip Lord, 2015/08/04
- [elpa] externals/dash 4205e58 064/439: Add clojure threading macros, !-> and !->>, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6520496 060/439: First release, remove warning., Phillip Lord, 2015/08/04
- [elpa] externals/dash ca3eea7 070/439: Fix examples-to-docs to support documenting macros., Phillip Lord, 2015/08/04
- [elpa] externals/dash b29a5bb 071/439: Use make-symbol in macros to avoid names clashing., Phillip Lord, 2015/08/04
- [elpa] externals/dash 6f17346 068/439: Don't run !rpartial tests on Emacsen <24, Phillip Lord, 2015/08/04
- [elpa] externals/dash 66ffaa6 072/439: Add !interpose, Phillip Lord, 2015/08/04
- [elpa] externals/dash ec6a85f 074/439: Add threading macro !!-> with `it` as placeholder., Phillip Lord, 2015/08/04
- [elpa] externals/dash 9410f7e 073/439: Add !replace-where,
Phillip Lord <=
- [elpa] externals/dash cd2a793 069/439: Run travis-ci on both E23 and E24, Phillip Lord, 2015/08/04
- [elpa] externals/dash 4e76865 075/439: Add !take-while, Phillip Lord, 2015/08/04
- [elpa] externals/dash 6238f8f 077/439: Add !split-with, Phillip Lord, 2015/08/04
- [elpa] externals/dash b9b6330 079/439: Add !take, Phillip Lord, 2015/08/04
- [elpa] externals/dash 8c75026 080/439: Add !drop, Phillip Lord, 2015/08/04
- [elpa] externals/dash 123eaaa 078/439: Docs: Refer to !replace-where from !!replace-where., Phillip Lord, 2015/08/04
- [elpa] externals/dash ec835e4 081/439: Add !split-at, Phillip Lord, 2015/08/04
- [elpa] externals/dash 3802890 082/439: Simplify !concat, Phillip Lord, 2015/08/04
- [elpa] externals/dash fea1aa3 086/439: Add -interleave, Phillip Lord, 2015/08/04
- [elpa] externals/dash 0dab44c 084/439: Add -flatten, Phillip Lord, 2015/08/04