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...
For further actions, you may consider blocking this person and/or reporting abuse
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.
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.
It may seen useless but if you have
map
function that works likeArray.map
but for "plain" objects, you could implement ashallow_copy
function like this.Yes, this is a nice example, I agree. And i will explain more about this, in the next posts
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 problemsThere's just this a tiny little thing that you missed:
Where you have:
It should be:
(notice the comment change)
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!
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. :)
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.
I'm a functional programmer myself, as much as possible. Nice reading your article!
Great article to encourage ppl About functional program Ing. Thx a lot
Thanks <3
Agreed
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 oni
inside the loop.That's anything except a pure function.
Pure function:
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 ;)
Nice article, actually I was going the counter direction, to make C go object oriented
github.com/sanousy/cplus
Hi Fanny! Keep it up! I'm glad to see FP being introduced in different languages
Good suggestion,thanks
Another option is
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
Thanks Fanny for informative explanation.
Thank you <3
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?
Glad to see functional programming is having a bit of a renaissance :)