Skip to content

Commit

Permalink
Speed up -flatten-n a bit
Browse files Browse the repository at this point in the history
* dash.el (-flatten-n): Don't maintain a list of intermediate
results.  Eta-reduce.
* dev/examples.el (-flatten-n): Check for destructive side effects.

Re: #373.
  • Loading branch information
basil-conto committed Feb 28, 2021
1 parent 3deba09 commit 0b9cdba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 3 additions & 1 deletion dash.el
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,9 @@ See also: `-flatten-n'"
See also: `-flatten'"
(declare (pure t) (side-effect-free t))
(-last-item (--iterate (--mapcat (-list it) it) list (1+ num))))
(dotimes (_ num)
(setq list (apply #'append (mapcar #'-list list))))
list)

(defun -concat (&rest lists)
"Return a new list with the concatenation of the elements in the supplied LISTS."
Expand Down
5 changes: 4 additions & 1 deletion dev/examples.el
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ new list."
(-flatten-n 0 '(3 4)) => '(3 4)
(-flatten-n 0 '((1 2) (3 4))) => '((1 2) (3 4))
(-flatten-n 0 '(((1 2) (3 4)))) => '(((1 2) (3 4)))
(-flatten-n 1 '(((1 . 2)) ((3 . 4)))) => '((1 . 2) (3 . 4)))
(-flatten-n 1 '(((1 . 2)) ((3 . 4)))) => '((1 . 2) (3 . 4))
(let ((l (list 1 (list 2) 3))) (-flatten-n 0 l) l) => '(1 (2) 3)
(let ((l (list 1 (list 2) 3))) (-flatten-n 1 l) l) => '(1 (2) 3)
(let ((l (list 1 (list 2) 3))) (-flatten-n 2 l) l) => '(1 (2) 3))

(defexamples -replace
(-replace 1 "1" '(1 2 3 4 3 2 1)) => '("1" 2 3 4 3 2 "1")
Expand Down

0 comments on commit 0b9cdba

Please sign in to comment.