Bangkok Functional Programmers
May 17th, 2017
null
(except for the sake of Java)A rich, powerful type system enables you
to design more error-proof APIs.
Elegant in Haskell:
data Bool = False | True
Noisier in Scala:
sealed trait Bool
case object False extends Bool
case object True extends Bool
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]
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
}
}
logger.debug(s"Some $expensive message!")
becomes:
if (logger.isDebugEnabled)
logger.debug(s"Some $expensive message!")
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
Functional Programming in Scala –
Chiusano, Bjarnason (“the Red Book”)
herding cats – Eugene Yokota