DEV Community

Cover image for Loops the functional way

Loops the functional way

Eckehard on December 05, 2023

Immutability is kind of a fashion trend (see here), but for beginners it may be challenging to understand the concepts. How can a program do anythi...
Collapse
 
artxe2 profile image
Yeom suyun

Extreme functional programming has some significant drawbacks compared to its advantages, but the concept of not modifying the input seems quite useful.
If the guarantee that every function does not modify the input is maintained, in most cases, logic can be handled with shallow copies.

function pure(args) {
   const result = { ...args }
   result.a = something()
   delete result.b
   return result
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
efpage profile image
Eckehard

Most people think, FP and OOP exclude each other, but this is not true. There is no limitation to build parts of your code following the principles of FP and call this functions from within class methods.

Even inside a class it can be most beneficial to limit the number of side effects and use mainly pure functions. This is already good programming style. But using ONLY functions may make things more complilcated then necessary.

Collapse
 
disane profile image
Marco

I guess a good and healthy combination of both styles should be preferred. OOP really benefits FP and vice versa.

Collapse
 
hseritt profile image
Harlin Seritt

Well said.

Collapse
 
artydev profile image
artydev • Edited

Thank you Eckehard ,
Look at this :
Trampoline
TCO

Collapse
 
peerreynders profile image
peerreynders

JavaScriptCore (Safari, Bun) is the only engine that implements TCO

Chrome (V8) has marked TCO as No longer Pursuing.

Tail call optimization in ECMAScript 6