This course has been updated! We now recommend you take the Functional-Light JavaScript, v3 course.
Table of Contents
Functional JavaScript v2
Functional Programming Introduction
Functional Programming
Kyle discusses why learning Functional Programming (FP) is essential to becoming a better programmer. FP is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. Functional programming is declarative rather than imperative, and application state flows through pure functions. Declarative code focuses on what happens while Imperative code is focusing how something should happen.Provable and Readable
Kyle explains that writing code that you don't understand is code that you cannot trust and vice versa. By using Functional Programming through mathematically proven patterns, programmers can trust their code. Kyle takes questions from students about abstraction.Pure Functions and Side-Effects
Underscoring pure functions are at the heart of functional programming, Kyle states that 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.Purifying Functions
Kyle reviews how to purify a function removing side effects, but the tradeoffs include pragmatic approach.Challenge 1: Purify a Function
In this challenge, students purify a function by creating a pure function named “bar” to wrap around the function “foo.”Challenge 1: Solution
Kyle walks through the solution to Challenge 1.Evolving Understanding of Impurity
After the exercise, Kyle continues to explore the potential pitfalls of coding pure functions and judging its purity relies on the context of the application.
Managing Function Inputs
Arguments
The order of arguments matter, Spreading PropertiesNo Points
Kyle reviews tacit programming or "point-free" style as it is commonly called. The term "point" refers to a function's parameter.Challenge 2: Point-Free Style
In this challenge, students refactor functions into point-free style.Challenge 2: Solution
Kyle walks through the solution to Challenge 2.
Composing Functions
Composition Introduction
Functions come in different shapes and sizes, Kyle illustrates how to combine functions to make a new compound function as a utlitilty in various parts of a program. This method of using functions in this manner is called composition.Challenge 3: Compose and Pipe Utility
In this challenge, students code their own compose and pipe utilitiy.Challenge 3: Solution
Kyle walks through the solution to Challenge 3.
Immutability
Immutability Introduction
To a functional programmer, Kyle explains that immutability refers to a variable's ability to be changed. Kyle illustrates this by explaining the difference between using the “const” keyword and using Object.freeze().Challenge 4: Compose and Pipe
In this challenge, students code their own compose and pipe utilitiy.Challenge 4: Solution
Kyle walks through the solution to Challenge 4.
Closure
Closure and Side Effects
Kyle reviews closure, which is when a function "remembers" the variables around it even when that function is executed elsewhere. While using closure is a very powerful technique, Kyle demonstrates that side-effects can be produced and how closure can be written in a more pure way.Challenge 5: Purifying Closure
In this challenge, students rewrite a closure to be more pure.Challenge 5: Solution
Kyle walks through the solution to Challenge 5 and takes questions from students.
Partial Application
Recursion
Recursion Introduction
Kyle introduces the concept of recursion, which is when a function calls itself to perform an operation and it will continue to call itself until a base case is reached.Challenge 6: Recursive Operation
In this challenge, students take create a recursive operation.Challenge 6: Solution
Kyle walks through the solution to Challenge 6.Proper Tail Calls
Kyle reviews Proper Tail Calls (PTC). A tail call occurs when a function calls another as its last action, so it has nothing else to do. A PTC does not need any extra stack space when doing a tail call.Continuation Passing Style
Kyle illustrates Continuation Passing Style (CPS), which is organizing code so that each function receives another function to execute at its end.Trampolines
In demonstrating trampolines, Kyle shows that CPS-like continuations are created, but instead of passed in, they are shallowly returned.
Data Structures
List Transformations
Kyle introduces the Array.map() method and explains how it can be used to create immutable list transformations. Transforming a list involves performing an operation on every value in that list. The new list of transformed values has a one-to-one “mapping” with the original list.Filter: Exclusion
Kyle reviews the Array.filter() as a method 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.Reduce: Combining
Kyle introduces Array.reduce() method as a combiner. 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.Challenge 7: Culmination Exercise
In this challenge, students work through a number of the concepts taught throughout this course. Before starting the exercise, Kyle shares a few observations about the process of learning functional programming.Challenge 7: Solution
Kyle begins walking through the solution by creating an addn() function that takes an array of values and adds them together using the add2() function.Challenge 7: Solution 2
Building off the previous solution, Kyle changes the addn() function to now work with an array of functions instead of an array of values creating an appraoch that allows the incorporation of the reduce() method. - Tthere's a horn that starts at 04:53:33:20 and lasts till 04:58:35:13Challenge 7: Solution 3
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().Fusion
Kyle introduces the concept of fusion, which deals with combining adjacent operators to reduce the number of times the list is iterated over.Transducing
Kyle shows how to create transducers, which are composable and efficient data transformation functions that do not create intermediate collections.
Data Structure Operations
Data Structure Operations Introduction
Kyle reviews lists, which are typically visualized as arrays, but can be generalized as any data structure that represents/produces an ordered collection of values. As such, all these "list operations" are actually "data structure operations."Challenge 8: Culmination Exercise 2
In this challenge, students take all the concepts discussed in the course and use them to solve a coding exercise.Challenge 8: Solution
Kyle walks through the solution to Challenge 8 and takes questions from students.
Functional Programing Utility
Async Programming
Lazy Arrays
Kyle illustrates that "eager" and "lazy" are ways to describe whether an operation will finish right away or progress over time. A "lazy array" is an array where the values will come in over time.Challenge 9: Observables
In this challenge, students program a countdown timer using observables.Challenge 9: Solution
Kyle walks through the solution to Challenge 9 and takes questions from students.