Slides of my talk "The Ease of Scalaz" at the Scala Days 2011 conference in Stanford, CA. The talk included a significant amount of live coding, hence the slides alone are of little avail.
6. What’s wrong with this code?
val versionToYear = Map(
"1.0" -> "2003",
"2.0" -> "2006",
"2.9" -> "2011")
def isVersionFromYear(version: String, year: String) =
(versionToYear get version) == year
7. What’s wrong with this code?
val versionToYear = Map(
"1.0" -> "2003",
"2.0" -> "2006",
"2.9" -> "2011")
def isVersionFromYear(version: String, year: String) =
(versionToYear get version) == year
== is not type-safe
Hence isVersionFromYear will always return false
9. How should we handle effects?
def effects(args: String *): Unit = {
def arg(index: Int) = args(index).toInt
val result = arg(0) + arg(1)
println(result)
}
10. How should we handle effects?
def effects(args: String *): Unit = {
def arg(index: Int) = args(index).toInt
val result = arg(0) + arg(1)
println(result)
}
Option, Either ?
11. How should we handle effects?
def effects(args: String *): Unit = {
def arg(index: Int) = args(index).toInt
val result = arg(0) + arg(1)
println(result)
}
Option, Either ?
Validation!
13. How can we process effects?
def arg(index: Int): Validation[NonEmptyList[String], Int] = ...
val result = (arg(0), arg(1)) match {
case (Success(i0), Success(i1)) => Success(i0 + i1)
case (Failure(f0), Failure(f1)) => Failure(f0.list <::: f1)
case (f @ Failure(_), _) => f
case (_, f @ Failure(_)) => f
}
println(result)
14. How can we process effects?
def arg(index: Int): Validation[NonEmptyList[String], Int] = ...
val result = (arg(0), arg(1)) match {
case (Success(i0), Success(i1)) => Success(i0 + i1)
case (Failure(f0), Failure(f1)) => Failure(f0.list <::: f1)
case (f @ Failure(_), _) => f
case (_, f @ Failure(_)) => f
}
println(result)
Not very helpful, right?
15. What we really want:
def arg(index: Int): Validation[NonEmptyList[String], Int] = ...
val result = { _ + _ } (arg(0), arg(1)) // Doesn’t compile!
println(result)
16. What we really want:
def arg(index: Int): Validation[NonEmptyList[String], Int] = ...
val result = { _ + _ } (arg(0), arg(1)) // Doesn’t compile!
println(result)
We want to apply a pure function
to effectful arguments!