SlideShare a Scribd company logo
Functional Programming
for the advanced beginner
By
Luis Atencio (@luijar)
https://joind.in/talk/d4540
About me (@luijar)
• Author of:
• Functional Programming in JS
• RxJS in Action
• UXDevSummit2018:
• Workshop on FP
• Videos:
• https://www.udemy.com/functional-php-7/
Outline
• Beginner
• FP Primer
• Composition
• Curry
• Advanced
• Functors
• Applicatives
• Monads
• Recursion & pattern matching
• Dealing with effects
JavaScript
“OO-ish” “FP-ish”
“Not Awesome: ES6 Classes”
-Josh Burgess
OOP is dead ONLY if FP is dead
Paradigm subtraction (abstraction)
Paradigm Mass adoption Subtraction
Modular 1970s Limitless files
Structured 1980s Removes “goto”
Object Oriented 1990s Removes function pointers for
polymorphism
Functional ? Takes away assignment and mutable
state
A Primer
Background
• Lambda calculus
• Category Theory
• Categorical properties: identity, composition, and morphisms
(functions)
• Algebraic data types: functors, applicatives, monads
Basic Principles
• A function is a mapping from one type (A) to another (B) within the
same category of objects
• f :: A -> B
• Everything is a function
• Functions are the main unit of work
• Functions are first-class citizens
• Functions are of higher-order
• Favor expressions vs statements
• Avoid externally visible side effects
• Avoid mutable state
Avoid side effects
• Keep changes from leaking
• If you must, move them to the boundaries of your logic
• Manage effects with declarative, lazy containers (e.g. IO monad)
Simplest function possible
Things you’ll hardly see in FP
• Counted loops or while statements
• If-else conditions (replaced by pattern-matching)
• Ternary operator accepted <condition> ? <expression> : <expression>
• Exceptions thrown
• Global variables passed around and/or referenced within functions
• Long functions (5+ lines)
• Void functions
• Variable re-assignment (e.g count += i )
Composition
“Simple ideas combine to form complex ideas”
-John Locke
Composition: control flow in FP
Composition of 2 functions
Example: counting words in a file
Example: reading words from a file2
In: string
Out: number
compose === assemble
Assemble all modules
Curry
Currying is essential for composition
• Partially evaluate functions
• Never evaluate a function with missing data
• Transform multi-arity in to single-arity functions
• Functional languages use auto-currying + lazy evaluation
Simple example
Sequences of partial function calls
Build abstractions over APIs
OR
Functors
What are Functors
• Functions are mappings from one object to another
• Functors are mappings from one category to another
• Most common is the endofunctor (same categories)
• Functors define a function named map (fmap)
What are Functors
• Functions are mappings from one type to another
• Functors are mappings from one context to another
• Most common is the endofunctor (same context)
• Functors define a function named map (fmap)
Well-known functor: Array
Maybe functor Rudimentary pattern-matching
Workings of Maybe Functor
Functor/Composition equivalence
• Define map for function types
• Equivalence: map can be derived from compose
Applicatives
What is an Applicative (functor)
• Functor with extra laws and operations
• Not just wrap values, wrap functions too!
• Implements the Apply interface: ap
Maybe Applicative object
Workings of an applicative
Applicative examples
Monads
Applicative + Functor + Effects
What is a monad
• Functors apply a function to a wrapped value
• Applicatives apply a wrapped function to a wrapped value
• Monads apply a function that returns a wrapped value to another
wrapped value. Enter flatMap (bind, chain)
Maybe Example: object traversal
Maybe Example: object traversal2
Maybe Example3
Back to composition
Point-free coding
What about flatMap?
A few more monads in the wild
• Algebraic Data Types:
• Either, Maybe, Try, Validation (sum type)
• Tuples, Records, State (product type)
• State management
• Do, State, IO
• Async
• Promise
• Observable
• Dependency Injection
• Reader
• Many more...
Recursion & Pattern
Matching
Imperative code
List Sum
List insert
Dealing with effects
IO: Monads as computations with effects
Keep effects at the boundaries
Keep effects at the boundaries
Parting Thoughts
• FP makes code easier to reason
about
• FP is closer to the maths and
propositional logic
• No need for patterns!

More Related Content

PDF
Functional programming with Java 8
PDF
Functional Programming in Java
PDF
Lambdas in Java 8
PPTX
Functional programming principles and Java 8
PDF
Simplicitly
PDF
Functional programming in scala
PPTX
Functional programming with Java 8
PPTX
2CPP04 - Objects and Classes
Functional programming with Java 8
Functional Programming in Java
Lambdas in Java 8
Functional programming principles and Java 8
Simplicitly
Functional programming in scala
Functional programming with Java 8
2CPP04 - Objects and Classes

What's hot (20)

