Add !interpose

master
Magnar Sveen 14 years ago
parent b29a5bba47
commit 66ffaa668d
  1. 11
      README.md
  2. 11
      bang.el
  3. 5
      examples.el

@ -20,6 +20,7 @@ Or you can just dump `bang.el` in your load path somewhere.
* [!keep](#keep-fn-list) `(fn list)`
* [!concat](#concat-rest-lists) `(&rest lists)`
* [!mapcat](#mapcat-fn-list) `(fn list)`
* [!interpose](#interpose-sep-list) `(sep list)`
* [!first](#first-fn-list) `(fn list)`
* [!partial](#partial-fn-rest-args) `(fn &rest args)`
* [!rpartial](#rpartial-fn-rest-args) `(fn &rest args)`
@ -159,6 +160,16 @@ Thus function `fn` should return a collection.
(!!mapcat (list 0 it) '(1 2 3)) ;; => '(0 1 0 2 0 3)
```
### !interpose `(sep list)`
Returns a new list of all elements in `list` separated by `sep`.
```cl
(!interpose "-" '()) ;; => '()
(!interpose "-" '("a")) ;; => '("a")
(!interpose "-" '("a" "b" "c")) ;; => '("a" "-" "b" "-" "c")
```
### !first `(fn list)`
Returns the first x in `list` where (`fn` x) is non-nil, else nil.

@ -144,6 +144,17 @@ the supplied LISTS."
Thus function FN should return a collection."
(!!mapcat (funcall fn it) list))
(defun !interpose (sep list)
"Returns a new list of all elements in LIST separated by SEP."
(let (result)
(when list
(setq result (cons (car list) result))
(setq list (cdr list)))
(while list
(setq result (cons (car list) (cons sep result)))
(setq list (cdr list)))
(nreverse result)))
(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.

@ -58,6 +58,11 @@
(!mapcat (lambda (item) (list 0 item)) '(1 2 3)) => '(0 1 0 2 0 3)
(!!mapcat (list 0 it) '(1 2 3)) => '(0 1 0 2 0 3))
(defexamples !interpose
(!interpose "-" '()) => '()
(!interpose "-" '("a")) => '("a")
(!interpose "-" '("a" "b" "c")) => '("a" "-" "b" "-" "c"))
(defexamples !first
(!first 'even? '(1 2 3)) => 2
(!first 'even? '(1 3 5)) => nil

Loading…
Cancel
Save