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?
Intro to Functional Programming with Scala - #psuweb
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
Intro to Functional Programming with Scala - #psuweb
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?

More Related Content

PDF
[Question Paper] Advanced SQL (Revised Course) [April / 2015]
PPTX
F# Ignite - DNAD2010
PDF
[Question Paper] Web Technology (Revised Course) [April / 2015]
PDF
Dynamic Type Inference for Gradual Hindley–Milner Typing
PDF
Java and Data Structure (April - 2015) [Revised Course | Question Paper]
PPTX
FSharp eye for the Haskell guy - London 2015
PDF
Python For Machine Learning
PDF
[Question Paper] Web Technology (Revised Course) [June / 2016]
[Question Paper] Advanced SQL (Revised Course) [April / 2015]
F# Ignite - DNAD2010
[Question Paper] Web Technology (Revised Course) [April / 2015]
Dynamic Type Inference for Gradual Hindley–Milner Typing
Java and Data Structure (April - 2015) [Revised Course | Question Paper]
FSharp eye for the Haskell guy - London 2015
Python For Machine Learning
[Question Paper] Web Technology (Revised Course) [June / 2016]

What's hot (20)

PDF
Java and Data Structure (October - 2016) [Revised Course | Question Paper]
PPT
Modern Compiler Design
PDF
[Question Paper] Web Technology (Revised Course) [September / 2013]
PDF
[Question Paper] Network Security (Revised Syllabus) [April / 2015]
PPT
CSC Millionaire
PDF
[Question Paper] Web Technology (Revised Course) [June / 2014]
PDF
[Question Paper] Advanced SQL (Revised Course) [June / 2016]
PDF
Some basic FP concepts
PDF
2011-09-19 Regex Day
PDF
[Question Paper] Web Technology (Revised Course) [October / 2016]
PDF
Network Security (Revised Syllabus) [QP / April - 2015]
PDF
Internet Technology (May – 2016) [Revised Syllabus | Question Paper]
PDF
[Question Paper] Data Communication and Network Standards (Revised Course) [J...
PDF
Introduction to clojure
PPT
Introduction to Python
PDF
Reduce course notes class xii
PPT
Logic Programming and Prolog
PDF
[Question Paper] Advanced SQL (Revised Course) [January / 2017]
Java and Data Structure (October - 2016) [Revised Course | Question Paper]
Modern Compiler Design
[Question Paper] Web Technology (Revised Course) [September / 2013]
[Question Paper] Network Security (Revised Syllabus) [April / 2015]
CSC Millionaire
[Question Paper] Web Technology (Revised Course) [June / 2014]
[Question Paper] Advanced SQL (Revised Course) [June / 2016]
Some basic FP concepts
2011-09-19 Regex Day
[Question Paper] Web Technology (Revised Course) [October / 2016]
Network Security (Revised Syllabus) [QP / April - 2015]
Internet Technology (May – 2016) [Revised Syllabus | Question Paper]
[Question Paper] Data Communication and Network Standards (Revised Course) [J...
Introduction to clojure
Introduction to Python
Reduce course notes class xii
Logic Programming and Prolog
[Question Paper] Advanced SQL (Revised Course) [January / 2017]
Ad

Similar to Intro to Functional Programming with Scala - #psuweb (20)

PPTX
Functional programming
PDF
Functional Programming in Scala: Notes
PPTX
Intro to Functional Programming
PPTX
Scala for curious
PDF
A taste of Functional Programming
PPTX
The joy of functional programming
PDF
Teach Yourself some Functional Programming with Scala
PDF
Functional programming is the most extreme programming
PDF
Functional Programming 101 for Java 7 Developers
PPTX
Scala Introduction
PDF
Functional programming in Scala
PDF
How to start functional programming (in Scala): Day1
PDF
Intro to functional programming - Confoo
PDF
Gentle Introduction to Scala
PDF
Demystifying functional programming with Scala
PDF
Functional Programming Essentials
PDF
Fp for the oo programmer
PPTX
Introduction to Scala
PPTX
Scala Introduction - Synerzip
PDF
Ankara Jug - Practical Functional Programming with Scala
Functional programming
Functional Programming in Scala: Notes
Intro to Functional Programming
Scala for curious
A taste of Functional Programming
The joy of functional programming
Teach Yourself some Functional Programming with Scala
Functional programming is the most extreme programming
Functional Programming 101 for Java 7 Developers
Scala Introduction
Functional programming in Scala
How to start functional programming (in Scala): Day1
Intro to functional programming - Confoo
Gentle Introduction to Scala
Demystifying functional programming with Scala
Functional Programming Essentials
Fp for the oo programmer
Introduction to Scala
Scala Introduction - Synerzip
Ankara Jug - Practical Functional Programming with Scala
Ad

Recently uploaded (20)

PPTX
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
PDF
Consumable AI The What, Why & How for Small Teams.pdf
PDF
Flame analysis and combustion estimation using large language and vision assi...
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PPTX
Modernising the Digital Integration Hub
PPTX
Final SEM Unit 1 for mit wpu at pune .pptx
DOCX
search engine optimization ppt fir known well about this
PDF
Developing a website for English-speaking practice to English as a foreign la...
PDF
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
PPTX
Configure Apache Mutual Authentication
PDF
CloudStack 4.21: First Look Webinar slides
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPTX
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
Five Habits of High-Impact Board Members
PPT
Geologic Time for studying geology for geologist
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PDF
Credit Without Borders: AI and Financial Inclusion in Bangladesh
PPT
What is a Computer? Input Devices /output devices
GROUP4NURSINGINFORMATICSREPORT-2 PRESENTATION
Consumable AI The What, Why & How for Small Teams.pdf
Flame analysis and combustion estimation using large language and vision assi...
Custom Battery Pack Design Considerations for Performance and Safety
Modernising the Digital Integration Hub
Final SEM Unit 1 for mit wpu at pune .pptx
search engine optimization ppt fir known well about this
Developing a website for English-speaking practice to English as a foreign la...
How IoT Sensor Integration in 2025 is Transforming Industries Worldwide
Configure Apache Mutual Authentication
CloudStack 4.21: First Look Webinar slides
A contest of sentiment analysis: k-nearest neighbor versus neural network
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
AI IN MARKETING- PRESENTED BY ANWAR KABIR 1st June 2025.pptx
NewMind AI Weekly Chronicles – August ’25 Week III
Five Habits of High-Impact Board Members
Geologic Time for studying geology for geologist
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Credit Without Borders: AI and Financial Inclusion in Bangladesh
What is a Computer? Input Devices /output devices

Intro to Functional Programming with Scala - #psuweb

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