SlideShare a Scribd company logo
INTRODUCTIONTO 

FUNCTIONAL
PROGRAMMING
A very brief
WHY FP?
• More structured (like goto vs OO)
• Clear & concise
• Performance, parallelism, concurrency
• Reusable, generics
• Expand your horizons!
• Become a more flexible programmer
• Have fun
WHAT IS FUNCTIONAL
PROGRAMMING
• Family of languages
• Not one monolithic thing
• Collection of attributes & (very) general style
TWO SOLUTIONSTOTHE SAME
ENTSCHEIDUNGSPROBLEM
AlanTuring Alonzo Church
Imperative (Instructions) Functional (everything is a function)
Global State (“memory”) Stateless (like REST)
Mechanistic (Turing Machine) Mathematical (Lambda Calculus)
CONSTELLATION OF
ATTRIBUTES
• Immutability
• Recursion
• Stateless / explicit state
• Higher-order functions
• Data types
• Type safety
• Don’t need all of these!
• Some languages enforce these more than others
• Can do some FP in traditionally “imperative” languages, too
• Java, Ruby, JavaScript, and so on
• Lazy evaluation
• Composition
• Referential transparency
• Currying & partial function application
• Reusability
• Pattern matching
APPROACH
• Imperative style describes how to do things
• Functional style is often declarative
• describes what things are
• …plus data and transformations between them
• Sometimes called “data oriented”
RELATIONSHIP WITH 

OBJECT-ORIENTATION
FunctionalObjective
Obviously opposites
RELATIONSHIP WITH 

OBJECT-ORIENTATION
Objective-ness
Functional-ness
Ruby
JavaScript
Haskell
OCaml
Erlang
Swift
C
Common LISP
Assembly
Clojure
IMMUTABILITY, PURITY &
RECURSION
• Advantages
• Get parallelization “for free” because no shared state
• No deadlocks or strange state infection bugs
• Compact and/or “does the work for you”
• Disadvantages
• Recursion can be mind bending at first
• Recursion may have time or memory space costs
IMMUTABILITY MAKES SENSE
What you type What the compiler says
x = 42 Okay,“x” means 42
x Hmmm… 42!
x = 99
What are you? Some kind of liar?

