branch: externals/dash commit aab346ed9d8f0f7ea033029c9688810353052e7e Merge: 28103ae 609b7e7 Author: Basil L. Contovounesios <conto...@tcd.ie> Commit: Basil L. Contovounesios <conto...@tcd.ie>
Merge PR#362 from perweij/refactor/avoid-recursion Closes #379. --- README.md | 4 +++- dash.el | 37 ++++++++++++++++++++++++------------- dash.texi | 4 +++- dev/examples.el | 20 +++++++++++++++++++- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 40d1a4e..0d9f7e3 100644 --- a/README.md +++ b/README.md @@ -1540,7 +1540,9 @@ other value (the body). #### -partition-after-pred `(pred list)` -Partition directly after each time `pred` is true on an element of `list`. +Partition `list` after each element for which `pred` returns non-nil. + +This function's anaphoric counterpart is `--partition-after-pred`. ```el (-partition-after-pred #'booleanp ()) ;; => () diff --git a/dash.el b/dash.el index c483a00..3b5dd5b 100644 --- a/dash.el +++ b/dash.el @@ -1382,20 +1382,31 @@ returns the header value, but only after seeing at least one other value (the body)." (--partition-by-header (funcall fn it) list)) +(defmacro --partition-after-pred (form list) + "Partition LIST after each element for which FORM evaluates to non-nil. +Each element of LIST in turn is bound to `it' before evaluating +FORM. + +This is the anaphoric counterpart to `-partition-after-pred'." + (let ((l (make-symbol "list")) + (r (make-symbol "result")) + (s (make-symbol "sublist"))) + `(let ((,l ,list) ,r ,s) + (when ,l + (--each ,l + (push it ,s) + (when ,form + (push (nreverse ,s) ,r) + (setq ,s ()))) + (when ,s + (push (nreverse ,s) ,r)) + (nreverse ,r))))) + (defun -partition-after-pred (pred list) - "Partition directly after each time PRED is true on an element of LIST." - (when list - (let ((rest (-partition-after-pred pred - (cdr list)))) - (if (funcall pred (car list)) - ;;split after (car list) - (cons (list (car list)) - rest) - - ;;don't split after (car list) - (cons (cons (car list) - (car rest)) - (cdr rest)))))) + "Partition LIST after each element for which PRED returns non-nil. + +This function's anaphoric counterpart is `--partition-after-pred'." + (--partition-after-pred (funcall pred it) list)) (defun -partition-before-pred (pred list) "Partition directly before each time PRED is true on an element of LIST." diff --git a/dash.texi b/dash.texi index 60aabb4..70e782a 100644 --- a/dash.texi +++ b/dash.texi @@ -2197,7 +2197,9 @@ other value (the body). @anchor{-partition-after-pred} @defun -partition-after-pred (pred list) -Partition directly after each time @var{pred} is true on an element of @var{list}. +Partition @var{list} after each element for which @var{pred} returns non-nil. + +This function's anaphoric counterpart is @code{--partition-after-pred}. @example @group diff --git a/dev/examples.el b/dev/examples.el index 4382ba4..df05d34 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -806,7 +806,25 @@ value rather than consuming a list to produce a single value." (-partition-after-pred #'booleanp '(t t)) => '((t) (t)) (-partition-after-pred #'booleanp '(0 0 t t 0 t)) => '((0 0 t) (t) (0 t)) (-partition-after-pred #'booleanp '(t)) => '((t)) - (-partition-after-pred #'booleanp '(0 t)) => '((0 t))) + (-partition-after-pred #'booleanp '(0 t)) => '((0 t)) + (--partition-after-pred (= (% it 2) 0) '()) => '() + (--partition-after-pred (= (% it 2) 1) '()) => '() + (--partition-after-pred (= (% it 2) 0) '(0)) => '((0)) + (--partition-after-pred (= (% it 2) 1) '(0)) => '((0)) + (--partition-after-pred (= (% it 2) 0) '(0 1)) => '((0) (1)) + (--partition-after-pred (= (% it 2) 1) '(0 1)) => '((0 1)) + (--partition-after-pred (= (% it 2) 0) '(0 1 2)) => '((0) (1 2)) + (--partition-after-pred (= (% it 2) 1) '(0 1 2)) => '((0 1) (2)) + (--partition-after-pred (= (% it 2) 0) '(0 1 2 3)) => '((0) (1 2) (3)) + (--partition-after-pred (= (% it 2) 1) '(0 1 2 3)) => '((0 1) (2 3)) + (--partition-after-pred t '()) => () + (--partition-after-pred t '(0)) => '((0)) + (--partition-after-pred t '(0 1)) => '((0) (1)) + (--partition-after-pred t '(0 1 2)) => '((0) (1) (2)) + (--partition-after-pred nil '()) => '() + (--partition-after-pred nil '(0)) => '((0)) + (--partition-after-pred nil '(0 1)) => '((0 1)) + (--partition-after-pred nil '(0 1 2)) => '((0 1 2))) (defexamples -partition-before-pred (-partition-before-pred #'booleanp '()) => '()