SlideShare a Scribd company logo
1 of 41
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?

More Related Content

What's hot

Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Design
nextlib
 

What's hot (20)

Java and Data Structure (October - 2016) [Revised Course | Question Paper]
Java and Data Structure (October - 2016) [Revised Course | Question Paper]Java and Data Structure (October - 2016) [Revised Course | Question Paper]
Java and Data Structure (October - 2016) [Revised Course | Question Paper]
 
Modern Compiler Design
Modern Compiler DesignModern Compiler Design
Modern Compiler Design
 
[Question Paper] Web Technology (Revised Course) [September / 2013]
[Question Paper] Web Technology (Revised Course) [September / 2013][Question Paper] Web Technology (Revised Course) [September / 2013]
[Question Paper] Web Technology (Revised Course) [September / 2013]
 
[Question Paper] Network Security (Revised Syllabus) [April / 2015]
[Question Paper] Network Security (Revised Syllabus) [April / 2015][Question Paper] Network Security (Revised Syllabus) [April / 2015]
[Question Paper] Network Security (Revised Syllabus) [April / 2015]
 
CSC Millionaire
CSC MillionaireCSC Millionaire
CSC Millionaire
 
[Question Paper] Web Technology (Revised Course) [June / 2014]
[Question Paper] Web Technology (Revised Course) [June / 2014][Question Paper] Web Technology (Revised Course) [June / 2014]
[Question Paper] Web Technology (Revised Course) [June / 2014]
 
[Question Paper] Advanced SQL (Revised Course) [June / 2016]
[Question Paper] Advanced SQL (Revised Course) [June / 2016][Question Paper] Advanced SQL (Revised Course) [June / 2016]
[Question Paper] Advanced SQL (Revised Course) [June / 2016]
 
Some basic FP concepts
Some basic FP conceptsSome basic FP concepts
Some basic FP concepts
 
2011-09-19 Regex Day
2011-09-19 Regex Day2011-09-19 Regex Day
2011-09-19 Regex Day
 
[Question Paper] Web Technology (Revised Course) [October / 2016]
[Question Paper] Web Technology (Revised Course) [October / 2016][Question Paper] Web Technology (Revised Course) [October / 2016]
[Question Paper] Web Technology (Revised Course) [October / 2016]
 
Network Security (Revised Syllabus) [QP / April - 2015]
Network Security (Revised Syllabus) [QP / April - 2015]Network Security (Revised Syllabus) [QP / April - 2015]
Network Security (Revised Syllabus) [QP / April - 2015]
 
Internet Technology (May – 2016) [Revised Syllabus | Question Paper]
Internet Technology (May – 2016) [Revised Syllabus | Question Paper]Internet Technology (May – 2016) [Revised Syllabus | Question Paper]
Internet Technology (May – 2016) [Revised Syllabus | Question Paper]
 
Getting started with R
Getting started with RGetting started with R
Getting started with R
 
[Question Paper] Data Communication and Network Standards (Revised Course) [J...
[Question Paper] Data Communication and Network Standards (Revised Course) [J...[Question Paper] Data Communication and Network Standards (Revised Course) [J...
[Question Paper] Data Communication and Network Standards (Revised Course) [J...
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Lec216
Lec216Lec216
Lec216
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
Logic Programming and Prolog
Logic Programming and PrologLogic Programming and Prolog
Logic Programming and Prolog
 
[Question Paper] Advanced SQL (Revised Course) [January / 2017]
[Question Paper] Advanced SQL (Revised Course) [January / 2017][Question Paper] Advanced SQL (Revised Course) [January / 2017]
[Question Paper] Advanced SQL (Revised Course) [January / 2017]
 

Similar to Intro to Functional Programming with Scala - #psuweb

Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
Alexander Podkhalyuzin
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
Eelco Visser
 

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

F# and the DLR
F# and the DLRF# and the DLR
F# and the DLR
 
Go Programming language, golang
Go Programming language, golangGo Programming language, golang
Go Programming language, golang
 
Functions, List and String methods
Functions, List and String methodsFunctions, List and String methods
Functions, List and String methods
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
It's All About Morphisms
It's All About MorphismsIt's All About Morphisms
It's All About Morphisms
 
Clojure Small Intro
Clojure Small IntroClojure Small Intro
Clojure Small Intro
 
Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)Under the hood of scala implicits (kl10tch 10.03.2015)
Under the hood of scala implicits (kl10tch 10.03.2015)
 
Introduction to F#
Introduction to F#Introduction to F#
Introduction to F#
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
Clojure intro
Clojure introClojure intro
Clojure intro
 
Tech Talks @NSU: DLang: возможности языка и его применение
Tech Talks @NSU: DLang: возможности языка и его применениеTech Talks @NSU: DLang: возможности языка и его применение
Tech Talks @NSU: DLang: возможности языка и его применение
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
TI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific LanguagesTI1220 Lecture 14: Domain-Specific Languages
TI1220 Lecture 14: Domain-Specific Languages
 
Uni texus austin
Uni texus austinUni texus austin
Uni texus austin
 
Just Kotlin
Just KotlinJust Kotlin
Just Kotlin
 
Experimental dtrace
Experimental dtraceExperimental dtrace
Experimental dtrace
 
The Arrow Library in Kotlin
The Arrow Library in KotlinThe Arrow Library in Kotlin
The Arrow Library in Kotlin
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 
Trafaret: monads and python
Trafaret: monads and pythonTrafaret: monads and python
Trafaret: monads and python
 
MMBJ Shanzhai Culture
MMBJ Shanzhai CultureMMBJ Shanzhai Culture
MMBJ Shanzhai Culture
 

Recently uploaded

IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Recently uploaded (20)

TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 

Intro to Functional Programming with Scala - #psuweb

Editor's Notes

  1. ask what they want to get out of talk, langs used, who’s used underscore ?
  2. Scala is #14 #25 in TIOBE
  3. F#, Scala, Clojure are functional. Rust has functional elements.
  4. pure function. value in value out. no side effects. side effects = reach outside box for variable access or io. or exceptions.
  5. No state. Not mutating a variable, we’re streaming data thru functions. Data isn’t changed during processing.
  6. Btw, ??? is valid scala - it’s a symbolic method name for undefined methods. Handy in code instead of TODO
  7. 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
  8. explain “sealed” keyword