James Ward - Developer Advocate @ Google Cloud
Is addOne()
a function?
“If you can use math to do something, you should”
Philip Wadler
val a = addOne(2)
val b = addOne(2)
val b = a
def upper(s: String): String = {
s.toUpperCase
}
val upper: String => String = (s: String) => s.toUpperCase
val upper: String => String = _.toUpperCase
val count = (s: String) => s.length
count(upper("hello"))
val handleRequest: Request => Response = { request =>
...
}
val getUsers: () => List[User]
val cu = upper.andThen(count)
cu("hello")
val doSomethingToFirst = (s: String, f: Char => Char) => {
if (s.nonEmpty)
f(s.head) + s.tail
else
s
}
val d = doSomethingToFirst("asdf", _)
d(_.toUpper)
"hello".map(_.toUpper)
Try("hello").map(_.toUpperCase)
val f: String => String = _.toUpperCase
val g: String => Int = _.length
val h = f andThen g
Try("hello").map(h)
Some("asdf").filter(_.contains("a"))
"hello".foldLeft(0)(_ + _.toInt)
Try("hello").map(_ => Try("asdf")).flatten
val num1 = Try(StdIn.readLine("Number 1: ").toInt)
val num2 = Try(StdIn.readLine("Number 1 was ???; Number 2: ").toInt)
import scala.io.StdIn
import scala.util.Try
for {
num1 <- Try(StdIn.readLine("Number 1: ").toInt)
num2 <- Try(StdIn.readLine("Number 2: ").toInt)
} yield num1 + num2
for {
num <- client.url(randomNumUrl).get().map(_.body.toInt)
wordReqs = Seq.fill(num)(client.url(randomWordUrl).get().map(_.body))
words <- Future.sequence(wordReqs)
} yield words.mkString(" ")
("asdf", 2)
case class Foo(name: String, age: Int)
Foo("asdf", 1)
sealed trait Bar
case class FooBar() extends Bar
case class BarBar() extends Bar
val b: Bar = BarBar()
foo match {
case Foo(name, 2) => name
case _ => "arggg!"
}
b match {
case FooBar() => "foo"
case BarBar() => "bar"
}