DEV Community

The beauty of Functional Programming

Fanny on December 26, 2019

There are many ways to make a program, probably you already made your program like a serie of commands, this is what we called of "imperative progr...
Collapse
 
chrisk314 profile image
Chris Knight

I agree. I doubt many people would consider the recursive version to be more readable in this case. Recursion is a very powerful tool which is essential in some scenarios and just plain more elegant and understandable in others. However, here it really is unnecessary. Recursive code can also be extremely difficult to debug. This is my own personal experience from working a lot with trees in several different contexts.

Collapse
 
lucis profile image
Lucis

Really nice post, Fanny!

The most cool thing about learning FP, in my opinion, is that it makes you a better developer as a hole, even when programming using other paradigms. Composition, avoiding state mutations, writing small and efective functions will all help you avoid common pitfalls when coding.

There's a cool article from Paul Graham, a famous entrepreneur and programmer, that he talks about the importance of LISP, the first FP language, in his startup years ago.

I would advise you to practice such skills using a "more functional" programming language, it's a nice exercise. There are three that I like: ReasonML (blends well with React), F# (being .NET is a pro, and it's really good when modeling a domain) and Clojure (super bright community, and made by some awesome developers. Used by Nubank).

Collapse
 
vonheikemen profile image
Heiker

Another important thing that functional programming encourage is function composition. Even the most simple function can be useful if you combine it with something else.

This is one example that I really like. Say that you have this function.

function identity(arg) {
  return arg;
}

It may seen useless but if you have map function that works like Array.map but for "plain" objects, you could implement a shallow_copy function like this.

function shallow_copy(obj){
  return map(identity, obj);
}
Collapse
 
fannyvieira profile image
Fanny

Yes, this is a nice example, I agree. And i will explain more about this, in the next posts

Collapse
 
fannyvieira profile image
Fanny • Edited

Okay, i agree partially with you, because for many friends is confuse the syntax of the loop reassigning the count value many times
i = i+1, and the math perspective this is dont make sense, in recursive way this is declarative, you express what you want and the recursion make the things, although in many scenarios i prefer using iterative way. Since this is my particular opinion and the example was too confuse, i removed. But yes, with recursion we have other problems

Collapse
 
joaoportela profile image
João Paulo dos Santos Portela

There's just this a tiny little thing that you missed:

Where you have:

const numbers = [1, 2, 3];
const doubles = numbers.map(num => num * 2) //[1, 4, 6]

It should be:

const numbers = [1, 2, 3];
const doubles = numbers.map(num => num * 2) //[2, 4, 6]

(notice the comment change)

Collapse
 
kelerchian profile image
Alan

These are great. Though you missed one best virtue of FP, the Type System. It's hard to do in js though as js doesn't have any explicit compilation step and compile time type at all.

Check this out dev.to/wolksoftware/why-typescript...

Collapse
 
rolandcsibrei profile image
Roland Csibrei

With a few mistakes (changeFirstElement in the immutable data example will ADD a record to the first position, index zero, and not change it) but a good article for the beginners to understand, why is fn programming and immutability so cool. Thx!

Collapse
 
artoodeeto profile image
aRtoo

Theres a book that ive read that you dont have to force yourself to use recursion because its prone to SO error, like if a base case is not correctly enforced. Although recursion is useful for sorting and tress. nice article btw. :)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
ekoutanov profile image
Emil Koutanov • Edited

Sorry Fanny, but it would be classed as a pure function in spite of writing to a local variable. As long as the write has no side effects that are externally observable, it would satisfy the definition of a pure function.

Even a recursive function with no visible variable assignment may introduce any number of intermediate variables as part of compiler optimisation; this wouldn't make it any less pure provided these changes did not escape outside function scope.

Collapse
 
nandoabreu profile image
Fernando Abreu

I'm a functional programmer myself, as much as possible. Nice reading your article!

Collapse
 
smil3cz profile image
Jan Melicherik

Great article to encourage ppl About functional program Ing. Thx a lot

Collapse
 
fannyvieira profile image
Fanny

Thanks <3

 
pthreat profile image
pthreat

Agreed

Thread Thread
 
ivanpierre profile image
Ivan Pierre • Edited

result is an external state, initialized outside the function. myCount() will give a different response, every time you call it. There's a side effect on i inside the loop.
That's anything except a pure function.

Pure function:

  • Its return value is the same for the same arguments (no variation with local static variables, non-local variables, mutable reference arguments or input streams from I/O devices).
  • Its evaluation has no side effects (no mutation of local static variables, non-local variables, mutable reference arguments or I/O streams).
Collapse
 
fannyvieira profile image
Fanny • Edited

Hi Marcelo, this is because the idea is show that you need create a new array in immutable data, and not only change a array element ;)

Collapse
 
sanousy profile image
Sanousy

Nice article, actually I was going the counter direction, to make C go object oriented

github.com/sanousy/cplus

Collapse
 
garciat profile image
Gabriel Garcia

Hi Fanny! Keep it up! I'm glad to see FP being introduced in different languages

 
fannyvieira profile image
Fanny

Good suggestion,thanks

Thread Thread
 
garciat profile image
Gabriel Garcia

Another option is

function what([x, ...xs]) {
  return ['new first elem', ...xs];
}
Collapse
 
korwalskiy profile image
AbdRozaq

Great write up, and I was looking out for the buzz word "passing functions as variables" as this helped me grasp the concept long ago.

Collapse
 
htewech7 profile image
taha HTEWECH

Thanks Fanny, i am new in programming and this article was awsome

Collapse
 
nguyenquangtin profile image
Tony Tin Nguyen

Thanks Fanny for informative explanation.

Collapse
 
fannyvieira profile image
Fanny

Thank you <3

 
fannyvieira profile image
Fanny

Because it is performing a side effect, by definition, can you write me a example using interation that you agree that is a impure function?

Collapse
 
lhmzhou profile image
Linda Zhou • Edited

Glad to see functional programming is having a bit of a renaissance :)