Add !replace-where

master
Magnar Sveen 14 years ago
parent 66ffaa668d
commit 9410f7e5a6
  1. 13
      README.md
  2. 20
      bang.el
  3. 5
      examples.el

@ -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.

@ -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.

@ -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

Loading…
Cancel
Save