Tips for CodeGolf in Scala

Check for http://qiita.com/cignoir/items/ce85a70d3333ca329222

Collections

val l=List(1,2,3)
val s=Seq(1,2,3)
// s(0) is one character shorter than s.head.
val s=Seq(1,2,3)
val t=(1,2,3)
// You can use s(0) or t._1 to ref their head.

Repeating

1 to 10 map(_=>println("hi!"))
for(i<-1 to 10)println("hi!")
Seq.fill(10)(println("hi!"))
while(true)
while(0<1)

Standard Input

io.Source.stdin
readLine
// Throw an exception after reading lines from stdin.
while(0<1)println(readLine.r)

Definition

def f(a:String,b:String,c:String)
type S=String;def f(a:S,b:S,c:S)
val a=Array(Array(1,2),Array(3,4))
val A=Array;val a=A(A(1,2),A(3,4))
def foo(s:Seq[Int])
def foo(s:Int*)
// Call twice the same function for initialization.
val n,k=readInt

Others

List(1,2,3,4).filter(_%2==0)
List(1,2,3,4)filter(_%2==0)
List("a","b","c") foreach println
List("a","b","c") map println
// Using ? as identifier.
val l=List(1,2,3)
val? =List(1,2,3) // OK
val ?=List(1,2,3) // Error

// You don't need a delimiter.
print(?size)
def a(? :Int*)=(?,?tail).zipped.map(_-_)
"abc"*2 => "abcabc"
"abc"*0 => ""