“x” means 42, obviously!
x Hmmm… still 42!
MAPPING, FILTERS, FOLDING &
ACCUMULATION
• Much simpler to reason about & cleaner syntax
• Often has a performance advantage
# Ruby Fold Example: sum numbers
range = (0..99_999_999)
## Imperative/procedural
total = 0
i = 0
while i < range.count do
total += range[i]
i += 1
end
total
## Functional
range.reduce(:+)
• Don’t need to calculate the length of the array
• Don’t rely on a throwaway variable
## Roughly
def reduce_add(array)
return 0 if array == []
array[0] + reduce_add(array[1..-1])
end
LIST COMPREHENSIONS &
CONSTRAINT PROGRAMMING
-- Haskell List Comprehension
[(x, y) | x <- [50..100], y <- [1..99], x `mod` 7 == 3, x `mod` y == 42]
-- RESULT: [(87,45),(94,52)]
-- Haskell List Comprehension: INFINITE EDITION
[(x, y) | x <- [50..], y <- [1..99], x `mod` 7 == 3, x `mod` y == 42]
-- RESULT: [(87,45),(94,52),(101,59),(108,66),(115,73),(122,80),(129,87),
(136,47),(136,94),(150,54),(164,61),(171,43),(178,68),(192,50),(192,75),
(206,82),(213,57),(220,89),(234,48),(234,64),(234,96),(255,71),(262,44),
(262,55),(276,78),(290,62),(297,51),(297,85),(318,46),(318,69)…
…and so on, FOREVER, and while working with the data stream
FURTHER, CRAZIER STUFF
• Faster custom server
• Serves ~2 million requests/second
• Triggered a rare Linux flaw due to high throughput
• Quantum computing
• Massive truly parallel systems
• Parallelism “for free” (more or less)
FURTHER READING
• There is so much more!

We’ve barely scratched the surface
• Vancouver Functional Programmers
• LearnYou a Haskell for Great Good
• Clojure for the Brave andTrue
• What I Wish I Knew Before Learning Haskell
• Refactoring Ruby with Monads
• The Underscore.js Source
• Don’t Fear the Monad
• Don’t be Afraid of Functional Programming (SmashingMag)

More Related Content

What's hot

Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
Marcus Denker
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
Bhanuka Uyanage
 
Elixir and elm
Elixir and elmElixir and elm
Elixir and elm
Mix & Go
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
Marcus Denker
 
Elixir Study Group Kickoff Meetup
Elixir Study Group Kickoff MeetupElixir Study Group Kickoff Meetup
Elixir Study Group Kickoff Meetup
Yuri Leikind
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
Marcus Denker
 
javascript
javascript javascript
javascript
Kaya Ota
 
Apache Velocity
Apache VelocityApache Velocity
Apache Velocity
Bhavya Siddappa
 
Introduction To Less
Introduction To Less Introduction To Less
Introduction To Less
Knoldus Inc.
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
vivek p s
 
Javascript Journey
Javascript JourneyJavascript Journey
Javascript Journey
Wanqiang Ji
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
Talha Ocakçı
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
Peter Maas
 
Lecture 5 javascript
Lecture 5 javascriptLecture 5 javascript
Lecture 5 javascript
Mujtaba Haider
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
Dmitrii Stoian
 
How to start using Scala
How to start using ScalaHow to start using Scala
How to start using Scala
Ngoc Dao
 
Variables and User Input
Variables and User InputVariables and User Input
Variables and User Input
primeteacher32
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
Diego Freniche Brito
 
Introduction to Laravel
Introduction to LaravelIntroduction to Laravel
Introduction to Laravel
Eli Wheaton
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
Rangana Sampath
 

What's hot (20)

Dynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection PromisesDynamically Composing Collection Operations through Collection Promises
Dynamically Composing Collection Operations through Collection Promises
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Elixir and elm
Elixir and elmElixir and elm
Elixir and elm
 
Reflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond SmalltakReflection in Pharo: Beyond Smalltak
Reflection in Pharo: Beyond Smalltak
 
Elixir Study Group Kickoff Meetup
Elixir Study Group Kickoff MeetupElixir Study Group Kickoff Meetup
Elixir Study Group Kickoff Meetup
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
javascript
javascript javascript
javascript
 
Apache Velocity
Apache VelocityApache Velocity
Apache Velocity
 
Introduction To Less
Introduction To Less Introduction To Less
Introduction To Less
 
Object oriented java script
Object oriented java scriptObject oriented java script
Object oriented java script
 
Javascript Journey
Javascript JourneyJavascript Journey
Javascript Journey
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 
Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3Domain Specific Languages In Scala Duse3
Domain Specific Languages In Scala Duse3
 
Lecture 5 javascript
Lecture 5 javascriptLecture 5 javascript
Lecture 5 javascript
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
 
How to start using Scala
How to start using ScalaHow to start using Scala
How to start using Scala
 
Variables and User Input
Variables and User InputVariables and User Input
Variables and User Input
 
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides:  Let's build macOS CLI Utilities using SwiftMobileConf 2021 Slides:  Let's build macOS CLI Utilities using Swift
MobileConf 2021 Slides: Let's build macOS CLI Utilities using Swift
 
Introduction to Laravel
Introduction to LaravelIntroduction to Laravel
Introduction to Laravel
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 

Similar to A (very brief) into to Functional Programming

Functional programming
Functional programmingFunctional programming
Functional programming
Prateek Jain
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
Doug Sparling
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
jamesmarken1
 
ppt7
ppt7ppt7
ppt7
callroom
 
ppt2
ppt2ppt2
ppt2
callroom
 
name name2 n
name name2 nname name2 n
name name2 n
callroom
 
ppt9
ppt9ppt9
ppt9
callroom
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
amiable_indian
 
ppt18
ppt18ppt18
ppt18
callroom
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
callroom
 
test ppt
test ppttest ppt
test ppt
callroom
 
name name2 n
name name2 nname name2 n
name name2 n
callroom
 
ppt21
ppt21ppt21
ppt21
callroom
 
name name2 n
name name2 nname name2 n
name name2 n
callroom
 
ppt17
ppt17ppt17
ppt17
callroom
 
ppt30
ppt30ppt30
ppt30
callroom
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
callroom
 
Basics of functional Programming by Siddharth Kulkarni
Basics of functional Programming by Siddharth KulkarniBasics of functional Programming by Siddharth Kulkarni
Basics of functional Programming by Siddharth Kulkarni
Chandulal Kavar
 
Introduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-UpsIntroduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-Ups
edlich
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
Shimi Bandiel
 

Similar to A (very brief) into to Functional Programming (20)

Functional programming
Functional programmingFunctional programming
Functional programming
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
 
PARADIGM IT.pptx
PARADIGM IT.pptxPARADIGM IT.pptx
PARADIGM IT.pptx
 
ppt7
ppt7ppt7
ppt7
 
ppt2
ppt2ppt2
ppt2
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt9
ppt9ppt9
ppt9
 
Ruby for Perl Programmers
Ruby for Perl ProgrammersRuby for Perl Programmers
Ruby for Perl Programmers
 
ppt18
ppt18ppt18
ppt18
 
name name2 n2
name name2 n2name name2 n2
name name2 n2
 
test ppt
test ppttest ppt
test ppt
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt21
ppt21ppt21
ppt21
 
name name2 n
name name2 nname name2 n
name name2 n
 
ppt17
ppt17ppt17
ppt17
 
ppt30
ppt30ppt30
ppt30
 
name name2 n2.ppt
name name2 n2.pptname name2 n2.ppt
name name2 n2.ppt
 
Basics of functional Programming by Siddharth Kulkarni
Basics of functional Programming by Siddharth KulkarniBasics of functional Programming by Siddharth Kulkarni
Basics of functional Programming by Siddharth Kulkarni
 
Introduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-UpsIntroduction to Clojure and why it's hot for Sart-Ups
Introduction to Clojure and why it's hot for Sart-Ups
 
Should i Go there
Should i Go thereShould i Go there
Should i Go there
 

Recently uploaded

GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
Neo4j
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
Rohit Gautam
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 

Recently uploaded (20)

GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
GraphSummit Singapore | Enhancing Changi Airport Group's Passenger Experience...
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
Large Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial ApplicationsLarge Language Model (LLM) and it’s Geospatial Applications
Large Language Model (LLM) and it’s Geospatial Applications
 
TrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy SurveyTrustArc Webinar - 2024 Global Privacy Survey
TrustArc Webinar - 2024 Global Privacy Survey
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 

A (very brief) into to Functional Programming

  • 2. WHY FP? • More structured (like goto vs OO) • Clear & concise • Performance, parallelism, concurrency • Reusable, generics • Expand your horizons! • Become a more flexible programmer • Have fun
  • 3. WHAT IS FUNCTIONAL PROGRAMMING • Family of languages • Not one monolithic thing • Collection of attributes & (very) general style
  • 4. TWO SOLUTIONSTOTHE SAME ENTSCHEIDUNGSPROBLEM AlanTuring Alonzo Church Imperative (Instructions) Functional (everything is a function) Global State (“memory”) Stateless (like REST) Mechanistic (Turing Machine) Mathematical (Lambda Calculus)
  • 5. CONSTELLATION OF ATTRIBUTES • Immutability • Recursion • Stateless / explicit state • Higher-order functions • Data types • Type safety • Don’t need all of these! • Some languages enforce these more than others • Can do some FP in traditionally “imperative” languages, too • Java, Ruby, JavaScript, and so on • Lazy evaluation • Composition • Referential transparency • Currying & partial function application • Reusability • Pattern matching
  • 6. APPROACH • Imperative style describes how to do things • Functional style is often declarative • describes what things are • …plus data and transformations between them • Sometimes called “data oriented”
  • 9. IMMUTABILITY, PURITY & RECURSION • Advantages • Get parallelization “for free” because no shared state • No deadlocks or strange state infection bugs • Compact and/or “does the work for you” • Disadvantages • Recursion can be mind bending at first • Recursion may have time or memory space costs
  • 10. IMMUTABILITY MAKES SENSE What you type What the compiler says x = 42 Okay,“x” means 42 x Hmmm… 42! x = 99 What are you? Some kind of liar?
 “x” means 42, obviously! x Hmmm… still 42!
  • 11. MAPPING, FILTERS, FOLDING & ACCUMULATION • Much simpler to reason about & cleaner syntax • Often has a performance advantage # Ruby Fold Example: sum numbers range = (0..99_999_999) ## Imperative/procedural total = 0 i = 0 while i < range.count do total += range[i] i += 1 end total ## Functional range.reduce(:+) • Don’t need to calculate the length of the array • Don’t rely on a throwaway variable ## Roughly def reduce_add(array) return 0 if array == [] array[0] + reduce_add(array[1..-1]) end
  • 12. LIST COMPREHENSIONS & CONSTRAINT PROGRAMMING -- Haskell List Comprehension [(x, y) | x <- [50..100], y <- [1..99], x `mod` 7 == 3, x `mod` y == 42] -- RESULT: [(87,45),(94,52)] -- Haskell List Comprehension: INFINITE EDITION [(x, y) | x <- [50..], y <- [1..99], x `mod` 7 == 3, x `mod` y == 42] -- RESULT: [(87,45),(94,52),(101,59),(108,66),(115,73),(122,80),(129,87), (136,47),(136,94),(150,54),(164,61),(171,43),(178,68),(192,50),(192,75), (206,82),(213,57),(220,89),(234,48),(234,64),(234,96),(255,71),(262,44), (262,55),(276,78),(290,62),(297,51),(297,85),(318,46),(318,69)… …and so on, FOREVER, and while working with the data stream
  • 13. FURTHER, CRAZIER STUFF • Faster custom server • Serves ~2 million requests/second • Triggered a rare Linux flaw due to high throughput • Quantum computing • Massive truly parallel systems • Parallelism “for free” (more or less)
  • 14. FURTHER READING • There is so much more!
 We’ve barely scratched the surface • Vancouver Functional Programmers • LearnYou a Haskell for Great Good • Clojure for the Brave andTrue • What I Wish I Knew Before Learning Haskell • Refactoring Ruby with Monads • The Underscore.js Source • Don’t Fear the Monad • Don’t be Afraid of Functional Programming (SmashingMag)