Salesforce
Course Description
Learn to apply techniques from the forefront of computer science research to solve practical problems in JavaScript. Discover functional programming concepts like pure functions, currying, composition, functors, and monads. You’ll see all these techniques in action, including the Either and Task monads, in a real-world application fetching weather data. Prepare to change the way you write and think about front-end code forever!
This course and others like it are available as part of our Frontend Masters video subscription.
Preview
CloseWhat They're Saying
I just completed "Hardcore Functional Programming in JavaScript, v2" by Brian Lonsdorf on Frontend Masters! This really opened my eyes about Functional Programming is not as scary as it looks.
![Marko](https://pbs.twimg.com/profile_images/737956633767227392/Fm5BMCxL.jpg)
Marko
ultrox
Course Details
Published: March 17, 2020
Learning Paths
Topics
Learn Straight from the Experts Who Shape the Modern Web
Your Path to Senior Developer and Beyond
- 200+ In-depth courses
- 18 Learning Paths
- Industry Leading Experts
- Live Interactive Workshops
Table of Contents
Introduction
Section Duration: 15 minutes
- Brian introduces the course, and explains what the intended audience is.
- Brian explores three small tests to determine if one is writing a function: the object needs to be total, have an input for every output, be deterministic, and have no observable side effects.
- Brian reviews the checklist of what makes a mathematical, pure function, and asks the audience to determine if the examples presented are pure functions or not.
- Brian explains how the mathematics used in functions can be useful when programming. Pure functions are reliable, portable, reusable, testable, and composable.
Currying
Section Duration: 30 minutes
- Brian explores the properties of a few calculation functions, and demonstrates how to write a curried function.
- Brian gives the example of the filter function, curries it, explains the argument order, and imports the currying function from the Ramda library to be able to curry with more than two arguments.
- Brian explains how to use the Ramba library while currying a function, and demonstrates how to write point free functions.
- Brian explains that partial application and curried functions are dual functions. Partial application only uses one of its arguments, a curried function uses one argument at a time. The two functions have the same effect, but are written differently.
- The students are instructed to code the solutions to various currying exercises.
- Brian live codes the solution to the currying exercises.
Composition
Section Duration: 33 minutes
- Brian defines composition and gives examples of composed functions. A function is composed when it contains one function nested in the other, and returns a new function.
- Brian demonstrates how to build a simple program using currying and compose, and argues that this way of writing code leads to consistent state mutations, but not efficient programming because of the absence of a clean data pipeline.
- Brian explains that composition and dot chaining are the same, and says that dot chaining is more ergonomic in JavaScript.
- Brian demonstrates how to use logging in function composition to debug code.
- Brian demonstrates how to use compose in various examples, and rewrites functions into pipelines, making code easier to read.
- Brian demonstrates how refactoring with function composition can make code easier to understand.
- Brian demonstrates how to refactor functions using the point free style, explains that it allows parallelization and a better data processing workflow thanks to the high level declarative syntax.
Functors
Section Duration: 27 minutes
- Brian explains that a functor is a container that holds an object that is mapped over, demonstrates how to dot chain a function into a linear control flow, and explains that a functor is an object that has a map method. A functor is useful in functional programming because it abstracts function application.
- Brian demonstrates how to use dot chaining with a functor and refactors the code written in the previous lesson.
- Brian demonstrates how to use composition and sub-composition to nest functions. Nested functions are useful to have a clear idea of the data flow.
- Brian demonstrates how to write a function that allows for chaining methods and argues that one could use promise.then instead, but in an effort to stay deterministic and mathematical, the use of chaining is better.
Either Monad
Section Duration: 39 minutes
- Brian explains that the type Either is a functor and a monad, it has both a map, a chain method, and a fold method. The Either type respects function purity and is effectively an if else statement, but inverted.
- Brian explains that fromNullable is useful because it is a definitive null check for every function, and avoids repetition.
- Brian demonstrates through a server example how to use the Either monad to refactor code.
- Brian demonstrates how to flatten the Either monad using both the chain and map methods.
- Brian recommends using the Either type instead of exploring specific syntax that only covers one unique way of using the type.
- Brian goes over examples of using the Either type using the dot syntax, and explains that, when working with Either, it is not possible to flatten an array of another type.
- Brian demonstrates how to use logs to debug the code, and answers questions about the Writer monad, a monad that, along with values, returns an error message.
Task
Section Duration: 1 hour, 11 minutes
- Brian explains that the Task monad is the functional equivalent of promise. Similarly to promise, Task takes resolve and reject functions, but in reversed order. A Task monad only starts running once it reaches the fork method, and this way avoids race conditions.
- Brian explains that Node IO, although commonly used, does not bring in any asynchronicity, and demonstrates how to use Task instead which gives a clean control flow.
- Brian goes over different Task monad examples and familiarizes the audience with the Task syntax, and characteristics.
- Brian explores various transformations, such as type transformations, free monads, and monoids with both the either and task monad.
- Brian demonstrates how to fetch weather data from the weather API, and explains the next data parsing steps.
- Brian builds a task monad to fetch data from an API and refactors the code using the Task monad.
- Brian demonstrates how to use the Task monad when creating a weather module, and how to use the monad in different places within the same application.
- Brian uses HTML to show some of the results gathered through the weather API on the screen, live codes a weather data type, and refactors the code into new types, making the application cleaner, and the functions within it more accessible.
Wrapping Up
Section Duration: 6 minutes
- Brian wraps up the course, thanks the audience, reviews briefly what the course covered, and answers questions about naming in functional programming.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops