
Untitled
By: a guest on
May 13th, 2012 | syntax:
None | size: 0.92 KB | hits: 18 | expires: Never
Scala by Example - problems with currying
def msort[A](less: (A, A) => Boolean)(xs: List[A]): List[A] = {
def merge(xs1: List[A], xs2: List[A]): List[A] =
if (xs1.isEmpty) xs2
else if (xs2.isEmpty) xs1
else if (less(xs1.head, xs2.head)) xs1.head :: merge(xs1.tail, xs2)
else xs2.head :: merge(xs1, xs2.tail)
val n = xs.length/2
if (n == 0) xs
else merge(msort(less)(xs take n), msort(less)(xs drop n))
}
val intSort = msort((x : Int, y : Int) => x < y)
val reverseSort = msort((x:Int, y:Int) => x > y)
val intSort = msort((x : Int, y : Int) => x < y)(List(1, 2, 4))
val reverseSort = msort((x:Int, y:Int) => x > y)(List(4, 3, 2))
error: missing arguments for method msort in object $iw;
follow this method with `_' if you want to treat it as a partially applied function
val intSort = msort((x : Int, y : Int) => x < y) _
val intSort: List[Int] => List[Int] = msort((x: Int, y: Int) => x < y)