PDF
What To Leave Implicit
PPTX
Flow control in Python
PPTX
Introduction to Python Programming
PPTX
Not Everything Is An Object
PDF
Functional programming
PDF
Preparing for Scala 3
PPTX
What To Leave Implicit
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
PDF
Swift, a quick overview
PPT
JavaScript Introductin to Functions
PPTX
cpp-2013 #9 STL Algorithms Part 1
PPTX
Introduction to functional programming with java 8
PDF
Advanced Reflection in Pharo
PPTX
Adobe Flash Actionscript language basics chapter-2
PPT
Functional OOP, Clojure style
PDF
Intro to JavaScript - Week 2: Function
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
PPTX
Break Free with Managed Functional Programming: An Introduction to F#
PPTX
05 functional programming
PDF
Why should a Java programmer shifts towards Functional Programming Paradigm
What To Leave Implicit
Flow control in Python
Introduction to Python Programming
Not Everything Is An Object
Functional programming
Preparing for Scala 3
What To Leave Implicit
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Swift, a quick overview
JavaScript Introductin to Functions
cpp-2013 #9 STL Algorithms Part 1
Introduction to functional programming with java 8
Advanced Reflection in Pharo
Adobe Flash Actionscript language basics chapter-2
Functional OOP, Clojure style
Intro to JavaScript - Week 2: Function
Break Free with Managed Functional Programming: An Introduction to F#
Break Free with Managed Functional Programming: An Introduction to F#
05 functional programming
Why should a Java programmer shifts towards Functional Programming Paradigm
Ad

Similar to Functional programming for the Advanced Beginner (20)

PPTX
Functions
PPTX
PHP = PHunctional Programming
PPTX
Polymorphism
PPTX
Functional Programming.pptx
PPT
Basics of cpp
PDF
Booting into functional programming
PDF
Java 8 - Project Lambda
PPTX
Funtional Programming
PPTX
Functional programming in python
PDF
Functional programming in python
PPTX
Iterator - a powerful but underappreciated design pattern
PDF
Functional Programming #FTW
PPTX
Intro to java 8
PPTX
Lecture 5 Functions all about business categories
PPTX
The Road to Lambda - Mike Duigou
PPTX
Lambda Expressions Java 8 Features usage
PPTX
The joy of functional programming
PPTX
Основы функционального JS
KEY
Programming with Python - Week 3
PDF
The Scheme Language -- Using it on the iPhone
Functions
PHP = PHunctional Programming
Polymorphism
Functional Programming.pptx
Basics of cpp
Booting into functional programming
Java 8 - Project Lambda
Funtional Programming
Functional programming in python
Functional programming in python
Iterator - a powerful but underappreciated design pattern
Functional Programming #FTW
Intro to java 8
Lecture 5 Functions all about business categories
The Road to Lambda - Mike Duigou
Lambda Expressions Java 8 Features usage
The joy of functional programming
Основы функционального JS
Programming with Python - Week 3
The Scheme Language -- Using it on the iPhone
Ad

More from Luis Atencio (7)

PDF
379008-rc217-functionalprogramming
PDF
DZone_RC_RxJS
PPTX
Luis Atencio on RxJS
PPTX
Thinking Functionally with JavaScript
PPTX
Functional Programming in JavaScript by Luis Atencio
PPTX
Java script Techniques Part I
PPTX
Luis atencio on_git
379008-rc217-functionalprogramming
DZone_RC_RxJS
Luis Atencio on RxJS
Thinking Functionally with JavaScript
Functional Programming in JavaScript by Luis Atencio
Java script Techniques Part I
Luis atencio on_git

Recently uploaded (20)

PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Introduction to Artificial Intelligence
PPTX
Transform Your Business with a Software ERP System
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Understanding Forklifts - TECH EHS Solution
PPT
Introduction Database Management System for Course Database
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PPTX
ai tools demonstartion for schools and inter college
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
How to Choose the Right IT Partner for Your Business in Malaysia
Softaken Excel to vCard Converter Software.pdf
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Which alternative to Crystal Reports is best for small or large businesses.pdf
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Upgrade and Innovation Strategies for SAP ERP Customers
Introduction to Artificial Intelligence
Transform Your Business with a Software ERP System
Online Work Permit System for Fast Permit Processing
Understanding Forklifts - TECH EHS Solution
Introduction Database Management System for Course Database
Operating system designcfffgfgggggggvggggggggg
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
ai tools demonstartion for schools and inter college
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Odoo Companies in India – Driving Business Transformation.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Nekopoi APK 2025 free lastest update

Functional programming for the Advanced Beginner

Editor's Notes

  • #5: Use Ramda to bring FP primitives into the language JS has lambdas and higher order functions JS has support for objects and classes (limited)
  • #6: https://medium.com/@dan_abramov/how-to-use-classes-and-sleep-at-night-9af8de78ccb4 https://github.com/joshburgess/not-awesome-es6-classes/
  • #13: Endofunction -> a function with equal domain and codomain
  • #22: Millennium Facon
  • #30: Slide switches “category” for “context”
  • #32: Used for error handling