diff --git a/README.md b/README.md index 6e55e09..bd343dd 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Or you can just dump `dash.el` in your load path somewhere. * [-split-at](#split-at-n-list) `(n list)` * [-split-with](#split-with-fn-list) `(fn list)` * [-interpose](#interpose-sep-list) `(sep list)` +* [-interleave](#interleave-rest-lists) `(&rest lists)` * [-replace-where](#replace-where-pred-rep-list) `(pred rep list)` * [-first](#first-fn-list) `(fn list)` * [-difference](#difference-list-list2) `(list list2)` @@ -244,6 +245,16 @@ Returns a new list of all elements in `list` separated by `sep`. (-interpose "-" '("a" "b" "c")) ;; => '("a" "-" "b" "-" "c") ``` +### -interleave `(&rest lists)` + +Returns a new list of the first item in each list, then the second etc. + +```cl +(-interleave '(1 2) '("a" "b")) ;; => '(1 "a" 2 "b") +(-interleave '(1 2) '("a" "b") '("A" "B")) ;; => '(1 "a" "A" 2 "b" "B") +(-interleave '(1 2 3) '("a" "b")) ;; => '(1 "a" 2 "b") +``` + ### -replace-where `(pred rep list)` Returns a new list where the elements in `list` that does not match the `pred` function diff --git a/dash.el b/dash.el index 6e3d266..c5deb09 100644 --- a/dash.el +++ b/dash.el @@ -217,6 +217,14 @@ Thus function FN should return a collection." (setq list (cdr list))) (nreverse result))) +(defun -interleave (&rest lists) + "Returns a new list of the first item in each list, then the second etc." + (let (result) + (while (--all? (not (null it)) lists) + (--each lists (setq result (cons (car it) result))) + (setq lists (-map 'cdr lists))) + (nreverse result))) + (defmacro --replace-where (pred rep list) "Anaphoric form of `-replace-where'." (let ((l (make-symbol "list")) diff --git a/examples.el b/examples.el index 438f82b..4f1acf2 100644 --- a/examples.el +++ b/examples.el @@ -94,6 +94,12 @@ (-interpose "-" '("a")) => '("a") (-interpose "-" '("a" "b" "c")) => '("a" "-" "b" "-" "c")) +(defexamples -interleave + (-interleave '(1 2) '("a" "b")) => '(1 "a" 2 "b") + (-interleave '(1 2) '("a" "b") '("A" "B")) => '(1 "a" "A" 2 "b" "B") + (-interleave '(1 2 3) '("a" "b")) => '(1 "a" 2 "b") + (-interleave '(1 2 3) '("a" "b" "c" "d")) => '(1 "a" 2 "b" 3 "c")) + (defexamples -replace-where (-replace-where 'even? 'square '(1 2 3 4)) => '(1 4 3 16) (--replace-where (> it 2) (* it it) '(1 2 3 4)) => '(1 2 9 16)