Skip to content

Instantly share code, notes, and snippets.

@bionicbrian
Created December 12, 2016 15:00
Show Gist options
  • Save bionicbrian/89e3d2bdcac77202d512f13f5f84551e to your computer and use it in GitHub Desktop.
Save bionicbrian/89e3d2bdcac77202d512f13f5f84551e to your computer and use it in GitHub Desktop.
Crazy Functional JS Things

This is a draft of stuff related to the weird and wonderful world of modern functional JavaScript, and will probably change over time.

This might serve as an entry point for anyone interested in exploring functional programming in JS who is just starting out. And I have a lot to learn, so I would love to geek out with anyone else who's interested in this stuff too.

Functional JavaScript, the Book

There is a great book appropriately named Functional JavaScript that dives into functional concepts using underscore. It goes a long way with just functions (no weird things like monads or types). This is a great place for people to get started with functional programming in JavaScript.

Ramda

See my "A Case for Ramda" for more on what I think about Ramda and why it's rad. Or even better, "Why Ramda?".

The Docs for Ramda Looks Weird

You might look at the docs for filter and see Filterable f => (a → Boolean) → f a → f a and think "What are all those funny arrows about?" And you wouldn't be alone. Some people think it's getting TOO weird. But it's Hindley-Milner! It's a compact and expressive way to describe what a function does. Because even though one might have only ever used map and filter on arrays, they can be used for much more, and this notation helps describe that.

For some, words like "Functor", "Filterable", "tranducer", "lens" are new. They're common terms to describe common ideas in programming in a functional style.

Typed-language Fantasy Land

Writing about the Promises/A+ spec, well-known functional programmer Brian McKenna tried "to show how category theory can give us a much simpler, more generalised and lawful API" and said, "Recognising that promises are monadic gives us an API which allows us to derive lots of useful functions."

When brought up as introducing category theory in the spec for Promises, spec author Domenic Denicola commented, "it totally ignores reality in favor of typed-language fantasy land."

The very next day Brian McKenna pushed the first commit to the Fantasy Land spec, a "Specification for interoperability of common algebraic structures in JavaScript."

What the heck is Fantasy Land in the real world?

It means that if you use a library and they are Fantasy Land-compatible, those functions will dispatch to a custom implementation of a method like ["fantasy-land/map"] on custom data types, and as long the result obeys some laws, it will compose and work with other libraries adhering to the spec. For example, you could map the same generalized function (or composition of functions) over an array, Promises, Tasks, streams and observables, etc.

Example of what could possible from a popular lodash issue:

var numbers = Promise.all([1,2,3,4])

var f = R.pipe(
  R.map(R.multiply(2))
  ,R.filter( R.gt( R.__, 4))
  ,R.sum // dispatches to Promise::reduce
)
await f(numbers) //=> 14

f([1,2,3,4]) //=> 14

Fantasy Land Library Examples

Brian Lonsdorf

Brian Lonsdorf is a weird dude. And he's a prolific and friendly teacher of functional programming in JavaScript. He teaches functional in a way that is both easy to understand and makes you feel like you're on LSD. His Mostly Adequate Guide to Functional Programming is pretty great. His original Classroom Coding series of videos is good, and has seen a recent update on egghead.io.

Modern Functional Compile-to-JS Languages

ClojureScript and Om

ClojureScript has been around for years and Om came out almost immediately after React did. There are many tools and libraries under active development, and many of the ideas from ClojureScript find their way into JavaScript. There are a lot of talks and tutorials for Om and CLJS.

Elm

Elm is a functional programming language in the "ML family", is part of the "prior art" of Redux, uses a virtual-dom like React and is statically typed -- which means the compiler helps you get it right as you write your code. There are many talks online about Elm, and a video course from Pragmatic Studio.

PureScript

PureScript is similar to Elm but is considered "the next step" from Elm. The community around PureScript is growing incredibly fast. Libraries like Thermite and Pux provide wrappers around React. There are many talks online about PureScript.

Down the Rabbit Hole

There is a lot going on here, but this just scratches the surface. If some of it looks crazy, it's probably just new. I think these techniques and libraries can be powerful, are based on rules and logic, are built up in small pieces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment