intro to functional programming with scala - #psuweb

41
Derek Morr, Penn State @derekmorr Functional Programming with

Upload: derekmorr

Post on 13-Aug-2015

64 views

Category:

Technology


7 download

TRANSCRIPT

Page 1: Intro to Functional Programming with Scala - #psuweb

Derek Morr, Penn State@derekmorr

Functional Programming with

Page 2: Intro to Functional Programming with Scala - #psuweb

Demo code is at https://github.com/derekmorr/webconf2015

Page 3: Intro to Functional Programming with Scala - #psuweb

What’s your background?

● Java? C#? C++?● Python, Ruby, JavaScript? ● Haskell, ML, Erlang, Lisp?● F#? Scala? Clojure?● Anything else?

Page 4: Intro to Functional Programming with Scala - #psuweb
Page 5: Intro to Functional Programming with Scala - #psuweb

Stack OverflowDeveloper Survey 2015

Page 6: Intro to Functional Programming with Scala - #psuweb

Stack OverflowDeveloper Survey 2015

Page 7: Intro to Functional Programming with Scala - #psuweb

Is this valid?

x = x + 1

Page 8: Intro to Functional Programming with Scala - #psuweb

x = x + 1

if x is 5, then:

5 = 5 + 15 = 6

no, 5 ≠ 6

Page 9: Intro to Functional Programming with Scala - #psuweb
Page 10: Intro to Functional Programming with Scala - #psuweb

Purity

// impure var a = 1def incr() = { a += 1}

// puredef incr(a: Int) = { a + 1}

Page 11: Intro to Functional Programming with Scala - #psuweb

An example

find . -type f | grep foo | sort | uniq

Page 12: Intro to Functional Programming with Scala - #psuweb

What’s a side effect?

A lot of stuff we do now:● Reassigning variables● Modifying data in-place● Throwing exceptions● Interacting w/ the external environment

Page 13: Intro to Functional Programming with Scala - #psuweb

What’s it like?

ImmutabilityValue In, Value OutEmphasis on data flowDescriptive, not imperative

Page 14: Intro to Functional Programming with Scala - #psuweb

Immutability

OO makes code understandable by encapsulating moving parts.

FP makes code understandable byminimizing moving parts.— Michael Feathers

Page 15: Intro to Functional Programming with Scala - #psuweb

An Example

def firstLarger(data: List[Int], threshold: Int): Int = ???

Page 16: Intro to Functional Programming with Scala - #psuweb

An Example

def firstLarger(data: List[Int], threshold: Int): Int = ???

val numbers = List(3, 2, 4, 7, 9, 8, 1)firstLarger(numbers, 5) // returns 7

firstLarger(numbers, 10) // returns ?

Page 17: Intro to Functional Programming with Scala - #psuweb

Method Signatures & Error Signaling

public int firstLarger(final int[] data, final int threshold) throws NoSuchElementException

Page 18: Intro to Functional Programming with Scala - #psuweb

Method Signatures & Error Signaling

/** * ... * @return null on error */public Integer firstLarger(final int[] data, final int threshold)

Page 19: Intro to Functional Programming with Scala - #psuweb

“My Billion Dollar Mistake”

“I call it my billion-dollar mistake. It was the invention of the null reference in 1965... This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.”

- Sir Tony Hoare (2009)Null References: The Billion Dollar Mistake

Page 20: Intro to Functional Programming with Scala - #psuweb

Use the type system

Option[T]

Some(value: T) None

Page 21: Intro to Functional Programming with Scala - #psuweb

Option

sealed abstract class Option[T]

// wrapper around a valuefinal class Some[T](value: T) extends Option[T]

// Singleton marker for no value// Not null. Safe to dereference.object None extends Option

Page 22: Intro to Functional Programming with Scala - #psuweb

Scala Map

val people = Map("Frank" -> "N. Furter", "Lady" -> "Gaga")

val frank = people.get("Frank")frank: Option[String] = Some(N. Furter)

val bob = people.get("Bob")bob: Option[String] = None

Page 23: Intro to Functional Programming with Scala - #psuweb

Signature

val numbers = List(3, 2, 4, 7, 9, 8, 1)

def firstLarger(data: List[Int], threshold: Int): Option[Int] = ???

firstLarger(numbers, 5) // Some(5)

firstLarger(numbers, 10) // None

Page 24: Intro to Functional Programming with Scala - #psuweb

Demo

Page 25: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

List(1,2,3)

Page 26: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

synonym for empty list

Page 27: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

pronounced “cons”

Page 28: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

Page 29: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

Page 30: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: NilList(1,2,3) =

Page 31: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

head

tail(also a List)

Page 32: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

head

tail

Page 33: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

head

tail

Page 34: Intro to Functional Programming with Scala - #psuweb

A Detour - Functional Lists

1 :: 2 :: 3 :: Nil

Page 35: Intro to Functional Programming with Scala - #psuweb

A nice overview of Scala

Page 36: Intro to Functional Programming with Scala - #psuweb

If you want to learn FP - short

Page 37: Intro to Functional Programming with Scala - #psuweb

FP for OO folks

Page 38: Intro to Functional Programming with Scala - #psuweb

If you want to learn FP - long

Page 39: Intro to Functional Programming with Scala - #psuweb

Two good books

First 11 chapters online for free. Some chapters online for free.

40% off at manning.com w/ code tsfp14

Page 41: Intro to Functional Programming with Scala - #psuweb

Questions?