Skip to main content
Mod Removes Wiki by Doorknob
added last paragraph and Collections-headline
Source Link
user unknown
  • 4.6k
  • 31
  • 31

Collections

The first choice for a random collection is often List. In many cases you can replace it with Seq, which saves one character instantan. :)

Instead of

val l=List(1,2,3)
val s=Seq(1,2,3)

and, while s.head and s.tail is more elegant in usual code, s(0) is again one character shorter than s.head.

Even shorter in some cases - depending on needed functionality is a tuple:

val s=Seq(1,2,3)
val t=(1,2,3)

saving 3 characters immediately, and for accessing:

s(0)
t._1

it is the same for direct index access. But for elaborated concepts, tuples fail:

scala> s.map(_*2)
res55: Seq[Int] = List(2, 4, 6)

scala> t.map(_*2)
<console>:9: error: value map is not a member of (Int, Int, Int)
       t.map(_*2)
         ^

###update

def foo(s:Seq[Int])
def foo(s:Int*)

In parameter declaration, Int* saves 4 characters over Seq[Int]. It is not equivalent, but sometimes, Int* will do.

The first choice for a random collection is often List. In many cases you can replace it with Seq, which saves one character instantan. :)

Instead of

val l=List(1,2,3)
val s=Seq(1,2,3)

and, while s.head and s.tail is more elegant in usual code, s(0) is again one character shorter than s.head.

Even shorter in some cases - depending on needed functionality is a tuple:

val s=Seq(1,2,3)
val t=(1,2,3)

saving 3 characters immediately, and for accessing:

s(0)
t._1

it is the same for direct index access. But for elaborated concepts, tuples fail:

scala> s.map(_*2)
res55: Seq[Int] = List(2, 4, 6)

scala> t.map(_*2)
<console>:9: error: value map is not a member of (Int, Int, Int)
       t.map(_*2)
         ^

Collections

The first choice for a random collection is often List. In many cases you can replace it with Seq, which saves one character instantan. :)

Instead of

val l=List(1,2,3)
val s=Seq(1,2,3)

and, while s.head and s.tail is more elegant in usual code, s(0) is again one character shorter than s.head.

Even shorter in some cases - depending on needed functionality is a tuple:

val s=Seq(1,2,3)
val t=(1,2,3)

saving 3 characters immediately, and for accessing:

s(0)
t._1

it is the same for direct index access. But for elaborated concepts, tuples fail:

scala> s.map(_*2)
res55: Seq[Int] = List(2, 4, 6)

scala> t.map(_*2)
<console>:9: error: value map is not a member of (Int, Int, Int)
       t.map(_*2)
         ^

###update

def foo(s:Seq[Int])
def foo(s:Int*)

In parameter declaration, Int* saves 4 characters over Seq[Int]. It is not equivalent, but sometimes, Int* will do.

Source Link
user unknown
  • 4.6k
  • 31
  • 31

The first choice for a random collection is often List. In many cases you can replace it with Seq, which saves one character instantan. :)

Instead of

val l=List(1,2,3)
val s=Seq(1,2,3)

and, while s.head and s.tail is more elegant in usual code, s(0) is again one character shorter than s.head.

Even shorter in some cases - depending on needed functionality is a tuple:

val s=Seq(1,2,3)
val t=(1,2,3)

saving 3 characters immediately, and for accessing:

s(0)
t._1

it is the same for direct index access. But for elaborated concepts, tuples fail:

scala> s.map(_*2)
res55: Seq[Int] = List(2, 4, 6)

scala> t.map(_*2)
<console>:9: error: value map is not a member of (Int, Int, Int)
       t.map(_*2)
         ^