Last active
August 29, 2015 14:26
-
-
Save fancellu/b9e8fe4ffb1c6589b2f8 to your computer and use it in GitHub Desktop.
RotateN to left : Alternate solution for http://aperiodic.net/phil/scala/s-99/p19.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Alternate solution for | |
http://aperiodic.net/phil/scala/s-99/p19.scala | |
def rotate[A](n: Int, ls: List[A])= | |
if (ls.isEmpty) ls | |
else{ | |
val (first,last)=ls.splitAt((if (n>=0) n else ls.length+n)%ls.length) | |
last:::first | |
} | |
val li=List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k) | |
rotate(3,li) //> res1: List[Symbol] = List('d, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a, 'b, 'c) | |
rotate(-2,li) //> res2: List[Symbol] = List('j, 'k, 'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i) | |
rotate(0,li) //> res3: List[Symbol] = List('a, 'b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k) | |
rotate(li.length+1,li) //> res4: List[Symbol] = List('b, 'c, 'd, 'e, 'f, 'g, 'h, 'i, 'j, 'k, 'a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment