lib/core: Added rotation methods to Sequence
authorLucas Bajolet <r4pass@hotmail.com>
Thu, 21 Apr 2016 15:04:22 +0000 (11:04 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Fri, 22 Apr 2016 15:09:38 +0000 (11:09 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

lib/core/collection/abstract_collection.nit

index ade3bfc..5cffbec 100644 (file)
@@ -1163,6 +1163,30 @@ interface Sequence[E]
        #
        # REQUIRE `index >= 0 and index < length`
        fun remove_at(index: Int) is abstract
+
+       # Rotates the elements of self once to the left
+       #
+       # ~~~nit
+       # var a = [12, 23, 34, 45]
+       # a.rotate_left
+       # assert a == [23, 34, 45, 12]
+       # ~~~
+       fun rotate_left do
+               var fst = shift
+               push fst
+       end
+
+       # Rotates the elements of self once to the right
+       #
+       # ~~~nit
+       # var a = [12, 23, 34, 45]
+       # a.rotate_right
+       # assert a == [45, 12, 23, 34]
+       # ~~~
+       fun rotate_right do
+               var lst = pop
+               unshift lst
+       end
 end
 
 # Iterators on indexed collections.