Add utility function ein:list-move-right

This commit is contained in:
Takafumi Arakaki 2012-09-02 04:05:44 +02:00
parent 81ec270382
commit f25e3b98a7
2 changed files with 27 additions and 0 deletions

View file

@ -358,6 +358,24 @@ Elements are compared using the function TEST (default: `eq')."
list)
finally do (error "ELEM %S is not in LIST %S" elem list))))))
(defun* ein:list-move-right (list elem &key (test #'eq))
"Move ELEM in LIST right. TEST is used to compare elements"
(loop with first = t
for rest on list
when (funcall test (car rest) elem)
return (if (cdr rest)
(let ((next (cadr rest)))
(setf (car rest) next)
(setf (cadr rest) elem)
list)
(if first
list
(setcdr rest-1 nil)
(cons elem list)))
finally do (error "ELEM %S is not in LIST %S" elem list)
for rest-1 = rest
do (setq first nil)))
(defun ein:get-value (obj)
"Get value from obj if it is a variable or function."
(cond

View file

@ -104,3 +104,12 @@ def func():
(should (equal (ein:list-move-left '(a b c d) 'b) '(b a c d)))
(should (equal (ein:list-move-left '(a b c d) 'c) '(a c b d)))
(should (equal (ein:list-move-left '(a b c d) 'd) '(a b d c))))
(ert-deftest ein:list-move-right ()
(should (equal (ein:list-move-right '(a) 'a) '(a)))
(should (equal (ein:list-move-right '(a b) 'a) '(b a)))
(should (equal (ein:list-move-right '(a b) 'b) '(b a)))
(should (equal (ein:list-move-right '(a b c d) 'a) '(b a c d)))
(should (equal (ein:list-move-right '(a b c d) 'b) '(a c b d)))
(should (equal (ein:list-move-right '(a b c d) 'c) '(a b d c)))
(should (equal (ein:list-move-right '(a b c d) 'd) '(d a b c))))