scala/lib.scala
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 03 Mar 2013 12:44:42 +0000
changeset 211 1d6a0fd9f7f4
parent 193 317a2532c567
child 221 18905d086cbb
permissions -rw-r--r--
added factorial as an example

package object lib {

//some list functions
def tl[A](xs: List[A]) : List[A] = xs match {
  case Nil => Nil
  case x::xs => xs
}

def hd[A](xs: List[A]) : A = xs.head

def nth_of[A](xs: List[A], n: Int) = 
  if (n <= xs.length) Some(xs(n)) else None 

//some map functions
def dget(m: Map[Int, Int], n: Int) = m.getOrElse(n, 0)

//some string functions
def join[A](xs: List[A]) = xs.foldLeft("")(_.toString + _)

}