Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add -separate-multi and --separate-multi. #299

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -183,6 +183,7 @@ Functions partitioning the input list into a list of lists.
* [-split-on](#-split-on-item-list) `(item list)`
* [-split-when](#-split-when-fn-list) `(fn list)`
* [-separate](#-separate-pred-list) `(pred list)`
* [-separate-multi](#-separate-multi-preds-list) `(pred list)`
* [-partition](#-partition-n-list) `(n list)`
* [-partition-all](#-partition-all-n-list) `(n list)`
* [-partition-in-steps](#-partition-in-steps-n-step-list) `(n step list)`
Expand Down Expand Up @@ -1343,6 +1344,16 @@ Return a list of ((-filter `pred` `list`) (-remove `pred` `list`)), in one pass
(-separate 'cdr '((1 2) (1) (1 2 3) (4))) ;; => '(((1 2) (1 2 3)) ((1) (4)))
```

### -separate-multi `(preds list)`

Return a list of ((-filter PRED1 LIST) (-filter PRED2 LIST) ... REST), in one pass through the list.

```el
(-separate-multi '(stringp floatp) '(:hi "ho" "ho" :he 2.4)) ;; => '(("ho" "ho") (2.4) (:hi :he))
(--separate-multi ((stringp it) (floatp it)) '(:hi "ho" "ho" :he 2.4)) ;; => '(("ho" "ho") (2.4) (:hi :he))
(-separate-multi '(stringp floatp keywordp) '(:hi "ho" "ho" :he 2.4)) ;; => (("ho" "ho") (2.4) (:hi :he) nil)
```

#### -partition `(n list)`

Return a new list with the items in `list` grouped into `n-`sized sublists.
Expand Down
18 changes: 18 additions & 0 deletions dash.el
Expand Up @@ -1048,6 +1048,24 @@ This function can be thought of as a generalization of
"Return a list of ((-filter PRED LIST) (-remove PRED LIST)), in one pass through the list."
(--separate (funcall pred it) list))

(defun -separate-multi (preds list)
"Return a list of ((-filter PRED1 LIST) (-filter PRED2 LIST) ... REST).

PREDS is a list of functions. REST is a list of elements that are unmatched by
PREDS.

This is done in one pass through the list."
(setq preds (mapcar #'list (-snoc preds (lambda (_) t))))
(dolist (elem list)
(--first (when (funcall (car it) elem)
(push elem (cdr it)))
preds))
(--map (nreverse (cdr it)) preds))

(defmacro --separate-multi (forms list)
"Anaphoric form of `-separate-multi.'"
`(-separate-multi '(,@(--map `(lambda (it) ,it) forms)) ,list))

(defun ---partition-all-in-steps-reversed (n step list)
"Private: Used by -partition-all-in-steps and -partition-in-steps."
(when (< step 1)
Expand Down