Awesome
Functional Programming Resources
An archive of the best resources surrounding the functional programming paradigm.
Object Orientation makes code understandable by encapsulating moving parts. Functional programming makes code understandable by minimizing moving parts. ~ Michael Feathers
Functional programming is making a strong comeback, and with it: a massive wave of internet buzz. Someone needs to sort the good from the bad, and that is what I will attempt to do here.
My focus is on the functional paradigm in the context of Javascript, but discussion and highlight of other Functional languages are welcome for the purpose of evaluation, comparison and education.
Note: This is very much a work in progress! Pull Requests welcome!
Keywords
- Higher-Order Functions A fancy term for a function that accepts another function as an argument. source
- Currying Currying is the process of transforming a function that takes multiple arguments into a function that takes just a single argument and returns another function if any arguments are still needed. source
- Pure Functions A pure function is a function that does not introduce any "side effects" by affecting state anywhere in your application. Pure functions return a value, and that is all they do. This keeps your functions clean, predictable, and highly-testable.
- Functors A functor is a container that you can map over; apply a function to its contents & return a new container with results. e.g. JS Arrays.
- Monad In functional programming, a monad is a structure that represents computations defined as sequences of steps: a type with a monad structure defines what it means to chain operations, or nest functions of that type together. This allows the programmer to build pipelines that process data in steps, in which each action is decorated with additional processing rules provided by the monad. source
- Immutable Immutable values are values that never change once instantiated. All variables (symbols) are immutable in functional languages.
- Tail Call A tail call happens when a function
F
makes a function call as its final action. At that pointF
will do absolutely no more work: it passes the ball to whatever function is being called and vanishes from the game. source
Articles
- Functional Programming for the Rest of Us. This article has a really great way of explaining some of the heavier aspects of FP. It breaks out of the typically-academic approach; speaking directly to developers, and not an assembly of mathematicians.
- Don't Be Scared of Functional Programming via Smashing Magazine.
- Why Curry Helps A great article on the beauties of currying.
- The Two Pillars of JavaScript — Pt 2: Functional Programming Nice article by Eric Elliot on the topic of FP.
- Functional Programming in Javascript Cool interactive lessons on FP in JS.
- Promises + FP = Beautiful Streams
- Bringing functional to the frontend: Clojure + ClojureScript for the web
- Om sweet Om: (high-)functional frontend engineering with ClojureScript and React
- Functional programming on frontend with React & ClojureScript
- Functional Programming for JavaScript People
- Master the JavaScript Interview: What is Functional Programming?
- JavaScript and Functional Programming Also includes a nice list of related FP articles.
- The Rise and Fall and Rise of Functional Programming (Composing Software Part 1)
- Why Learn Functional Programming in JavaScript? (Composing Software Part 2)
- Reduce (Composing Software Part 3)
- Currying the Callback or the Essence of Futures
Videos & Presentations
- Simple Made Easy by Rich Hickney (author of Clojure)
- React.js Conf 2015 - Immutable Data and React Not 100% about FP, but has some great tips on immutable data with the react.js library.
- Functional Programming with Generators
- An Introduction to Functional Reactive Programming
- Immutability: Putting The Dream Machine To Work by David Nolen
- Functional Programming with Generators
- What if the user were a function? Great talk by @andrestaltz. Explores reactive / functional programming and more.
- Build Your Own Curry Function in JavaScript
- The Ramda Library: Functional Programming in JavaScript
Video Series: Functional Programming in Javascript
Created by Mattias P Johansson twitter | youtube
- Part 1: Higher-order functions
- Part 2: Map
- Part 3: Reduce Basics
- Part 4: Reduce Advanced
- Part 5: Closures
- Part 6: Currying
- Part 7: Recursion
- Part 8: Promises
Courses (free and paid)
- Javascript: From Fundamentals to Functional JS (Frontend Masters)
- Hardcore Functional Programming in JavaScript (Frontend Masters)
- Introduction to Functional Programming (edX)
Interactive Online Courses
- Learn Rx exercises by Jafar Husain
Benefits of Functional Programming
- Unit Testing No function can ever cause side-effects, there is no need to care about external state, or calling functions in any particular order, all you need to do is pass arguments to your functions that represent your edge cases, this makes unit testing an absolute dream, and your applications can ship with confidence!
- Debugging Debugging a functional program is a breeze. If a function doesn't behave appropriately, just take a peek at your stack, and all the functions and variables that lead you to this point are staring right at you. No need to examine any external state, or investigate who/what/where could have interfered, it's all right in front of you.
- Concurrency The functional programming paradigms offers concurrency as a bonus right out of the box. Nothing in a functional program is ever modified twice by the same thread, let alone by two different threads. There is never any need to use locks, or worry about race conditions. Add as many threads to your app as you want, and your program will keep humming away. Beautiful.
- Hot Code Deployment Ever wish you could update/patch your live application without having to shut the system down first? With FP, it's not a problem!
- Machine-Assisted Proofs & Optimizations Because functional languages share a common root in mathematics, tooling for optimizations, testing, evaluation, etc can be built to further demonstrate the validity of your application. See "Machine Assisted Proofs and Optimizations" for more information.
- Highly-Readable Code FP is an extremely expressive design paradigm, and this expressiveness tends to lend itself to beautifully-crafted programs that are clear and concise.
- Excellent Code Reuse The FP paradigm supports modular, reusable code straight out of the box. This is because functions compose themselves of existing functions, so you get to decide how these functions are combined to perform your application logic.
- Highly-Maintainable Code FP programs are incredibly easy to read, due to the declarative (and modular) nature of functional programming. Additionally: Functional programs also tend to be shorter than their imperative counterparts, and a smaller code base is easier to maintain than a large one.
- Static Types In FP, all 'variables' (immutable symbols) have a type (either declared, or inferred at compile-time). The compiler can then analyse your code and help you avoid a wide range of common problems that plague developers in non statically-typed languages. Note: Javascript is not a statically-typed language, see the "Gotchas" section.
The "Gotchas" of Javascript
Certain aspects of the javascript language make it tricky to harness the full power of a fully-functional language, namely:
- Javascript is not a statically-typed language. Variables in javascript are dynamically-typed, which means that they can change their type at anytime. This is counter to the functional methodology where all symbols are immutable and have an explicit type.
- Variables are only immutable in ES6. All ECMAScript specifications lower than 6 ( >= ES5 ) do not support immutable variables. Note that the
const
keyword is available in ES6, where variables declared likeconst foo = 123;
are immutable, but objects declared withconst
behave differently (see the next gotcha). - In ES6, objects declared with
const
are mutable. The idea here is that the pointer reference to the object is immutable when your object is declared withconst
, but the object itself is mutable.
Libraries & Frameworks
- Ramda A utility library with a focus on flexible functional composition enabled by automatic currying and reversed argument order. Avoids mutating data.
- functionaljs A functional JavaScript library. It facilitates currying and point-free / tacit programming and this methodology has been adhered to from the ground up.
- lodash - A utility library delivering consistency, customization, performance, & extras. A better and faster Underscore.js.
- Mout - Utility library with the biggest difference between other existing solutions is that you can choose to load only the modules/functions that you need, no extra overhead.
- mori - A library for using ClojureScript's persistent data structures and supporting API from the comfort of vanilla JavaScript.
- Folktale - A suite of libraries for generic functional programming in JavaScript that allows you to write elegant, modular applications with fewer bugs, and more reuse.
- immutable - Immutable data collections.
- underscore-contrib - The brass buckles on Underscore's utility belt.
- Bacon.js - Functional reactive programming.
- RxJS - Functional reactive library for transforming, composing, and querying various kinds of data.
- Lazy.js - Utility library similar to lodash/Underscore but with lazy evaluation, which can translate to superior performance in many cases.
Functional Javascript Books
- Eloquent JavaScript Has some chapters dedicated to functional programming. Read for free online, or purchase a paperback copy.
- Programming Javascript Applications : Robust Web Architecture with Node, HTML5, and Modern JS Libraries
- Professor Frisby's Mostly Adequate Guide to Functional Programming (in Javascript)
- Functional Programming in Javascript
- Functional Light JS WIP FP Book in JS by Kyle Simpson
Functional Programming Books, Papers & Articles
Worthwhile publications on FP in languages other than Javascript.
- Real World Haskell
- Learn You a Haskell For Great Good
- Functional Programming in Javascript
- Game programming in Haskell Haskell programmer on the challenges / rewards of game development in Haskell
- Functional Reactive Programming
- Why Functional Programming Matters by John Hughes, 1984 #paper #pdf
- Functional Programming in Swift #iOS
- Living with Lambdas: Functional Programming in C++
- A Practical Introduction to Functional Programming by Mary Rose Cook (Python 2)
- Purely Functional, Declarative Game Logic Using Reactive Programming Using a Pure, Functional Approach to managing complex state in your Functional (Haskell) applications
Resources to be Explored
These are articles and resources that were recommended for one reason or another, but I haven't actually reviewed them to determine their status.
- On the Importance of Purity
- Total Functional Programming
- Swift Functional Programming Tutorial
- Functional Reactive React.js
- Reactive MVC and the Virtual DOM
- Curry or Partial Application?
- The General Theory of Reactivity What is all this talk about reactive? Functional? Promises?
- coderoad-functional-school
Blogs to Follow
Functional Languages -> Compile to Javascript
- Elm-lang.org
- ClosureScript : A closure to JS compiler
Functional UI
Functional Software
Below are examples of Open Source projects using the functional paradigm. Pull requests are welcome for contributions!
FP Lists
Other curated lists of Awesome FP Knowledge