首页 > 代码库 > Scala function programming

Scala function programming

1. Arbitrary multi parameters funcs

sum(1,2,3,4,5) = sum(1 to 5: _*)
the equal ‘=‘ can be ignored if there is none  return val
1 def sum(args: Int*) = {2     var result = 03     for(arg <- args) result += arg4     result    5 }
 
  scala Array Buffer to Java List:
1 import scala.collection.JavaConversions.bufferAsJavaList2 import scala.collection.mutable.ArrayBuffer3 val command = ArrayBuffer("ls","-al","/home/mike")4 val pb = new ProcessBuilder(command)

  reverse operation

1 import scala.collection.JavaConversion.asScalaBuffer2 import scala.collection.mutable.Buffer3 val cmd:Buffer[String] = pb.command()

  Hash Map

1 val hs01 = Map("s01" -> 342342,"s02" -> 4245)2 hs01.getOrElse("s01",0)

 

Scala function programming