Intro to Scala – Get Started with Typesafe Activator – Immutability



Intro to Scala – Get Started with Typesafe Activator – Immutability

0 1


intro-to-scala


On Github jamesward / intro-to-scala

Intro to Scala

James Ward ~ @_JamesWard

Get Started with Typesafe Activator

Download Activator: http://typesafe.com/platform/getstarted Extract & launch Activator:
activator new
(or launch the UI from your file browser) Create a new app with the Hello, Scala template

Run the app once:

activator run

Run the app continuously:

activator ~run

Launch the Scala REPL:

activator console

Run the tests continuously:

activator ~test

Immutability

Non-side-effecting and concurrency friendly

val foo: String = "asdf"

Type Inference

Type Safety with less Typing

val foo = "asdf"
val listOfInt: List[Int] = List(1,2,3)

Case Classes

Value Objects Done Right

case class Foo(name: String)
  • Concise Syntax
  • Built-in toString, equals, hash-code, extractor, creator, copy
val foo = Foo("asdf")
val foo1 = Foo.apply("asdf")
val foo2 = new Foo("asdf")
val bar = foo.copy("zxvc")

Optional Values

Say Goodbye to Null Pointer Exceptions

val asdf: Option[String] = Some("asdf")
val notta: Option[String] = None

Higher Order Functions

Functions are a thing

def stringLength(s: String): Int = s.length

Functions can take other functions as params

def stringTransformer(s: String, f: String => Int): Int = f(s)

The map() function transforms each item in a container

List("a","bb","ccc").map(stringLength)

Anonymous functions

List("a","bb","ccc").map(s => s.length)

Learn More