Add -separate (thanks @Fuco1)

master
Magnar Sveen 14 years ago
parent cca9b1b741
commit c025efb9fc
  1. 11
      README.md
  2. 12
      dash.el
  3. 5
      examples.el

@ -35,6 +35,7 @@ Or you can just dump `dash.el` in your load path somewhere.
* [-drop-while](#-drop-while-pred-list) `(pred list)`
* [-split-at](#-split-at-n-list) `(n list)`
* [-split-with](#-split-with-pred-list) `(pred list)`
* [-separate](#-separate-pred-list) `(pred list)`
* [-partition](#-partition-n-list) `(n list)`
* [-partition-all](#-partition-all-n-list) `(n list)`
* [-partition-by](#-partition-by-fn-list) `(fn list)`
@ -331,6 +332,16 @@ Returns a list of ((-take-while `pred` `list`) (-drop-while `pred` `list`))
(--split-with (< it 4) '(1 2 3 4 3 2 1)) ;; => '((1 2 3) (4 3 2 1))
```
### -separate `(pred list)`
Returns a list of ((-filter `pred` `list`) (-remove `pred` `list`)).
```cl
(-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) ;; => '((2 4 6) (1 3 5 7))
(--separate (< it 5) '(3 7 5 9 3 2 1 4 6)) ;; => '((3 3 2 1 4) (7 5 9 6))
(-separate 'cdr '((1 2) (1) (1 2 3) (4))) ;; => '(((1 2) (1 2 3)) ((1) (4)))
```
### -partition `(n list)`
Returns a new list with the items in `list` grouped into `n-`sized sublists.

@ -335,6 +335,18 @@ Returns `nil` both if all items match the predicate, and if none of the items ma
"Returns a list of ((-take-while PRED LIST) (-drop-while PRED LIST))"
(--split-with (funcall pred it) list))
(defmacro --separate (form list)
"Anaphoric form of `-separate'."
(let ((y (make-symbol "yes"))
(n (make-symbol "no")))
`(let (,y ,n)
(--each ,list (if ,form (!cons it ,y) (!cons it ,n)))
(list (nreverse ,y) (nreverse ,n)))))
(defun -separate (pred list)
"Returns a list of ((-filter PRED LIST) (-remove PRED LIST))."
(--separate (funcall pred it) list))
(defun -partition (n list)
"Returns a new list with the items in LIST grouped into N-sized sublists.
If there are not enough items to make the last group N-sized,

@ -130,6 +130,11 @@
(-split-with 'even? '(2 4 5 6)) => '((2 4) (5 6))
(--split-with (< it 4) '(1 2 3 4 3 2 1)) => '((1 2 3) (4 3 2 1)))
(defexamples -separate
(-separate (lambda (num) (= 0 (% num 2))) '(1 2 3 4 5 6 7)) => '((2 4 6) (1 3 5 7))
(--separate (< it 5) '(3 7 5 9 3 2 1 4 6)) => '((3 3 2 1 4) (7 5 9 6))
(-separate 'cdr '((1 2) (1) (1 2 3) (4))) => '(((1 2) (1 2 3)) ((1) (4))))
(defexamples -partition
(-partition 2 '(1 2 3 4 5 6)) => '((1 2) (3 4) (5 6))
(-partition 2 '(1 2 3 4 5 6 7)) => '((1 2) (3 4) (5 6))

Loading…
Cancel
Save