Last active
August 29, 2015 14:00
-
-
Save ryoco/11281440 to your computer and use it in GitHub Desktop.
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
*.swp | |
*.class |
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
class OneToTen { | |
def last(xs: List[Int]) = xs.reverse.head | |
def lastR(xs: List[Int]): Int = xs match { | |
case x :: Nil => x | |
case _ :: tail => lastR(tail) | |
case _ => throw new NoSuchElementException | |
} | |
def penultimate(xs: List[Int]) = xs.reverse.tail.head | |
def penultimateR(xs: List[Int]): Int = xs match { | |
case x :: _ :: Nil => x | |
case _ :: tail => penultimateR(tail) | |
case _ => throw new NoSuchElementException | |
} | |
def nth(n: Int, xs: List[Int]) = xs(n) | |
def nthR(n: Int, xs: List[Int]): Int = (n, xs) match { | |
case (0, x :: _) => x | |
case (n, x :: tail) => nthR(n - 1, tail) | |
case (n, Nil) => throw new NoSuchElementException | |
} | |
def length(xs: List[Int]) = xs.size | |
def lengthR(xs: List[Int]): Int = xs match { | |
case Nil => 0 | |
case x :: tail => 1 + lengthR(tail) | |
} | |
def reverse(xs: List[Int]) = xs.reverse | |
def reverseR(xs: List[Int]): List[Int] = xs match { | |
case Nil => Nil | |
case x :: tail => reverseR(tail) :+ x | |
} | |
def isPalindrome(xs: List[Int]) = xs == xs.reverse | |
def flatten(xs: List[Any]): List[Any] = xs flatMap { | |
case xs: List[_] => flatten(xs) | |
case x => List(x) | |
} | |
def compress(xs: List[Any]): List[Any] = xs.distinct | |
def pack[A](xs: List[A]): List[List[A]] = xs match { | |
case Nil => List(List()) | |
case _ => { | |
val (packed, next) = xs span(_ == xs.head) | |
if (next == Nil) List(packed) | |
else packed :: pack(next) | |
} | |
} | |
def encode[A](xs: List[A]): List[(Int, A)] = xs match { | |
case Nil => Nil | |
case _ => { | |
val (packed, next) = xs span(_ == xs.head) | |
val p = (packed.length, packed.head) | |
if (next == Nil) List(p) | |
else p :: encode(next) | |
} | |
} | |
} |
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
object S99 extends OneToTen { | |
def main(args: Array[String]){ | |
println( | |
"last", | |
last(List(1, 1, 2, 3, 5, 8)), | |
lastR(List(1, 1, 2, 3, 5, 8)) | |
) | |
println( | |
"penultimate", | |
penultimate(List(1, 1, 2, 3, 5, 8)), | |
penultimateR(List(1, 1, 2, 3, 5, 8)) | |
) | |
println( | |
"nth", | |
nth(2, List(1, 1, 2, 3, 5, 8)), | |
nthR(2, List(1, 1, 2, 3, 5, 8)) | |
) | |
println( | |
"length", | |
length(List(1, 1, 2, 3, 5, 8)), | |
lengthR(List(1, 1, 2, 3, 5, 8)) | |
) | |
println( | |
"reverse", | |
reverse(List(1, 1, 2, 3, 5, 8)), | |
reverseR(List(1, 1, 2, 3, 5, 8)) | |
) | |
println( | |
"isPalindrome", | |
isPalindrome(List(1, 2, 3, 2, 1)) | |
) | |
println( | |
"flatten", | |
flatten(List(List(1, 1), 2, List(3, List(5, 8)))) | |
) | |
println( | |
"compress", | |
compress(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) | |
) | |
println( | |
"pack", | |
pack(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) | |
) | |
println( | |
"encode", | |
encode(List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)) | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment