Intro to
Learning Scala

Bangkok Functional Programmers

May 17th, 2017

Yours Truly

ches

ches

chesmart.in

meetup.com/bkkhack

Why Scala?

Multi-Paradigm

  • Your personal on-ramp to FP
  • A Trojan Horse for FP into your company

Long Live the JVM

  • The JVM is pervasive, mature, and performant
  • Statically-typed benefits,
    dynamic language comforts
    • Type inference
    • REPL
    • Everything is an expression

…But

Community

❤️ Martin

Prof. Odersky man crush
Prof. Odersky man crush

Concurrency & Streams

  • Modern abstractions built on JVM’s
    proven primitives
  • Futures, parallel collections
  • Actors
  • Rx, Reactive Streams
  • Monix, FS2 née scalaz Streams, FRP
  • In the large: Spark, Flink

Progressive Research

  • Alternative runtimes
  • Macros: compile-time metaprogramming
  • DOT calculus, dotty, Scala 3

The Gateway Drug

Lead a Horse to Water

  • Scala makes immutable the default
  • There is no null (except for the sake of Java)
  • Everything is an expression, remember?
  • Algebraic Data Types (sit tight)
Turing-complete type system‽
Turing-complete type system‽

A rich, powerful type system enables you
to design more error-proof APIs.

Algebraic Data Types

Sum Types

Elegant in Haskell:

data Bool = False | True

Noisier in Scala:

sealed trait Bool
case object False extends Bool
case object True extends Bool

Product Types

The venerable Tuple:

val ches: Tuple2[String, Int] = ("Ches", 35)

And its richer cousin, the case class:

case class Person(name: String, age: Int)
// Actual type ends up being:
// case class Person extends Product with Serializable

Person.tupled(ches) // => Person = Person(Ches,35)

These sums and products compose,
that’s the “algebra”.

/* (Slightly simplified from the true Scala Option type) */
sealed abstract class Option[A]
case object None extends Option[Nothing]
final case class Some[A](value: A) extends Option[A]

Functions as Objects

package object logging {
  type Tags = Map[String, String]
}

package logging {

  /** Tags associated with a logged metric. */
  object Tags {
    def apply(elems: (String, String)*): Tags = Map(elems: _*)

    def empty: Tags = Map.empty
  }
}

Practical Macros: Logging

logger.debug(s"Some $expensive message!")

becomes:

if (logger.isDebugEnabled)
  logger.debug(s"Some $expensive message!")


https://github.com/typesafehub/scala-logging

The Typeclass Pattern

  • Ad hoc polymorphism (what?)
  • This is a deep talk, maybe another Meetup
  • Familiar with typeclasses?
    (You probably learned Haskell).
    You may want to look at Cats & scalaz.

Resources

Warming Up

Programming in Scala, 3rd ed.
Odersky, Spoon, Venners
(“the Stairway Book”)

The Neophyte’s Guide to Scala – Daniel Westheide

Scala’s Types of Types – Konrad Malawski

Find Li Haoyi’s trove like Hands-on Scala.js

Getting Serious

EPFL Scala specialization on Coursera

Finding Libraries

Scala Library Index

Hardcore FP

Functional Programming in Scala
Chiusano, Bjarnason
(“the Red Book”)

Typelevel Blog

scala-exercises.org

herding cats – Eugene Yokota

Workshop Time

REPL Workout!

  • Please ask questions. Please.
  • Try stuff!
  • Remind me about sbt.
  • I could go on for awhile, leave if you gotta.