From f25e3b98a73a92b8d893e18423b2e6f15db599de Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sun, 2 Sep 2012 04:05:44 +0200 Subject: [PATCH] Add utility function ein:list-move-right --- lisp/ein-utils.el | 18 ++++++++++++++++++ tests/test-ein-utils.el | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/lisp/ein-utils.el b/lisp/ein-utils.el index a6c15e8..80aab6c 100644 --- a/lisp/ein-utils.el +++ b/lisp/ein-utils.el @@ -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 diff --git a/tests/test-ein-utils.el b/tests/test-ein-utils.el index cb74fa5..565bd39 100644 --- a/tests/test-ein-utils.el +++ b/tests/test-ein-utils.el @@ -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))))