Handle empty list in -reductions[-r]

master
Basil L. Contovounesios 8 years ago
parent 88737493cd
commit 8f90dd7e33
No known key found for this signature in database
GPG Key ID: 205AB54A5D5D8CFF
  1. 8
      dash.el
  2. 24
      dev/examples.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'.

@ -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

Loading…
Cancel
Save