
Functional JavaScript First Steps
Learning Paths:
Topics:
Table of Contents
Introduction
What is Functional Programming
What is Functional Programming
Anjana describes functional programming as having a specific mindset when writing code and compares it to other paradigms like imperative programming or object-oriented programming. A core concept of functional programming is the use of pure functions which are functions that do not produce side effects.Pure vs Impure Functions
Anjana compares two functions to highlight the differences between pure and impure functions. A pure function is deterministic which means when given a specific input, the output will always be the same.Why Functional JavaScript
Anjana explains that functional programming is more predictable and easier to test and debug. Using functional programming in JavaScript can be an easier alternative to object-oriented techniques or prototypical inheritance.Side Effects
Anjana demonstrates how to remove side effects from an impure function. Side effects are produced any time a function depends on or manipulates data outside of itself. Pure functions do nothing but return output based on an input.Pure Functions Exercise
Students are instructed to decide whether each function is pure or impure.Pure Functions Solution
Anjana demonstrates the solution to the Pure Functions exercise.
Staying out of the Loop with Recursion
Recursion
Anjana explains iterative loops are not a part of functional programming and demonstrates how a for loop can be refactored to use recursion. When a function is recursive, it continues to call itself until a non-recursive base case is reached.Iteration vs Recursion Exercise
Students are instructed to compare iterative and recursive functions for their readability, writability, and performance.Iteration vs Recursion Solution
Anjana demonstrates the solution to the Iteration vs Recursion exercise.Recursion Performance & Proper Tail Calls
Anjana discusses the performance implications of recursive functions. Deep recursive stacks can lead to memory issues and stack overflow errors. Tail call optimization can address these issues but is not implemented in all JavaScript runtime environments.
HIgher Order Functions
filter, map, & reduce
Anjana explains that higher order functions receive another function as an input or output. JavaScript Array methods like map, reduce, and filter are useful high order functions in functional programming because they replace the need for iteration.filter, map, & reduce Exercise
Students are instructed create predicate functions that can be used with the filter function, implement a map function, and create "sum" and "max" reducer functions.filter, map, & reduce Solution: filter
Anjana demonstrates the solution to the filter, map & reduce exercise. This segment covers implementing predicate functions for the filter function.filter, map, & reduce Solution: map
Anjana demonstrates the solution to the filter, map & reduce exercise. This segment covers implementing the map function.filter, map, & reduce Solution: reduce
Anjana demonstrates the solution to the filter, map & reduce exercise. This segment covers implementing the sum and max reducer functions.
Closure
Closure
Anjana describes that closure occurs when a function defined inside another function retains access to scope from the outer function's body. This allows the inner function to access data that was not explicitly passed to it as an argument.Partial Application & Currying
Anjana explains partially applied functions have some arguments "locked in" by using a closure. Currying is the process of refactoring a single function with multiple arguments to a series of partially applied functions.Closure & Currying Exercise
Students are instructed to create two functions that return closures and curry another function to make it more reusable.Closure & Currying Solution
Anjana demonstrates the solution to the Closure & Currying exercise.
Function Composition
Function Composition
Anjana explains how functional programers use functional composition to create full programs. One function's output becomes the input for another function. The program itself is a function composed from other smaller functions.Function Composition Exercise
Students are instructed to write a pipeline function that receives a series of functions as input and then use the pipeline technique to convert strings from snake_case to camelCase.Function Composition Solution
Anjana demonstrates the solution to the Function Composition exercise.Immutability
Anjana demonstrates how to use the map function to copy an array to prevent mutation. Although the new copy has prevented mutating the original Array, creating copies can increase the amount of memory required to run a program. Immutable or persistent data structures allow unchanged parts of the data to be reused throughout a program.Avoiding Mutation Exercise
Students are instructed to rewrite the mutable push and pop Array methods and experiment with the Immutable.js and Immer libraries.Avoiding Mutation Solution
Anjana demonstrates the solution to the Avoiding Mutation exercise.