3 ms·
I've been reading this on functional JS: https://news.ycombinator.com/item?id=31944352 https://news.ycombinator.com/item?id=31944352 For example, functions th
by photochemsyn 4y ago
I've been reading this on functional JS:
https://news.ycombinator.com/item?id=31944352 https://news.ycombinator.com/item?id=31944352
For example, functions that return functions:
const filter = (predicate, xs) => xs.filter(predicate)
So it's an assignment, leading me to think it's a statement, but then using filter later would be an expression?
- filoeleven 4y agoThat example is particularly confusing because it’s a function definition written as an assignment for IMO no good reason. The outcome is the same either way, but function filter(predicate, xs) { return xs.filter(predicate) } lets you know immediately that “filter” is a function, not a value. There are valid cases where assigning an anonymous function a name via “const” is appropriate, for example when working with higher-order functions, but most of the time it’s not necessary or helpful. I wish it hadn’t caught on. EDIT: I should have read your comment more carefully. The example function is not a HOF though, it runs xs.filter(pred) when called. A HOF would be function addConst(x) ( return (y) => x + y } const add3 = addConst(3) add3(5) // returns 8 The second line can still be written as function add3(x) { return addConst(3) } I think people use the const form because there’s less text to write. Syntax highlighting makes this form more bearable, but I still don’t like it.