branch: externals/dash commit eb1231eca425b2ee6cb41ae757fccf0ef9185d51 Author: Matus Goljer <matus.gol...@gmail.com> Commit: Matus Goljer <matus.gol...@gmail.com>
Add -reductions[-r][-from] --- dash.el | 36 ++++++++++++++++++++++++++++++++++++ dev/examples.el | 20 ++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/dash.el b/dash.el index 7e0c75d..f40dad1 100644 --- a/dash.el +++ b/dash.el @@ -235,6 +235,38 @@ See also: `-reduce-r-from', `-reduce'" (declare (debug (form form))) `(-reduce-r (lambda (&optional it acc) ,form) ,list)) +(defun -reductions-from (fn init list) + "Return a list of the intermediate values of the reduction. + +See `-reduce-from' for explanation of the arguments. + +See also: `-reductions', `-reductions-r', `-reduce-r'" + (nreverse (--reduce-from (cons (funcall fn (car acc) it) acc) (list init) list))) + +(defun -reductions (fn list) + "Return a list of the intermediate values of the reduction. + +See `-reduce' for explanation of the arguments. + +See also: `-reductions-from', `-reductions-r', `-reduce-r'" + (-reductions-from fn (car list) (cdr list))) + +(defun -reductions-r-from (fn init list) + "Return a list of the intermediate values of the reduction. + +See `-reduce-r-from' for explanation of the arguments. + +See also: `-reductions-r', `-reductions', `-reduce'" + (--reduce-r-from (cons (funcall fn it (car acc)) acc) (list init) list)) + +(defun -reductions-r (fn list) + "Return a list of the intermediate values of the reduction. + +See `-reduce-r' for explanation of the arguments. + +See also: `-reductions-r-from', `-reductions', `-reduce'" + (-reductions-r-from fn (-last-item list) (-butlast list))) + (defmacro --filter (form list) "Anaphoric form of `-filter'. @@ -2483,6 +2515,10 @@ structure such as plist or alist." "--reduce-r-from" "-reduce-r" "--reduce-r" + "-reductions-from" + "-reductions-r-from" + "-reductions" + "-reductions-r" "-filter" "--filter" "-select" diff --git a/dev/examples.el b/dev/examples.el index a560ec3..d629cba 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -333,6 +333,26 @@ new list." (-reduce-r '+ '(1)) => 1 (--reduce-r (format "%s-%s" it acc) '()) => "nil-nil") + (defexamples -reductions-from + (-reductions-from (lambda (a i) (format "(%s FN %s)" a i)) "INIT" '(1 2 3 4)) => '("INIT" "(INIT FN 1)" "((INIT FN 1) FN 2)" "(((INIT FN 1) FN 2) FN 3)" "((((INIT FN 1) FN 2) FN 3) FN 4)") + (-reductions-from 'max 0 '(2 1 4 3)) => '(0 2 2 4 4) + (-reductions-from '* 1 '(1 2 3 4)) => '(1 1 2 6 24)) + + (defexamples -reductions-r-from + (-reductions-r-from (lambda (i a) (format "(%s FN %s)" i a)) "INIT" '(1 2 3 4)) => '("(1 FN (2 FN (3 FN (4 FN INIT))))" "(2 FN (3 FN (4 FN INIT)))" "(3 FN (4 FN INIT))" "(4 FN INIT)" "INIT") + (-reductions-r-from 'max 0 '(2 1 4 3)) => '(4 4 4 3 0) + (-reductions-r-from '* 1 '(1 2 3 4)) => '(24 24 12 4 1)) + + (defexamples -reductions + (-reductions (lambda (a i) (format "(%s FN %s)" a i)) '(1 2 3 4)) => '(1 "(1 FN 2)" "((1 FN 2) FN 3)" "(((1 FN 2) FN 3) FN 4)") + (-reductions '+ '(1 2 3 4)) => '(1 3 6 10) + (-reductions '* '(1 2 3 4)) => '(1 2 6 24)) + + (defexamples -reductions-r + (-reductions-r (lambda (i a) (format "(%s FN %s)" i a)) '(1 2 3 4)) => '("(1 FN (2 FN (3 FN 4)))" "(2 FN (3 FN 4))" "(3 FN 4)" 4) + (-reductions-r '+ '(1 2 3 4)) => '(10 9 7 4) + (-reductions-r '* '(1 2 3 4)) => '(24 24 12 4)) + (defexamples -count (-count 'even? '(1 2 3 4 5)) => 2 (--count (< it 4) '(1 2 3 4)) => 3)