REAL WORLD 1 final case class Somefink[A](a: A) extends PossiblyMaybe[A] 2 final case object Nowt extends PossiblyMaybe[ Nothing] 3 4 sealed trait PossiblyMaybe[+A] 5 object PossiblyMaybeOps { 6 def noneDefault[A](pm: PossiblyMaybe[A])(a: A): A = pm match { 7 case Somefink(x) => x 8 case _ => a 9 } 10 } Note _ in second match, caters for nulls with Java interop 9
MODEL 1 final case object Production extends Env 2 final case object Staging extends Env 3 final case class QA(n: Int) extends Env 4 final case class Dev(u: String) extends Env 5 final case object Integration extends Env 6 sealed trait Env 7 object Env { 8 /* companion object code... "instances" */ 9 } 10
POLYMORPHISM 1 // companion object for Env 2 object Env { 3 // default "instances" over type Env 4 // for typeclasses below 5 implicit val EnvRead: Read[Env] = ??? 6 implicit val EnvShow: Show[Env] = ??? 7 8 // maybe you want ability to use 9 // one of two implementations of Order[Env] 10 // as well as Equal[Env]. Anyone? 11 implicit val EnvOrder: Order[Env] = ??? 12 implicit val EnvEqual: Equal[Env] = ??? 13 } 11
1 // At the end of the universe we then do... 2 run(env).unsafePerformIO 3 4 // or whatever your starting point is, e.g. 5 main(args).unsafePerformIO 6 7 // only ONCE... most of the time ;) 19
"INTERFACES" A type defined as a Monad (think: (>>=)) can also be used as: → A Functor (think: fmap) → An Applicative (think: <*>, pure) → And possibly others though not necessarily 20
Monoids: Accumulators are everywhere, almost → Functors: Lots of uses with common and user defined types → Monads: Effects, "linear happy path", and more → Applicatives: "validations", safer Java interop, and more → More: e.g. Arrows, Zippers, Lenses, Tagged Types, … 22
Properties: property based testing: quickcheck, scalacheck → Data Types: start closed, extend using "type classes", dependent types, etc when relevant → Abstractions: build small building blocks, use motar to build solid walls → Dist Systems: using algebraic abstractions, properties to build more useful distributed systems 23