emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/dash 774aa96 5/5: Optimize -rotate for n=0


From: ELPA Syncer
Subject: [elpa] externals/dash 774aa96 5/5: Optimize -rotate for n=0
Date: Tue, 6 Jul 2021 15:57:10 -0400 (EDT)

branch: externals/dash
commit 774aa967b64e8078d3238b984fcacf056b7bb661
Author: Basil L. Contovounesios <contovob@tcd.ie>
Commit: Basil L. Contovounesios <contovob@tcd.ie>

    Optimize -rotate for n=0
    
    * dash.el (-rotate): Reduce to copy-sequence when n=0.
    * dev/examples.el (-rotate): Extend tests.
    
    * README.md:
    * dash.texi: Regenerate docs.
---
 README.md       |  2 +-
 dash.el         | 13 +++++++------
 dash.texi       |  2 +-
 dev/examples.el | 31 +++++++++++++++++++++++++++----
 4 files changed, 36 insertions(+), 12 deletions(-)

diff --git a/README.md b/README.md
index baea7d8..c738bf9 100644
--- a/README.md
+++ b/README.md
@@ -1800,7 +1800,7 @@ Other list functions not fit to be classified elsewhere.
 
 #### -rotate `(n list)`
 
-Rotate `list` `n` places to the right.  With `n` negative, rotate to the left.
+Rotate `list` `n` places to the right (left if `n` is negative).
 The time complexity is O(n).
 
 ```el
diff --git a/dash.el b/dash.el
index d53c3bc..6f97aca 100644
--- a/dash.el
+++ b/dash.el
@@ -1190,14 +1190,15 @@ is done in a single list traversal."
     (list (nreverse result) list)))
 
 (defun -rotate (n list)
-  "Rotate LIST N places to the right.  With N negative, rotate to the left.
+  "Rotate LIST N places to the right (left if N is negative).
 The time complexity is O(n)."
   (declare (pure t) (side-effect-free t))
-  (when list
-    (let* ((len (length list))
-           (n-mod-len (mod n len))
-           (new-tail-len (- len n-mod-len)))
-      (append (nthcdr new-tail-len list) (-take new-tail-len list)))))
+  (cond ((null list) ())
+        ((zerop n) (copy-sequence list))
+        ((let* ((len (length list))
+                (n-mod-len (mod n len))
+                (new-tail-len (- len n-mod-len)))
+           (append (nthcdr 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/dash.texi b/dash.texi
index e2d0d8c..e7e0376 100644
--- a/dash.texi
+++ b/dash.texi
@@ -2650,7 +2650,7 @@ Other list functions not fit to be classified elsewhere.
 
 @anchor{-rotate}
 @defun -rotate (n list)
-Rotate @var{list} @var{n} places to the right.  With @var{n} negative, rotate 
to the left.
+Rotate @var{list} @var{n} places to the right (left if @var{n} is negative).
 The time complexity is @var{o}(n).
 
 @example
diff --git a/dev/examples.el b/dev/examples.el
index 95cdf98..d5d5210 100644
--- a/dev/examples.el
+++ b/dev/examples.el
@@ -1006,16 +1006,39 @@ related predicates."
     (-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)
-    (-rotate -1 ()) => ()
-    (-rotate 0 ()) => ()
-    (-rotate 1 ()) => ()
+    (let* ((l (list 1 2)) (r (-rotate -1 l))) (setcdr l 0) r) => '(2 1)
+    (let* ((l (list 1 2)) (r (-rotate 0 l))) (setcdr l 0) r) => '(1 2)
+    (let* ((l (list 1 2)) (r (-rotate 1 l))) (setcdr l 0) r) => '(2 1)
+    (-rotate -1 '()) => '()
+    (-rotate 0 '()) => '()
+    (-rotate 1 '()) => '()
+    (-rotate -2 '(1)) => '(1)
     (-rotate -1 '(1)) => '(1)
     (-rotate 0 '(1)) => '(1)
     (-rotate 1 '(1)) => '(1)
+    (-rotate 2 '(1)) => '(1)
+    (-rotate -4 '(1 2)) => '(1 2)
+    (-rotate -3 '(1 2)) => '(2 1)
+    (-rotate -2 '(1 2)) => '(1 2)
     (-rotate -1 '(1 2)) => '(2 1)
     (-rotate 0 '(1 2)) => '(1 2)
     (-rotate 1 '(1 2)) => '(2 1)
-    (-rotate 2 '(1 2)) => '(1 2))
+    (-rotate 2 '(1 2)) => '(1 2)
+    (-rotate 3 '(1 2)) => '(2 1)
+    (-rotate 4 '(1 2)) => '(1 2)
+    (-rotate -6 '(1 2 3)) => '(1 2 3)
+    (-rotate -5 '(1 2 3)) => '(3 1 2)
+    (-rotate -4 '(1 2 3)) => '(2 3 1)
+    (-rotate -3 '(1 2 3)) => '(1 2 3)
+    (-rotate -2 '(1 2 3)) => '(3 1 2)
+    (-rotate -1 '(1 2 3)) => '(2 3 1)
+    (-rotate 0 '(1 2 3)) => '(1 2 3)
+    (-rotate 1 '(1 2 3)) => '(3 1 2)
+    (-rotate 2 '(1 2 3)) => '(2 3 1)
+    (-rotate 3 '(1 2 3)) => '(1 2 3)
+    (-rotate 4 '(1 2 3)) => '(3 1 2)
+    (-rotate 5 '(1 2 3)) => '(2 3 1)
+    (-rotate 6 '(1 2 3)) => '(1 2 3))
 
   (defexamples -repeat
     (-repeat 3 :a) => '(:a :a :a)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]