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!

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