Functional Programming Fundamentals

Last updated 258 days ago by Matthew Gerstman

javascript

In the past few years, React and Redux have generated a surge of Functional Programming which we often take for granted. However many of us never got a chance to learn the fundamentals.

In this post, we’ll cover the fundamentals of Functional Programming and how they apply to modern JavaScript. We’ll also avoid unnecessary jargon like monads and functors and stick to concepts that will make our code better.

None of this is new

Before we dive into actual concepts, I want to lead by saying that none of this is new. Functional Programming as existed for over 60 years. Lisp, a popular FP programming language, was first developed in 1958.

What is new is the popularity of FP, which has surged in the past few years.

When is Functional Programming most useful?

Before we cover what functional programming is. I think it's helpful to define when we use it.

Functional Programming is most useful when we’re doing 1 to 1 data transformations.

// This is the data store type UserMap = {[userId: number]: User} // This is the functional layer type convertUserMapToArray: (userMap: UserMap) => User[]; // This is the presentation layer. type Component = ({ users: User[] }) => JSX.Element

In the code snipped above we have some types listed for a data store, a component, and a functional layer in between them.

This functional layer, convertUserMapToArray, converts the data from a format that makes sense to the store to a format that makes sense for the UI. This is what we're going to zero in on today.

What is Functional Programming?

Functional programming (often abbreviated FP) is the process of building software by composing pure functions. A pure function is a function which given the same inputs, always returns the same output, and has no side-effects. [9]

Read full Article