branch: externals/dash commit b540ebb9d6289a54188649818ffbab9bf4e859d6 Author: Per Weijnitz <per.weijn...@gmail.com> Commit: Basil L. Contovounesios <conto...@tcd.ie>
Refactor -partition-after-pred to avoid recursion * dash.el (--partition-after-pred): New anaphoric macro. (-partition-after-pred): Use it. * dev/examples.el (-partition-after-pred): Test it. --- dash.el | 33 +++++++++++++++++++++------------ dev/examples.el | 4 +++- 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/dash.el b/dash.el index c483a00..69c474b 100644 --- a/dash.el +++ b/dash.el @@ -1382,20 +1382,29 @@ 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) + "Anaphoric form of `-partition-after-pred'." + (let ((r (make-symbol "result")) + (s (make-symbol "sublist")) + (l (make-symbol "list"))) + `(let ((,l ,list)) + (when ,l + (let* ((,r nil) + (,s nil)) + (while ,l + (let* ((it (car ,l))) + (!cdr ,l) + (!cons it ,s) + (when ,form + (!cons (nreverse ,s) ,r) + (setq ,s nil)))) + (if ,s + (!cons (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-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/dev/examples.el b/dev/examples.el index 4382ba4..81d6da9 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -806,7 +806,9 @@ 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 (= 1 (% it 2)) '(0 0 0 1 0 1 1 0 1)) + => '((0 0 0 1) (0 1) (1) (0 1))) (defexamples -partition-before-pred (-partition-before-pred #'booleanp '()) => '()