Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Making the functional paradigm shift, a book you need to read

conor10
March 11, 2015

Making the functional paradigm shift, a book you need to read

There are plenty of great resources available for learning FP and Scala. In this talk, I'll give you my take on why this book is one that I believe no Scala developer should be without...

conor10

March 11, 2015
Tweet

More Decks by conor10

Other Decks in Technology

Transcript

  1. Why talk about a book? It’s a book that actually

    helps you get functional programming though an emphasis on practical application
  2. A bit about me • Financial technologist (writing trading, regulatory,

    risk applications) for ~10 years • Java technical lead • Do a lot financial modelling with Python in my spare time (www.quanttech.co) • Recently started working with Scala professionally
  3. Functional Programming in Scala • Written by Paul Chiusano &

    Rúnar Bjarnason • Scalaz core contributors • “The book we wish had existed when we were learning functional programming”
  4. The Content • 4 Parts, 15 Chapters 1. FP introduction

    2. Designing functional libraries 3. Common structures (patterns?) in FP 4. Dealing with effects and IO • Workbook structure - theory, examples, & exercises • 1-2 weeks per chapter
  5. The Exercises • 5 to 30 exercises per chapter •

    Companion repo on GitHub https://github.com/ fpinscala/fpinscala containing: • Placeholders for chapter exercises • Hints for most exercises • Solutions for all • It even has it's own Google Group scala-functional
  6. Polymorphic Functions def partial1[A,B,C](a: A, f: (A,B) => C): B

    => C = (b: B) => f(a, b) def curry[A,B,C](f: (A, B) => C): A => (B => C) = (a: A) => (b: B) => f(a, b) def uncurry[A,B,C](f: A => B => C): (A, B) => C = (a: A, b: B) => f(a)(b)
  7. Functional Data Structures Persistent lists (as per Okasaki) sealed trait

    List[+A] case object Nil extends List[Nothing] case class Cons[+A](head: A, tail: List[A]) extends List[A]
  8. Append object List { def append[A](a1: List[A], a2: List[A]): List[A]

    = a1 match { case Nil => a2 case Cons(h,t) => Cons(h, append(t, a2)) }
  9. Fold def foldRight[A,B](as: List[A], z: B)(f: (A, B) => B):

    B = as match { case Nil => z case Cons(x, xs) => f(x, foldRight(xs, z)(f)) }
  10. def foldLeft[A,B](l: List[A], z: B)(f: (B, A) => B): B

    = { @annotation.tailrec def loop(l: List[A], acc: B): B = l match { case Nil => acc case Cons(x, xs) => loop(xs, f(acc, x)) } loop(l, z) }
  11. Map def map[A,B](l: List[A])(f: A => B): List[B] = foldRight(l,

    Nil: List[B])((x, y) => Cons(f(x), y)) def flatMap[A,B](l: List[A])(f: A => List[B]): List[B] = foldRight(l, Nil: List[B])((x, y) => append(f(x), y))
  12. Streams case object Empty extends Stream[Nothing] case class Cons[+A](h: ()

    => A, t: () => Stream[A]) extends Stream[A] object Stream { def cons[A](hd: => A, tl: => Stream[A]): Stream[A] = { lazy val head = hd lazy val tail = tl Cons(() => head, () => tail) }
  13. trait Stream[+A] { def foldRight[B](z: => B)(f: (A, => B)

    => B): B = this match { case Cons(h,t) => f(h(), t().foldRight(z)(f)) case _ => z } def map[B](f: A => B): Stream[B] = foldRight(empty[B])((h, t) => cons(f(h), t)) def flatMap[B](f: A => Stream[B]): Stream[B] = foldRight(empty[B])((h, t) => f(h).append(t)) }
  14. Error handling sealed trait Option[+A] case class Some[+A](get: A) extends

    Option[A] case object None extends Option[Nothing] sealed trait Either[+E,+A] case class Left[+E](get: E) extends Either[E,Nothing] case class Right[+A](get: A) extends Either[Nothing,A]
  15. State case class State[S,+A](run: S => (A, S)) { def

    map[B](f: A => B): State[S, B] = flatMap(a => State.unit(f(a))) def map2[B,C](sb: State[S, B])(f: (A, B) => C): State[S, C] = flatMap((a: A) => sb.map((b: B) => f(a, b))) def flatMap[B](f: A => State[S, B]): State[S, B] = State(s => { val (a, s1) = run(s) f(a).run(s1) }) }
  16. Other topics • Property-based testing • Monads • Monoids •

    Parallelism • Working with I/O and mutable state
  17. A few things to note • The workbook format may

    not appeal to all • Not a reference book • Some of the exercises are very tough • Not suitable as a first book on Scala (especially if you’re unfamiliar with it’s type system) • No unit tests, but…
  18. Unit tests Fork the project and add your own class

    ListTest extends FunSuite { test("append") { assert(List(1, 2, 3, 4) === append(List(1, 2), List(3, 4))) } test("flatMap") { assert(List(1, 1, 2, 2, 3, 3) === flatMap(List(1, 2, 3))(i => List(i, i))) } …
  19. In summary You should read this book if: • You

    know the basics of Scala & want to further your abilities in FP • You’re an expert - you can test yourself on the exercises Otherwise: • Learn the basics of Scala (e.g. via Coursera) • Then read this book
  20. Resources Book site http://manning.com/bjarnason/ Full story behind the book http://blog.higher-order.com/blog/2014/09/18/at-long-last/

    Companion book http://blog.higher-order.com/blog/2015/03/06/a-companion-booklet-to-functional- programming-in-scala/ Github repo https://github.com/fpinscala/fpinscala My blog http://www.quanttech.co