Merge pull request #290 from leungbk/rotate

Generalize `-rotate` for `|n|` greater than `(length list)`.
master
Matus Goljer 7 years ago committed by GitHub
commit 677c156114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      dash.el
  2. 4
      dev/examples.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.

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

Loading…
Cancel
Save