Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Why talk about a book? It’s a book that actually helps you get functional programming though an emphasis on practical application

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

The Book…

Slide 5

Slide 5 text

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”

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

*My solutions, not the authors have been used Some examples*

Slide 9

Slide 9 text

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)

Slide 10

Slide 10 text

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]

Slide 11

Slide 11 text

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)) }

Slide 12

Slide 12 text

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)) }

Slide 13

Slide 13 text

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) }

Slide 14

Slide 14 text

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))

Slide 15

Slide 15 text

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) }

Slide 16

Slide 16 text

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)) }

Slide 17

Slide 17 text

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]

Slide 18

Slide 18 text

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) }) }

Slide 19

Slide 19 text

Other topics • Property-based testing • Monads • Monoids • Parallelism • Working with I/O and mutable state

Slide 20

Slide 20 text

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…

Slide 21

Slide 21 text

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))) } …

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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