Commit c14545ea authored by Nikodemus Siivola's avatar Nikodemus Siivola

ROTATE-RIGHT and ROTATE-LEFT replaced by a single function ROTATE

parent 7a338639
......@@ -145,8 +145,7 @@ terms and conditions:
@include include/macro-alexandria-deletef.texinfo
@include include/macro-alexandria-removef.texinfo
@include include/fun-alexandria-rotate-left.texinfo
@include include/fun-alexandria-rotate-right.texinfo
@include include/fun-alexandria-rotate.texinfo
@include include/fun-alexandria-suffle.texinfo
@include include/fun-alexandria-random-elt.texinfo
@include include/fun-alexandria-emptyp.texinfo
......
......@@ -57,8 +57,7 @@
#:deletef
#:proper-sequence
#:random-elt
#:rotate-left
#:rotate-right
#:rotate
#:sequence-of-length-p
#:suffle
;; Macros
......
(in-package :alexandria)
(defun rotate-left (sequence &optional (n 1))
"Rotates the SEQUENCE to left by N, by moving N elements from the end of the
sequence to the front. Resulting sequence may share structure with the
original one. N defaults to 1. Sequence must be a proper sequence. N can be
creater then the length of sequence."
(defun rotate-tail-to-head (sequence n)
(declare (type (integer 1) n))
(if (listp sequence)
(let ((m (mod n (list-length sequence))))
(if (null (cdr sequence))
......@@ -20,10 +17,8 @@ creater then the length of sequence."
(replace sequence tail)
sequence)))
(defun rotate-right (sequence &optional (n 1))
"Rotates the SEQUENCE to right by N, by moving N element from the front of
the sequence to the end. Resulting sequence may share structure with the
original one. N defaults to 1."
(defun rotate-head-to-tail (sequence n)
(declare (type (integer 1) n))
(if (listp sequence)
(let ((m (mod (1- n) (list-length sequence))))
(if (null (cdr sequence))
......@@ -39,6 +34,21 @@ original one. N defaults to 1."
(replace sequence head :start1 (- len m))
sequence)))
(defun rotate (sequence &optional (n 1))
"Returns a sequence of the same type as SEQUENCE, with the elements of
SEQUENCE rotated by N: N elements are moved from the end of the sequence to
the front if N is positive, and -N elements moved from the front to the end if
N is negative. SEQUENCE must be a proper sequence. N must be an integer,
defaulting to 1. If absolute value of N is greater then the length of the
sequence, the results are identical to calling ROTATE with (* (SIGNUM N) (MOD
N (LENGTH SEQUENCE))). The original sequence may be destructively altered, and
result sequence may share structure with it."
(if (plusp n)
(rotate-tail-to-head sequence n)
(if (minusp n)
(rotate-head-to-tail sequence (- n))
sequence)))
(defun suffle (sequence &key (start 0) end)
"Returns a radom permutation of SEQUENCE bounded by START and END.
Permuted sequence may share storage with the original one. Signals
......
......@@ -476,25 +476,25 @@
;;;; Sequences
(deftest rotate-left.1
(list (rotate-left (list 1 2 3) 0)
(rotate-left (list 1 2 3))
(rotate-left (list 1 2 3) 2)
(rotate-left (list 1 2 3) 3)
(rotate-left (list 1 2 3) 4))
(deftest rotate.1
(list (rotate (list 1 2 3) 0)
(rotate (list 1 2 3) 1)
(rotate (list 1 2 3) 2)
(rotate (list 1 2 3) 3)
(rotate (list 1 2 3) 4))
((1 2 3)
(3 1 2)
(2 3 1)
(1 2 3)
(3 1 2)))
(deftest rotate-left.2
(list (rotate-left (vector 1 2 3 4) 0)
(rotate-left (vector 1 2 3 4))
(rotate-left (vector 1 2 3 4) 2)
(rotate-left (vector 1 2 3 4) 3)
(rotate-left (vector 1 2 3 4) 4)
(rotate-left (vector 1 2 3 4) 5))
(deftest rotate.2
(list (rotate (vector 1 2 3 4) 0)
(rotate (vector 1 2 3 4))
(rotate (vector 1 2 3 4) 2)
(rotate (vector 1 2 3 4) 3)
(rotate (vector 1 2 3 4) 4)
(rotate (vector 1 2 3 4) 5))
(#(1 2 3 4)
#(4 1 2 3)
#(3 4 1 2)
......@@ -502,25 +502,25 @@
#(1 2 3 4)
#(4 1 2 3)))
(deftest rotate-right.1
(list (rotate-right (list 1 2 3) 0)
(rotate-right (list 1 2 3))
(rotate-right (list 1 2 3) 2)
(rotate-right (list 1 2 3) 3)
(rotate-right (list 1 2 3) 4))
(deftest rotate.3
(list (rotate (list 1 2 3) 0)
(rotate (list 1 2 3) -1)
(rotate (list 1 2 3) -2)
(rotate (list 1 2 3) -3)
(rotate (list 1 2 3) -4))
((1 2 3)
(2 3 1)
(3 1 2)
(1 2 3)
(2 3 1)))
(deftest rotate-right.2
(list (rotate-right (vector 1 2 3 4) 0)
(rotate-right (vector 1 2 3 4))
(rotate-right (vector 1 2 3 4) 2)
(rotate-right (vector 1 2 3 4) 3)
(rotate-right (vector 1 2 3 4) 4)
(rotate-right (vector 1 2 3 4) 5))
(deftest rotate.4
(list (rotate (vector 1 2 3 4) 0)
(rotate (vector 1 2 3 4) -1)
(rotate (vector 1 2 3 4) -2)
(rotate (vector 1 2 3 4) -3)
(rotate (vector 1 2 3 4) -4)
(rotate (vector 1 2 3 4) -5))
(#(1 2 3 4)
#(2 3 4 1)
#(3 4 1 2)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment