First Steps into Functional Programming
A super fun(ctional) programming workshop by @AnjanaVakil (slides)
Why am I here?
You may have found yourself hearing more and more about this trendy topic of “functional programming", from a bunch of excited coders enthusiastically gushing about how it helps them write elegant, modular code that's easier to test, debug, and reason about. If you're like me, you may have started wondering:
- What is functional programming anyway, and why are people so excited about it?
- What does functional code look like? How is it different from imperative or OO code?
- What tools do I need to write functional programs? Do I have to learn Haskell? (spoiler alert: no!)
If you too have been asking yourself questions like these, you're in the right place! If you're a functional programming pro or have zero interest in learning a new paradigm, then this probably isn't the workshop for you.
What will we learn in this workshop?
This will be a friendly, practical exploration of functional programming fundamentals.
We'll learn how to follow the basic principles of the functional programming paradigm, such as:
- construct programs from pure, “input in-output out” functions through which data flows
- use higher-order functions like map & filter instead of iterative loops, and closures instead of objects & classes
- avoid mutation (changing things in-place), and use immutable data structures for efficiency
How do I use this repository?
The examples in this repository are written in Node.js, and exercise instructions will assume that you're working in Node.
- Install Node (version 6 or 8 recommended), if you don't already have it - I recommend installing via NVM (Node Version Manager). Installing Node also installs the package manager npm.
- Clone the workshop repository:
git clone https://github.com/vakila/functional-workshop.git
- Feel free to fork the repo first, and track your work in your own fork!
- If you don't use GitHub, no problem: download a zip file of the repository instead, and unzip it.
- Change into the repository directory and install the project dependencies:
$ cd functional-workshop $ npm install
- Follow along with the exercises in each section's
readme.md
, linked in the schedule below.
Do I have to use Node?
Not necessarily! If you prefer to use client-side JavaScript, that's also fine: you can follow along with the exercises by copy-pasting the code samples into your browser's web console. For the closest similarity to Node.js, you may want to use Chrome.
What should I do if I get confused?
If you are struggling to wrap your head around a functional concept, that means you're completely normal and doing things right! But to keep things from getting too frustrating:
- Ask questions at any time - there are no dumb questions!
- Read up on the topic with the resources listed below. You might find an explanation or example that makes it click for you.
- Ask the facilitator for additional exercises you can do to practice working with the concept
What should I do if I get bored?
If the workshop is moving too slowly for you or you're already familiar with some concept(s) we're working on, no worries! Take your learning into your own hands:
- Work on solving small programming problems in a functional style. Find a problem from Exercism, Daily Programmer, or a similar site you like, and force yourself to solve it functionally.
- Feel like reading rather than coding? Dive deeper into the topic(s) we're covering by checking out some of the resources listed below.
Outline & Schedule
9:00-9:30 Intro & icebreakers
9:30-10:30 Functional Programming in a nutshell
- Pure Functions vs. Side Effects
- Time, state, and (in)sanity
- Imperative vs. Declarative programming
10:30-10:45 Coffee break
10:45-11:15 Staying out of the loop with Recursion
- Iteration vs. Recursion
- Recursive function basics
- The problem with the problem with the problem with recursion
And now, a word from our sponsors: Tail Call Optimization
11:30-12:00 Flying first-class with higher-order functions
- Functions as values
- Filter, Map, Reduce
12:00-13:00 Lunch
13:00-13:45 Getting Closure
- Closures
- Currying
- FP & OOP: BFFs
And now, a word from our sponsors: The Lambda Calculus
13:45-14:00 FOOP demo & discussion
14:00-14:30 It's functions all the way down
- Function composition
- Data flow
- Pipelining
14:30-14:45 Coffee break
14:45-15:30 Immutability and you
- Time, state, and (in)sanity, revisited
- Copying vs. Mutating
- The pitfalls of immutability
And now, a word from our sponsors: Persistent Data Structures
15:30-16:00 Recap & takeaways (discussion)
- Review: Thinking functionally
- When is FP the right choice?
- Libraries & languages to try
16:00 End
References & Further Reading
FP Principles & Concepts
- Mary Rose Cook, "A Practical Introduction to Functional Programming"
- Professor Frisby's Mostly Adequate Guide to Functional Programming
- Anouk Ruhaak, Get started with Functional Programming, otsconf 2015
- J. Kerr, “Functional Principles for OO Development”, GOTO Chicago '14
- B. Stokke, "The Miracle of Generators", GOTO Chicago '16
- Code Words by the Recurse Center
- Mary Rose Cook, "An introduction to functional programming"
- Sal Becker, "This just isn't functional"
- Patrick Dubroy, "Immutability is not enough"
- James Hague
- Eric Normand, LispCast
Closures & Currying
- Scott Sauyet
- Hugh FD Jackson
- Todd Motto, Everything you wanted to know about JavaScript scope
Immutable data structures
- D. Nolen, “Immutability, interactivity & JS”, FutureJS '14
- L. Byron, “Immutable Data & React”, React.js Conf '15
- A. Vakil, "Immutable data structures for functional JS", JSConf EU '17
- J.N. L'orange, Understanding Clojure's Persistent Data Structures
Messaging & OOP
- Alan Kay:
- William Cook:
- Structure & Interpretation of Computer Programs
Libraries & Languages to try
Functional JavaScript Libraries
- [FP utilities] Ramda (also available in Українська (Ukrainian)!)
- [FP utilities] Lodash
- [Persistent Data Structures] Immutable.js
- [Persistent Data Structures] Seamless-Immutable
- [FP utilities, Persistent Data Structures] Mori
Functional Languages
- [frontend] Elm
- [frontend] ClojureScript
- [backend] Clojure
- [backend] Elixir