Add -interleave

master
Magnar Sveen 14 years ago
parent 9ff9fd06da
commit fea1aa363a
  1. 11
      README.md
  2. 8
      dash.el
  3. 6
      examples.el

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

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

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

Loading…
Cancel
Save