View
78
Download
7
Category
Tags:
Preview:
Citation preview
Derek Morr, Penn State@derekmorr
Functional Programming with
What’s your background?
● Java? C#? C++?● Python, Ruby, JavaScript? ● Haskell, ML, Erlang, Lisp?● F#? Scala? Clojure?● Anything else?
Stack OverflowDeveloper Survey 2015
Stack OverflowDeveloper Survey 2015
Is this valid?
x = x + 1
x = x + 1
if x is 5, then:
5 = 5 + 15 = 6
no, 5 ≠ 6
Purity
// impure var a = 1def incr() = { a += 1}
// puredef incr(a: Int) = { a + 1}
An example
find . -type f | grep foo | sort | uniq
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
What’s it like?
ImmutabilityValue In, Value OutEmphasis on data flowDescriptive, not imperative
Immutability
OO makes code understandable by encapsulating moving parts.
FP makes code understandable byminimizing moving parts.— Michael Feathers
An Example
def firstLarger(data: List[Int], threshold: Int): Int = ???
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 ?
Method Signatures & Error Signaling
public int firstLarger(final int[] data, final int threshold) throws NoSuchElementException
Method Signatures & Error Signaling
/** * ... * @return null on error */public Integer firstLarger(final int[] data, final int threshold)
“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
Use the type system
Option[T]
Some(value: T) None
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
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
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
Demo
A Detour - Functional Lists
List(1,2,3)
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
synonym for empty list
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
pronounced “cons”
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
A Detour - Functional Lists
1 :: 2 :: 3 :: NilList(1,2,3) =
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
head
tail(also a List)
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
head
tail
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
head
tail
A Detour - Functional Lists
1 :: 2 :: 3 :: Nil
If you want to learn FP - long
Two good books
First 11 chapters online for free. Some chapters online for free.
40% off at manning.com w/ code tsfp14
Pattern Matching
Slides: Czech Scala Enthusiasts: Pattern Matching and Case Classes (1h45)
See also their blog.
Questions?
Recommended