diff --git a/README.md b/README.md index 242b532..0b613f3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/dash.el b/dash.el index 7933f53..3b870f4 100644 --- a/dash.el +++ b/dash.el @@ -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, diff --git a/examples.el b/examples.el index bb07fa0..8bc0a37 100644 --- a/examples.el +++ b/examples.el @@ -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))