Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Last active December 28, 2015 14:58
Show Gist options
  • Save JohnMurray/7518029 to your computer and use it in GitHub Desktop.
Save JohnMurray/7518029 to your computer and use it in GitHub Desktop.
Blog post
val s1 = {"hello"} // s1: String = hello
val s = "hello" // s: String = hello
val f1 = (x : Int => x * x) // invalid - compiler error (can't resolve x)
val f2 = (x : Int) => x * x // valid
val f3 = {x : Int => x * x} // valid
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That = {
def builder = { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
val b = bf(repr)
b.sizeHint(this)
b
}
val b = builder
for (x <- this) b += f(x)
b.result
}
val list = List(1, 2, 3, 4)
list.map(x => x * x)
val list = List(1, 2, 3, 4)
list.map { x => x * x }
val list = List(1, 2, 3, 4)
list.map(x => x * x)
list.map({x => x * x})
val list = List(1, 2, 3, 4)
// not valid (compile-error)
list.map(x =>
val y = x * x
val z = y * x
Math.sin(z / x)
)
// valid
list.map{x =>
val y = x * x
val z = y * x
Math.sin(z / x)
}
def echo(x: String) = println(x)
echo{"hello"} // valid
echo("hello") // valid
def echoName(name: String, x: String) = println("$name: $x")
echoName{"John", "hello"} // invalid
echoName("John", "hello") // valid
echoName({"John"}, {"hello"}) // valid
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment