branch: externals/dash commit 8f90dd7e33ba140ec5ad005c0ad58befface73dd Author: Basil L. Contovounesios <conto...@tcd.ie> Commit: Basil L. Contovounesios <conto...@tcd.ie>
Handle empty list in -reductions[-r] --- dash.el | 8 ++++++-- dev/examples.el | 24 ++++++++++++++++-------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/dash.el b/dash.el index e0437b8..fd0cb03 100644 --- a/dash.el +++ b/dash.el @@ -289,7 +289,7 @@ See also: `-reductions', `-reductions-r', `-reduce-r'" See `-reduce' for explanation of the arguments. See also: `-reductions-from', `-reductions-r', `-reduce-r'" - (-reductions-from fn (car list) (cdr list))) + (and list (-reductions-from fn (car list) (cdr list)))) (defun -reductions-r-from (fn init list) "Return a list of the intermediate values of the reduction. @@ -305,7 +305,11 @@ See also: `-reductions-r', `-reductions', `-reduce'" See `-reduce-r' for explanation of the arguments. See also: `-reductions-r-from', `-reductions', `-reduce'" - (-reductions-r-from fn (-last-item list) (-butlast list))) + (when list + (let ((rev (reverse list))) + (--reduce-from (cons (funcall fn it (car acc)) acc) + (list (car rev)) + (cdr rev))))) (defmacro --filter (form list) "Anaphoric form of `-filter'. diff --git a/dev/examples.el b/dev/examples.el index 7ae2d1f..fff94d3 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -340,24 +340,32 @@ new list." (--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 (lambda (a i) (format "(%s FN %d)" 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)) + (-reductions-from '* 1 '(1 2 3 4)) => '(1 1 2 6 24) + (-reductions-from '- 10 '(1)) => '(10 9) + (-reductions-from '- 10 ()) => '(10)) (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 (lambda (i a) (format "(%d 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)) + (-reductions-r-from '* 1 '(1 2 3 4)) => '(24 24 12 4 1) + (-reductions-r-from '- 10 '(1)) => '(-9 10) + (-reductions-r-from '- 10 ()) => '(10)) (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 (lambda (a i) (format "(%s FN %d)" 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)) + (-reductions '* '(1 2 3 4)) => '(1 2 6 24) + (-reductions '- '(1)) => '(1) + (-reductions '- ()) => ()) (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 (lambda (i a) (format "(%d 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)) + (-reductions-r '* '(1 2 3 4)) => '(24 24 12 4) + (-reductions-r '- '(1)) => '(1) + (-reductions-r '- ()) => ()) (defexamples -count (-count 'even? '(1 2 3 4 5)) => 2