– https://en.wikipedia.org/wiki/Kleisli_category
“In category theory, a Kleisli category is
a category naturally associated to any monad T. It is
equivalent to the category of free T-algebras. The Kleisli
category is one of two extremal solutions to the
question ‘Does every monad arise from an adjunction?’”
– https://en.wikipedia.org/wiki/Kleisli_category
“In category theory, a Kleisli category is
a category naturally associated to any monad T. It is
equivalent to the category of free T-algebras. The Kleisli
category is one of two extremal solutions to the
question ‘Does every monad arise from an adjunction?’”
Practical Functional
Programming In JavaScript
Ian Thomas (@anatomic) | December 2017
–Paul Graham
“Object-oriented programming offers a sustainable way
to write spaghetti code. It lets you accrete programs as
a series of patches.”
Why JS? ?
-Crockford’s Law (aka The Monadic Curse)
“The Monadic Curse is that once
someone learns what Monads are
and how to use them, they lose
the ability to explain them to
other people”
Why Functional
Programming?
–Edsger Dijkstra
“Program testing can be a very effective way to show
the presence of bugs, but is hopelessly inadequate for
showing their absence.”
–Simon Peyton Jones
“The most depressing thing about life as a programmer, I
think, is if you’re faced with a chunk of code that either
someone else wrote or, worse still, you wrote yourself
but no longer dare to modify. That’s depressing.”
What is a function?
A function relates each element of a set
with exactly one element of another set
(possibly the same set).
What is a higher-order
function?
A higher-order function is a function which operates
on another functions, either by taking them as
arguments or returning them
Some principals of
functional programming
Purity
Purity
or, perhaps, why should we avoid impurity?
A pure function is a function that, given the same input,
will always return the same output and does not have
any observable side effect.
How many tests would you require to adequately check this function?
Would it be more than 6?
Immutability
Immutability goes hand in hand with purity. Returning
copies of data and not manipulating global state means
you can guarantee certain properties and rules will
always hold in your programs.
Composition
–Ellen Ullman
“We build our computer (systems) the way we build our
cities: over time, without a plan, on top of ruins.”
Imagine if you could just build your application by putting
together well tested, easily understood pieces of code in a
declarative manner. Just like building with lego blocks,
composition of pure functions unlocks this ability.
https://drboolean.gitbooks.io/mostly-adequate-guide/ch5.html#category-theory
Currying
Currying is the process of converting a function that
takes multiple arguments into a function that can
take them one at a time
Pointfree Style
“Pointfree is a good litmus test for functional code as it lets us know we've got
small functions that take input to output. One can't compose a while loop, for
instance. Be warned, however, pointfree is a double-edged sword and can
sometimes obfuscate intention. Not all functional code is pointfree and that is
O.K. We'll shoot for it where we can and stick with normal functions otherwise.”
–Professor Frisby’s Mostly Adequate Guide To Functional Programming
Algebraic Data Types
Algebraic Data Types
–Greg Nelson
“To keep large programs well structured, you either
need superhuman will power, or proper language
support for interfaces.”
https://github.com/fantasyland/fantasy-land
https://github.com/fantasyland/fantasy-land
https://github.com/fantasyland/fantasy-land
https://github.com/fantasyland/fantasy-land
Better known as….
Array (Functor)
Maybe (Monad)
Either (Monad)
Task/Future (Monad)
Reader (Monad)
State (Monad)
Product (Monoid)
Sum (Monoid)
First (Monoid)
Any (Monoid)
All (Monoid)
Max (Monoid)
Min (Monoid)
Etc.
While the mathematical descriptions and terminology
will ultimately prove useful, in many cases knowing
some basic laws is enough.
Identity
u.map(x => x) === u
Associativity
a.concat(b.concat(c)) === a.concat(b).concat(c)
Composition
u.map(x => f(g(x))) === u.map(g).map(f)
Stuff I haven’t
covered…
Stuff I haven’t
covered…
Transducers
Observables
Lenses
Foldable
Referential Transparency
Applicative
Traversable Isomorphism
Property based testing
Practical Example 1
Creating a pure http client
Wrap a promise based API using a Future
Use Reader to create an API which can ask for config
Use Reader pattern to create an API which can ask for config
Compose our application run with config
Practical Example 2
Generating RequestABet™ Recommendations
Customer selects bets
Request is sent to traders
Nearest matches are calculated
Top 4 recommendations shown
Given this as an API Response
Encapsulate business logic in pure functions
Separate out state based operations
Compose functionality to produce pipelines of execution
Pure Validation
Add some monads to handle IO and control flow
Demo
–Tom Cargill
“The first 90 percent of the code accounts for the first
90 percent of the development time. The remaining 10
percent of the code accounts for the other 90 percent
of the development time.”
Ian Thomas (@anatomic) | December 2017
Practical Functional
Programming In JavaScript
https://drboolean.gitbooks.io/mostly-adequate-guide/
https://github.com/getify/Functional-Light-JS
https://medium.com/@lunde.andrews/easing-into-functional-programming-in-javascript-30056bcc49a9
https://fp-bingotron.ian-thomas.net/
http://www.tomharding.me/
https://github.com/fantasyland/fantasy-land
https://staltz.com/is-your-javascript-function-actually-pure.html
https://egghead.io/courses/quickly-transform-data-with-transducers
https://egghead.io/courses/professor-frisby-introduces-composable-functional-javascript
https://www.youtube.com/watch?v=moAfgDFVLUs

Practical functional programming in JavaScript for the non-mathematician