This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.
Table of Contents
Pure Functions
Course Introduction
Kyle Simpson (@getify) begins his course on Functional Lite Programming with a brief introduction of himself. He also shares a few of his more popular open source projects like LabJS, Grips, and Asynquence.Course Agenda
Before jumping into the course, Kyle walks through the agenda. He will begin with higher-level concepts like Composition, Immutability, and Closures. Then the course will move into list-specific operations including the use of map(), filter(), reduce, and forEach().Side Effects
Pure functions are at the heart of functional programming. The first step in learning how to write pure functions is to understand what makes a function impure or have side effects. For example, a function that alters variables outside of its scope is an impure function.Exercise 1
In this exercise, you will create a pure function named “bar” to wrap around the function “foo”.Exercise 1 Solution Part 1
Kyle walks through the solution to exercise 1.Exercise 1 Solution Part 2
Now that Kyle has finished demonstrating the solution to exercise 1, he spends a few minutes answering some audience questions.
Composition & Immutability
Manual Composition
Composition involves taking the output from one function and directly inputting it into another function. Kyle demonstrates how composition can be created manually by calling a function within the parameter list of another function. He also shows how to create a manual composition by writing a separate function.Composition Utility
Manually creating compositions can be cumbersome if there are many situations where functions need to be combined. Kyle explains how to create a compose2() function that will take two functions as arguments and return a composition.Immutability
To a functional programmer, immutability refers to a variables ability to be changed. Kyle illustrates this by explaining the difference between using the “const” keyword and using Object.freeze().Questions on Immutability
Kyle spends a few minutes answering audience questions on Immutability. He also talks about implementing a function that returns a copy of an Array as a way to remain pure while working with Array values.
Closure & Recursion
Closure
Closure is when a function “remembers” the variables around it even when that function is executed elsewhere. While using closure is a very powerful technique, side-effects can be produced. Kyle demonstrates this and explains how closure can be written in a more pure way.Exercise 2
In this exercise, you will use closure to define a foo() function that returns a function. This returned function remembers only the first two arguments that were passed to foo() and always adds them together.Exercise 2 Solution
Kyle walks through the solution to exercise 2.Recursion
Recursion occurs when a function calls itself to perform an operation. A recursive function will continue to call itself until a base case is reached. Kyle introduces this concept and shares some thoughts on its perceived complexity.Recursive Example
Using a function that sums three numbers as an example, Kyle demonstrates how to take a non-recursive operation and add recursion. He also shows a cleaner version of the recursive function which utilizes ES6 syntax.Exercise 3
In this exercise, you will turn the mult() function into a recursive function that can work on as many arguments as necessary.Exercise 3 Solution
Kyle walks through the solution to exercise 3.
List Operations
List Transformation
Transforming a list involves performing an operation on every value in that list. Kyle introduces the Array.map() method and explains how it can be used to create immutable list transformations. The new list of transformed values has a one-to-one “mapping” with the original list.List Exclusion
The Array.filter() method is used to create list exclusions. Each item in the list is passed to a function which returns a boolean value representing whether that item should be included in the list or not. A new list of these filtered items is returned, leaving the original list unmodified.List Composition
List compositions are created using the Array.reduce() method. Just like the map() method, reduce() iterates through a list performing a transformation on each value. The difference is an initial value is also passed which the items are composed onto creating a smaller, “reduced” list or even a single return value.List Iteration
List iteration is performed by using the Arrach.forEach() method to loop through list items. Function programmers typically avoid this method since most list iteration use-cases include side effects. Kyle spends a few minutes discussing the list iteration implementation and explains this difference between using forEach() and map().Exercise 4
In this multi-part exercise, you will combine a number of the concepts learned throughout this course. Before starting the exercise, Kyle shares a few observations about the process of learning functional programming.Exercise 4 Solution Part 1
Kyle begins walking through the solution to exercise 4. First he creates an add() function that will add two numbers. He then creates an add2() function that takes two functions instead of two numbers, calls each function, and sends those values to the add() function.Exercise 4 Solution Part 2
Kyle continues his demonstration of the solution to exercise 4. He creates an addn() function that takes an array of values and adds them together using the add2() function.Exercise 4 Solution Part 3
Building off the previous solution, Kyle changes the addn() function to now work with an array of functions instead of an array of values. This allows him to incorporate the reduce() method.Exercise 4 Solution Part 4
Kyle spends a few minutes answering audience questions. He then continues the solution to exercise 4 by using the map() method so an array of individual values can be passed to addn() as an array of functions.Exercise 4 Solution Part 5
The last step in the exercise 4 solution is to incorporate the filter() method so only odd or even values can be passed to addn().Wrap Up
Kyle wraps up the course with a review of the “Functional Lite” concepts he covered and some of his own thoughts on functional programming in general.