activator new
activator run
activator ~run
activator console
activator ~test
App (Hello.scala):
object Hello extends App {
println("hello, world")
}
REPL:
scala> println("hello, world")
Mutable state is the new spaghetti code
val foo: String = "asdf"
Let the compiler figure out the types
val foo = "asdf"
val listOfInt = List(1,2,3)
val f = (s: String) => s.length
def stringLength(s: String): Int = s.length
List("asdf","zxc","q").map(stringLength)
List("asdf","zxc","q").map(s => s.length)
List("asdf","zxc","q").map(_.length)
Unit
is a valueclass Foo
return foo;
val foo = if (true) "foo" else "bar"
foo match {
case foo: Foo => println("foo")
case bar: Bar => println("bar")
}
List(1, "asdf", false).collect {
case i: Int => "not a number"
case s: String => s.toUpperCase
}
val maybeFoo = if (util.Random.nextBoolean()) Some("Foo") else None
maybeFoo.getOrElse("Bar")
case class Foo(name: String)
val foo = new Foo("asdf")
val foo = Foo.apply("asdf")
val foo = Foo("asdf")
val bar = foo.copy(name = "zxvc")
val m1 = Some("asdf")
val m2 = None
m1.flatMap(mm1 => m2.map(mm2 => mm1 + mm2))
for {
mm1 <- m1
mm2 <- m2
} yield mm1 + mm2
def foo(s: String)(i: Int): String = s.substring(i)
foo("asdf")(1)
val f: (Int => String) = foo("asdf")(_)
Fill in this value by finding something in scope that matches the type
def foo(s: String)(implicit i: Int): String = s.substring(i)
implicit val i = 1
foo("asdf")
val l = List(1, "asdf")
object Foo {
val a = "asdf"
}
class Foo {
val a = "zcxv"
}
new Foo().a
Foo.a
def >*#(s: String): String = s.toUpperCase
>*#("asdf")
"asdf".substring(1)
"asdf" substring 1