Functional Programming: With Examples and Lots of Cats šŸˆ

JoĆ£o Marcus Fernandes
ArcTouch
Published in
5 min readSep 29, 2021

--

The idea of this post to help you better understand Functional Programming (FP) and how it can reduce side effects and take maximum advantage of our languages. This is based on a presentation I gave at TDC Transformation on August 25, 2021. Thank you to my colleagues at ArcTouch for their support.

The benefits of Functional Programming

  • Side effects are avoided, and if needed, we can isolate them in wrappers.
  • Functional codes are predictable, which makes it easier to test all parts of your code.
  • They work great in concurrent systems, since FP systems are by definition a function. You can create a bunch of instances of the same system calling the same function.
Soon cat looking

Pure Functions

Pure functions are the base of FP, and they follow this rule:
ā€œFunctions that given the same arguments have the same returnā€

Pure functions donā€™t use data from external contexts, they just use their
arguments, following the closure like.

Curiosity: lambda functions are anonymous functions passed to
blocks and other functions.

First Order Functions

These are functions that take other functions as arguments, often used as ā€œyieldā€ blocks, or callback blocks.

Although simple, they are critical features to extend the capabilities of FP. With the use of this type of function, it is possible to write complex functions using several other smaller functions. For example:

Composition

Composition is my favorite technique of Functional Programming.

Composition is the idea of stringing together function calls in such a way that when the composite function will execute in sequence, the others passing the return of one as an argument to the next:

Recursion

Recursion is not an FP-only technique. It is part of the algorithms' category that work with the concept of ā€œDivide and conquerā€.

The same function is called, reducing the number of elements it receives, to carry out a certain routine.

It is a very common technique to handle an element list.

For example, this is an array of items, each item may have children, and the children must be shown recursively.

With this example, we create a component that calls itself when the children are detected.

Partial Application

This is Functional Programming technique is also known as Currying.

It is not possible in all modern languages, but its idea can be applied even without native support.

Partial Application creates ā€œcurriedā€ functions that can receive arguments at different times, which allows you to create helpers using different arguments.

This is the buildUri function, but it is the lame way to do it, since all arguments must be present.

Now, the cool way using the FP approach.

If we use curry, we can create a function and then call it with just the path.

Useful Functional Programming methods

Map

Map is a widely used function for manipulating lists. You get a list and a callback, and inside the callback you return what you want, creating a new list with the callback returns.

Filter

Filter also takes a callback and a list, and returns a new filtered list based on the true/false callback return.

Reduce

Reduce is a very powerful feature. You can turn a list into literally anything you want, whether itā€™s a string, an object, a new list, the sky is the limit to reduce.

Point-free

Normally functional languages donā€™t use dot-like signatures, like document.getElementById(app).innerHTML.If JavaScript were a full functional language, like ClojureScript, this call could look something like this:

Monads

Monads are the way Functional Programming deals with Null values. Usually, it implements the Maybe, which wraps the value, and can only be accessed by the wrap instead of using point path.

Monads are more than just that, but thatā€™s all you need to know for now.

Simple implementation for a Maybe.
Converting functions to use the Maybe example above.
The first example of this post, but now with Maybe.

Fantasy-Land

Fantasy Land is the specification for Functional Programming, and it uses many concepts from Algebra.

Tail Call Optimization and Lazy evaluation

Tail Call Optimization is a feature of functional languages, which given the predictability of recursive functions, maps, reduces, filters, and other methods that use lists that can be huge, the languageā€™s own interpreter/compiler/VM predicts the next call results much faster.

When these calls are applied to very large lists, the same interpreter/compiler/VM optimizes processing to only apply functions when needed, greatly reducing memory usage.

How to start Functional Programming today in JS?

There are some libraries that implement the FP concepts, and the specifications for the Fantasy-land.

Wrapping up

You are now ready to start exploring Function Programming. I recommend you start with the Lodash/FP and then use its helpers to improve your code.

Do you like Functionanl Programming? Come to ArcTouch and join our FP study group. Weā€™ll hold a spot for you. Check out our career opportunities.

--

--