4 ms·
Hmm, I'm not convinced by the article. One of the best tools I found to really dive into functional programming in JavaScript was Lodash's "fp" variant. It wil
by Rodeoclash 5y ago
Hmm, I'm not convinced by the article.
One of the best tools I found to really dive into functional programming in JavaScript was Lodash's "fp" variant. It will swap the order of the arguments to its functions and curry them all by default. So you're building a function to "add one"
const addOne = map(function(n) { return n + 1 });
Can then be applied against an array of numbers:
addOne([1, 2 ,3])
> [2, 3, 4]
This is fairly basic though. Where it really shines is when you start to use the `flow` function will can be used to compose multiple functions together (and makes the order of execution follow from left to right)
flow(addOne, reverse)([1, 2, 3])
> [4, 3, 2]
I find this is one of the small redeeming features of JavaScript!
- yakshaving_jgt 5y agoIf I were reviewing someone's code, I would be quite surprised to see this: addOne :: [Int] -> [Int] It's not idiomatic in FP (as far as I'm aware) to bake the structure in like that.
- Rodeoclash 5y agoTrue, the example could be better: const addOne = (n) => { return n + 1} map(addOne)([1, 2, 3]) > [2, 3, 4] Would decompose the addOne into its own standalone function. My only excuse was that I was in a hurry due to Minecraft time with my son.
- onion2k 5y agoI worked on a project that used lodash/fp for a couple of years. It really is game-changing if you have a large object that you need to change in similar ways across different properties. Theres also Ramda (https://ramdajs.com/ https://ramdajs.com/), which I slightly prefer because the documentation is significantly better.
- Rodeoclash 5y agoRambda is an excellent library but I've found difficult to use to teach newcomers to FP concepts. Once you start to get into transducers, lenses (which Lodash has, just calls them something else) then it can be a bit of a bridge too far as a tool to start with. Lodash/FP always seemed to me to strike that nice balance between complexity and getting things done. That said, if I was on a project with a bunch of developers that really grok'd FP concepts then Rambda would be my first pick, absolutely.
- cleancoder0 5y agoYeah, this goes full FP with fp-ts (although it's TypeScript). The only thing I hate about it is brackets everywhere. pipe( xs, reverse, A.map(addOne), )
- agumonkey 5y agoI wonder if lodash/ramda etc are still used often. There was a time when every week was about their new features but now it's silent.
- kall 5y agoI still rarely see a sizeable project without a lodash dependency. It is sometimes just one or two of the single function npm packages though.
- kall 5y agodate-fns has the same fp variant which can make a lot of sense. A note from personal experience: never mix fp and normal variants of date-fns and lodash in one project. If you import isAfter from date-fns/fp in one file and date-fns in another (or your editor imports it for your) you‘re gonna have a BAD time.