I admit that I am still at the beginning of my FP journey, but it's examples like these that make me sceptical about the whole FP hype which is consuming the JS world.
const isOdd = n => n%2 === 1;
const isEven = n => n%2 === 0;
Declarative / FP isOdd:
// utilities const mod = y => x => x % y; const eq = y => x => x === y; const mod2 = mod(2); const eq1 = eq(1); const compose = (fn2, fn1) => v => fn2(fn1(v)); // end result const fpIsOdd = compose(eq1, mod2);
Declarative / FP isEven:
// utilities const not = fn => x => !fn(x); // end result const fpIsEven = not(fpIsOdd);
The FP style may be more readable (or is it?), but I needed to write 5-6 utility functions to achieve the same end result, so what are the real benefits here?
I wouldn't even know how I could justify writing code like this to my team members.
I am already used to writing FP code in moderation (in my JS/TS projects), like
- using .map, .reduce, .filter
- constructing pure functions
- using redux for global state management (store, pure reducers, actions)
- doing shallow/deep copying of function params when needed (to minimise mutating reference variables outside of the function's lexical scope)
And implementing these concepts doesn't require so much overhead in code. To be fair, redux does add a layer of indirection but it's totally manageable and justified.
But the more I learn about advanced FP concepts like pointfree style, composition and piping, the more absurd it seems to use it in my projects.
Maybe I just can't see the grand and majestic FP forest because of all the composition/piping trees that I'm currently surrounded with.
What do you think about FP? Do you have any experience in using the more advanced techniques in some serious JS/TS projects?