Functional Programming for the Object-Oriented Developer - Part 0
Tags: functional, codenewbie, webdev, beginners
Published: 2020-01-14 21:40:36

If you are interested in reading this article in Spanish, check out my blog [The Developer's Dungeon](https://www.patferraggi.dev/blog/2020/jan/programacion-funcional-para-oop/)

If you been following my latest articles you probably know that I have been studying functional programming, and let me be clear, I am a newbie at it, and I mean under level 12, not doing PVP, killing small monsters for hours kind of a newbie.
As a classic newbie, I went hard on it, I entered a dungeon with monsters well beyond my level, as you can probably figure out I am still scared, luckily slowly and surely I am leveling up, so hopefully, I can give you a walkthrough so you don't fall into the same traps as I did.

![Zelda](https://thepracticaldev.s3.amazonaws.com/i/zsoxoc4tgtla335vzzm6.gif)

----

According to functional programming(from now on I will say FP) books, FP is deep-rooted in math, if you are a self-taught developer like me this is very scary but once you start getting the hang of it, it is actually really nice. It gives you some background concepts where you can map the things you are doing to know if you are doing things in the right way or not, it is something that usually doesn't happen when doing Object-oriented programming where all concepts are abstract.

As you probably figured out FP is all about functions. 

![FP Joke](https://thepracticaldev.s3.amazonaws.com/i/rkt7ybtc1kxa6n8dr9hi.jpg)

In OOP you will usually design classes that represent the domain and you will give them behaviors in the form of methods on those classes, in FP, on the other hand, you will define things in simpler terms, you will create types and you will use functions to manipulate data and types to produce results.

So let's start from scratch:

## What is a function? 

I know what you are thinking, is this guy serious? I know what a function is dude, give me a break. Well, I thought I knew too and now I will not see a function in the same way again. FP defines a function not like most oop programming languages do but it defines it in mathematical terms. 
So let's go to the definition:

*In mathematics, a function is a relation between sets that associates to every element of a first set exactly one element of the second set*

![Function](https://thepracticaldev.s3.amazonaws.com/i/p0sa0aezq7f74b1a1407.png)

![Not a function](https://thepracticaldev.s3.amazonaws.com/i/rbjrkshcvb2obav1tn9e.png)

Wait, what?

This basically means that when you create a function like this one in JavaScript:

```javascript
function isBiggerThanThree(value) {
   return value > 3;
}

isBiggerThanThree(3); // FALSE
isBiggerThanThree(5); // TRUE
```

This function is a relationship between INT and BOOL(yes I know types in JavaScript are funky) where for every possible value on the first set(INT) there is only one possible answer on the second set(BOOL).

Because of this, every time you pass a number bigger than three to this function, it will always return TRUE. There is no way around it, if this doesn't happen then you are not doing functional programming, but luckily most of the functional programming languages like Haskell enforce this behavior on you.

This also relates to a Buzz Word that you hear along FP all the time, the concept of Pure Functions, what are those you might be asking? well, Pure Functions are regular functions in Functional Programming, they are functions that don't rely on, or affect the outside world when computing, just like the one we wrote a few lines ago, it receives an input, it computes and produces a result, that's it.

I know what you are thinking:

#####Why is this useful?

1. If functions always return the same value given the same input, this makes testing trivial, and yes, FP is not an excuse to drop Unit Testing
2. If functions always return the same value given the same input, then results can be cached to improve performance, this is a concept called [Memoization](https://en.wikipedia.org/wiki/Memoization) very common in FP and comes included in many libraries like the method [memoizeWith in Ramda.js](https://ramdajs.com/docs/#memoizeWith)
3. If a function doesn't depend on outside state then our code works wonders in environments where concurrency is the common currency.
4. Since we use functions in a mathematical sense, then properties that apply to math also will apply to programming languages, for example 
[Function Composition](https://en.wikipedia.org/wiki/Function_composition)


FP is all about composition, is all about taking small little functions that do very specific and generic behaviors and combining them to create more complex behaviors, this is done with a technique called Function Composition. 
Again, let's go to the definition just to check that box:

*Function composition is an operation that takes two functions f and g and produces a function h such that h(x) = g(f(x))*

![Composition](https://thepracticaldev.s3.amazonaws.com/i/ylybung2c6yxypxpfoe6.png)

![Woah Slow Down](https://thepracticaldev.s3.amazonaws.com/i/0bxes9jz96bixwowf6id.gif)


This means that if we have a function that takes an INT and returns a BOOL and we have a second function that takes a BOOL and returns a STRING, then we automatically have a third function that takes an INT and return a STRING, due to composition, let's see an example:

```javascript
function isBiggerThanThree(value) {
   return value > 3;
}

function mapBoolToHumanOutput(value) {
   return value ? 'yes' : 'no'; 
}


const result = mapBoolToHumanOutput(isBiggerThanThree(3));
```

In the following blog posts, I will explain other concepts that relate to this one and allows us to create new functions we can pass around freely instead of just applying them.

----

If you follow me on this journey we will succeed in completing this dungeon, we will reach a point where we know all the corners, know all the weapons and we will be able to save the progress after everything is completed.

I hope you liked this small introduction, I will continue leveling up and guiding you on this process. If you find this useful please let me know in the comments, if you have any remarks or advice also please let me know in the comments :smile:

Finally a small list of the material I will be using in this journey.
Currently, I am learning Haskell but this is just a tool to learn functional programming.

- I am following this live course from MIT: [Programming with categories](http://brendanfong.com/programmingcats.html), it's about Category Theory and Functional programming with Haskell.
You can watch it online and access the course material.
- I am also reading a book from one of the teachers of the course: [Category theory for programmers](https://github.com/hmemcpy/milewski-ctfp-pdf)
- After that, there are many options depending on the build I pick:

#### HASKELL BUILD

I could continue learning Haskell with [Learn you a Haskell for great good](http://learnyouahaskell.com/chapters) or [Haskell from principles](https://haskellbook.com/) or [A type of programming](https://atypeofprogramming.com/). 

#### CLOJURE BUILD

I could switch to something like Clojure with [Structure and Interpretation of Computer Programs](https://web.mit.edu/alexmv/6.037/sicp.pdf) or the book that gives the name to this series [Functional programming for the object-oriented programmer](https://leanpub.com/fp-oo)

#### JAVASCRIPT BUILD

I could read [Mostly adequate guide to functional programming](https://github.com/MostlyAdequate/mostly-adequate-guide) and do some experimentation with Ramda.js and RX.js

It is still unclear how my character will end up being when I reach the max level but you can count on me sharing all the knowledge I gather during this process.