Professor Frisby’s Mostly Adequate Guide: Your Map to the Functional Programming World 🌍

4 Min Read

blog img

Hey fellow devs, it’s me again, your JavaScript companion, on a quest to conquer another coding beast 🐉.

You know that euphoric moment when you discover a concept that entirely changes how you see your code? That’s exactly what’s happening to me right now!

I’ve been diving headfirst into the “Mostly Adequate Guide to Functional Programming” by our dear Professor Frisby.

While I’m still midway through this book, I thought I’d share some of the cool things I’ve learned so far. Let’s dive into these topics with some simple examples. 🏊‍♂️

💡 Before start i want to invite you to read by yourself the book, its free and open for everyone that would like to dive in this world: https://github.com/MostlyAdequate/mostly-adequate-guide

Immutability: The Unchanging Northern Star 🌟

Imagine you have a list of friends const friends = ['Alice', 'Bob', 'Charlie'].

Now, you meet a new friend, Dave. With immutability in our grasp, we don’t just push Dave into the friends list. We conjure a new list: const newFriends = [...friends, 'Dave'].

The original friends list stands unchanged, while newFriends now welcomes Dave.

This immutability is the superhero we didn't know we needed, helping us avoid those pesky unexpected side-effects and making debugging feel like a walk in the park.

First-Class Functions:

In JavaScript, functions are first-class citizens. They can be assigned to variables, stored in data structures, passed as arguments to other functions, and even returned as values from other functions.

const sayHello = name => 'Hello, ' + name 
const greet = sayHello 
console.log(greet('Readers')) // Outputs: Hello, Readers

In the example, sayHello is a function that we're assigning to the variable greet. Now, greet acts exactly like sayHello, greeting the readers with a warm "Hello".

Declarative Programming:

“Declarative, as opposed to imperative, means that we will write expressions, as opposed to step by step instructions”

It’s a mindset switch, we’re going to stop telling the computer how to do the things, and instead focusing more in the results that we want:

//imperative 
const makes = [] 
for(let i = 0; i < cars.length; i += 1){ 
  makes.push(cars[i].make) 
} 
//declarative 
const makes = cars.map(car => car.make)

In the imperative version, you’re giving step-by-step instructions: Start at the beginning of the ‘cars’ array, access each car one by one, get the ‘make’ of each car, and add it to the ‘makes’ array.

On the other hand, the declarative version simply states what you want: “For each car in the ‘cars’ array, give me the ‘make’.” It doesn’t detail how to access each car or how to add each ‘make’ to the array. This is handled by the map function.

In declarative programming, we concentrate on the ‘what’ while the ‘how’ is taken care of by the language’s built-in functions, like map. This leads to cleaner, more readable code that's easier to reason about.

Composition:

With function composition, you’re a master architect, creating a mansion from humble bricks. Here’s how:

const double = x => x * 2 
const square = x => x * x 
const doubleThenSquare = x => square(double(x)) 
console.log(doubleThenSquare(5)) // Outputs: 100

Here, doubleThenSquare is a composited function, which first doubles the input and then squares the result. Like a skilled architect, we've crafted a larger function from two smaller ones. Isn't it satisfying?

Pure Functions:

They’re the buddies that never let you down, providing the same output for the same input, every time, with no side effects.

const multiply = (a, b) => a * b 
console.log(multiply(2, 3)) // Outputs: 6 
console.log(multiply(2, 3)) // Still outputs: 6

Here, the function multiply is our pure function. Call it a hundred times with the same arguments, and it will consistently give you the same result. Like a well-oiled machine, no hiccups or unpredictability.

Currying:

I dont have words to describe it better. Let’s see it in action:

const add = a => b => a + b 
const addFive = add(5) 
console.log(addFive(7)) // Outputs: 12

Here, our add function is playing coy. It takes the first number and then sends out a new function, eager for the second number.

This is currying, our trick up the sleeve, making our code more flexible and reusable.

Elevate Your JavaScript Skills with Functional Programming 🏅

To sum up, functional programming is like a secret weapon 🍲, a magical spell that transfigures your JavaScript from a jumble of instructions into a masterpiece of efficient and elegant code. It’s not merely about getting the job done; it’s about executing it with flair, precision, and grace.

The deeper I delve into this book, the more I feel like Neo in The Matrix, finally perceiving the coding world for what it truly is: a space where all this concepts are not mere optional extras, but the core essence of proficient coding.

If you’re ready to accept this challenge, if you’re eager to ascend your coding skills, and behold your code in a fresh light, I recommend downloading a copy of the “Mostly Adequate Guide to Functional Programming” and joining me on this voyage.

Let’s unravel the mysteries of functional programming together, and catapult our JavaScript skills to realms we never envisioned! Until then, keep coding, keep exploring, and as always, may your brackets always match, and your functions be free of side effects! 🚀

If you’re interested in staying updated with my future posts, you can follow me on the following links:

I look forward to connecting with other developers, i would love to know if you liked this new type of post! 😃

More content at PlainEnglish.io.

Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.