Selasa, 11 Januari 2011

foldRight Scala

$ cat Main.scala
object Main {
def main ( args: Array[String] ) {
try {
val elems = args map Integer.parseInt
println ( "The sum of my arguments is: " + elems.foldRight ( 0 ) ( _ + _ ) )
} catch {
case e: NumberFormatException =>
println ( "Usage: scala Main ... " )
}
}
}


$ scalac Main.scala


$ scala Main `seq 1 10`
The sum of my arguments is: 55


$ scala Main `seq 1 100`
The sum of my arguments is: 5050


$ scala Main `seq 1 1024`
The sum of my arguments is: 524800

Tidak ada komentar:

Posting Komentar