loading...

### 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... [Read Full]

While I do prefer a FP-esque style of programming, I don't think that the recursive `myCount` is more readable.

It's also less safe, at least as it's currently written, since it'll cause "infinite" recursion for any `i` bigger than 10.

The imperative version, on the other hand, will default to 0 for any such values

``````function iterativeMyCount(n) {
let count = 0;
for (let i = n; i < 10; ++i) {
count += i;
}

return count;
}
``````

I generally prefer to use local mutation (when circumstances allow) instead of going full recursive - unless the problem is tailor made for it, like tree traversal or something.

I find it easier to reason about and avoids the parameter growth of completely mutation free cases - especially if optimizing for TCO or memory.

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.

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

The non recursive `myCount` is a pure function.

[deleted]

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.

Well, it is a pure function.
What is wrong with accessing variables from function scope?

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?

Sure

``````let result = 0

function myCount() {
for (let i = 1; i < 10; i++) {
result += i
}
return result
}
``````

There is example of doing side effects by modifying outside scope, which as you stated, makes the function impure.

In your example you aren't accessing "outside" state

There's a small issue in the daft punk example. Both fns aren't equivalents.

On the first version, you modify the element zero of the array.

On the second one, you add a new element in the beginning.

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 ;)

Yep but, to have the same behaviour on both examples, you should use something like:

``````function changeFirstElem(array) {
const modifiedArray = ['Lose yourself to dance', ...array.slice(1)]
return modifiedArray
}

// changeFirstElem([1, 2, 3]) will be ['Lose yourself to dance', 2, 3], like your first non-pure fn
``````

Good suggestion,thanks

Another option is

``````function what([x, ...xs]) {
return ['new first elem', ...xs];
}
``````

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).

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);
}
``````

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

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)

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. :)

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...

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!

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

github.com/sanousy/cplus

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

Thanks <3

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

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

Thanks Fanny for informative explanation.

Thank you <3

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.

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

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

code of conduct - report abuse