diff --git a/README.md b/README.md index d8081dc..f488889 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/bang.el b/bang.el index 128aae5..78c21a5 100644 --- a/bang.el +++ b/bang.el @@ -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. diff --git a/examples.el b/examples.el index 524fe67..970953d 100644 --- a/examples.el +++ b/examples.el @@ -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