KEMBAR78
Intro to Functional Programming with Scala - #psuweb | PPTX
Derek Morr, Penn State
@derekmorr
Functional Programming with
Demo code is at
https://github.com/derekmorr/webconf2015
What’s your background?
● Java? C#? C++?
● Python, Ruby, JavaScript?
● Haskell, ML, Erlang, Lisp?
● F#? Scala? Clojure?
● Anything else?
Stack Overflow
Developer Survey
2015
Stack Overflow
Developer Survey
2015
Is this valid?
x = x + 1
x = x + 1
if x is 5, then:
5 = 5 + 1
5 = 6
no, 5 ≠ 6
Purity
// impure
var a = 1
def incr() = {
a += 1
}
// pure
def 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?
Immutability
Value In, Value Out
Emphasis on data flow
Descriptive, not imperative
Immutability
OO makes code understandable by
encapsulating moving parts.
FP makes code understandable by
minimizing 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 value
final 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
A nice overview of Scala
If you want to learn FP - short
FP for OO folks
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?

Intro to Functional Programming with Scala - #psuweb

  • 1.
    Derek Morr, PennState @derekmorr Functional Programming with
  • 2.
    Demo code isat https://github.com/derekmorr/webconf2015
  • 3.
    What’s your background? ●Java? C#? C++? ● Python, Ruby, JavaScript? ● Haskell, ML, Erlang, Lisp? ● F#? Scala? Clojure? ● Anything else?
  • 5.
  • 6.
  • 7.
  • 8.
    x = x+ 1 if x is 5, then: 5 = 5 + 1 5 = 6 no, 5 ≠ 6
  • 10.
    Purity // impure var a= 1 def incr() = { a += 1 } // pure def incr(a: Int) = { a + 1 }
  • 11.
    An example find .-type f | grep foo | sort | uniq
  • 12.
    What’s a sideeffect? A lot of stuff we do now: ● Reassigning variables ● Modifying data in-place ● Throwing exceptions ● Interacting w/ the external environment
  • 13.
    What’s it like? Immutability ValueIn, Value Out Emphasis on data flow Descriptive, not imperative
  • 14.
    Immutability OO makes codeunderstandable by encapsulating moving parts. FP makes code understandable by minimizing moving parts. — Michael Feathers
  • 15.
    An Example def firstLarger(data:List[Int], threshold: Int): Int = ???
  • 16.
    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 ?
  • 17.
    Method Signatures &Error Signaling public int firstLarger(final int[] data, final int threshold) throws NoSuchElementException
  • 18.
    Method Signatures &Error Signaling /** * ... * @return null on error */ public Integer firstLarger(final int[] data, final int threshold)
  • 19.
    “My Billion DollarMistake” “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
  • 20.
    Use the typesystem Option[T] Some(value: T) None
  • 21.
    Option sealed abstract classOption[T] // wrapper around a value final class Some[T](value: T) extends Option[T] // Singleton marker for no value // Not null. Safe to dereference. object None extends Option
  • 22.
    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
  • 23.
    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
  • 24.
  • 25.
    A Detour -Functional Lists List(1,2,3)
  • 26.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil synonym for empty list
  • 27.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil pronounced “cons”
  • 28.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil
  • 29.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil
  • 30.
    A Detour -Functional Lists 1 :: 2 :: 3 :: NilList(1,2,3) =
  • 31.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil head tail (also a List)
  • 32.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil head tail
  • 33.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil head tail
  • 34.
    A Detour -Functional Lists 1 :: 2 :: 3 :: Nil
  • 35.
  • 36.
    If you wantto learn FP - short
  • 37.
    FP for OOfolks
  • 38.
    If you wantto learn FP - long
  • 39.
    Two good books First11 chapters online for free. Some chapters online for free. 40% off at manning.com w/ code tsfp14
  • 40.
    Pattern Matching Slides: CzechScala Enthusiasts: Pattern Matching and Case Classes (1h45) See also their blog.
  • 41.

Editor's Notes

  • #4 ask what they want to get out of talk, langs used, who’s used underscore ?
  • #5 Scala is #14 #25 in TIOBE
  • #7 F#, Scala, Clojure are functional. Rust has functional elements.
  • #10 pure function. value in value out. no side effects. side effects = reach outside box for variable access or io. or exceptions.
  • #12 No state. Not mutating a variable, we’re streaming data thru functions. Data isn’t changed during processing.
  • #17 Btw, ??? is valid scala - it’s a symbolic method name for undefined methods. Handy in code instead of TODO
  • #20 Famous computer scientist. Invented Quicksort. Won Turing Award, John von Neumann Medal, Kyoto Prize, Fellow of Royal Society & Royal Academy of Engineering. Even in “null-safe” languages like Ruby, nil is a bad idea - http://www.sandimetz.com/blog/2014/12/19/suspicions-of-nil Also, NULLs in SQL are a bad idea: https://www.youtube.com/watch?v=BC-OBmreMHY
  • #22 explain “sealed” keyword