branch: externals/dash commit 677c1561142db89cb151f50ce21f24096ca1a328 Merge: f1ffdf7 016e0de Author: Matus Goljer <dota.k...@gmail.com> Commit: GitHub <nore...@github.com>
Merge pull request #290 from leungbk/rotate Generalize `-rotate` for `|n|` greater than `(length list)`. --- dash.el | 8 +++++--- dev/examples.el | 4 +++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/dash.el b/dash.el index 5822361..fcfe5d2 100644 --- a/dash.el +++ b/dash.el @@ -921,9 +921,11 @@ See also: `-drop'" "Rotate LIST N places to the right. With N negative, rotate to the left. The time complexity is O(n)." (declare (pure t) (side-effect-free t)) - (if (> n 0) - (append (last list n) (butlast list n)) - (append (-drop (- n) list) (-take (- n) list)))) + (when list + (let* ((len (length list)) + (n-mod-len (mod n len)) + (new-tail-len (- len n-mod-len))) + (append (-drop new-tail-len list) (-take new-tail-len list))))) (defun -insert-at (n x list) "Return a list with X inserted into LIST at position N. diff --git a/dev/examples.el b/dev/examples.el index a250b34..2c8a94d 100644 --- a/dev/examples.el +++ b/dev/examples.el @@ -694,7 +694,9 @@ new list." (defexamples -rotate (-rotate 3 '(1 2 3 4 5 6 7)) => '(5 6 7 1 2 3 4) - (-rotate -3 '(1 2 3 4 5 6 7)) => '(4 5 6 7 1 2 3)) + (-rotate -3 '(1 2 3 4 5 6 7)) => '(4 5 6 7 1 2 3) + (-rotate 16 '(1 2 3 4 5 6 7)) => '(6 7 1 2 3 4 5) + (-rotate -16 '(1 2 3 4 5 6 7)) => '(3 4 5 6 7 1 2)) (defexamples -repeat (-repeat 3 :a) => '(:a :a :a)