DEV Community

Cover image for Functional Programming for the Object-Oriented Developer - Part 0
Patricio Ferraggi
Patricio Ferraggi

Posted on • Updated on

Functional Programming for the Object-Oriented Developer - Part 0

If you are interested in reading this article in Spanish, check out my blog The Developer's Dungeon

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


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

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

Not a function

Wait, what?

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

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

isBiggerThanThree(3); // FALSE
isBiggerThanThree(5); // TRUE
Enter fullscreen mode Exit fullscreen mode

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 very common in FP and comes included in many libraries like the method memoizeWith in Ramda.js
  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

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

Woah Slow Down

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:

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

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


const result = mapBoolToHumanOutput(isBiggerThanThree(3));
Enter fullscreen mode Exit fullscreen mode

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 😄

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, 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
  • 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 or Haskell from principles or A type of programming.

CLOJURE BUILD

I could switch to something like Clojure with Structure and Interpretation of Computer Programs or the book that gives the name to this series Functional programming for the object-oriented programmer

JAVASCRIPT BUILD

I could read Mostly adequate guide to functional programming 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.

Top comments (9)

Collapse
 
shimmer profile image
Brian Berns • Edited

I think it's great that you're learning functional programming and category theory, although that's potentially a lot of advanced stuff to take in at once. (Personally, I'm a big fan of F# if you want to consider yet another language.)

The meme that all patterns are just functions in FP is only kind of true. There are actually lots of interesting patterns in FP. You even mentioned one above: composition. More advanced patterns include functors, applicatives, monoids, monads, etc. The cool thing about FP patterns is that they come from math, so they have a solid foundation that OOP patterns often lack.

Best of luck on your journey!

Collapse
 
patferraggi profile image
Patricio Ferraggi • Edited

Thank you for the kind words, yes indeed there are alot more patterns to follow but also yeah they come from math so you can map what you are doing to some other concept.

F# is also in consideration as I am mainly a C# dev, do you have any material recommendation?.

For now I am just following that course on category theory that uses Haskell and reading the book.

Collapse
 
shimmer profile image
Brian Berns • Edited

Ah, if you're a C# dev, then F# will be perfect for you! A lot of people find F# for fun and profit to be a good way to learn the language. I'm happy to help as well if you decide to jump in and have questions.

Collapse
 
markoshiva profile image
Marko Shiva

Great post. Good luck with leveling up the functional programming. You interested me to when find free time start also learning functional programming.

Collapse
 
patferraggi profile image
Patricio Ferraggi

That is good news, happy it had that effect on you. It is definitely an interesting subject, I strongly recommend that MIT course that is happening right now, great resource.

Collapse
 
sirgee profile image
Sergey Sachkov

Thanks Patricio for this introduction. Waiting for the next post.

Collapse
 
patferraggi profile image
Patricio Ferraggi

Thanks Sergey, I will try to post two episodes a week if I can. If not I will continue doing 1 a week like I been doing so far with my other blogposts.

Since I am also translating them to Spanish for my personal blog it takes me longer to do one.

Collapse
 
kraken47 profile image
kraken47

Nice post bro

Collapse
 
patferraggi profile image
Patricio Ferraggi

Thank you I am glad you like it.