Currying - Part 6 of Functional Programming in JavaScript
Why Stack Overflow recommends it
Stack Overflow users link it when explaining currying, single-argument functions and functions that return functions in JavaScript.
Recommended by 2 people across 2 different months, 2017 to 2022.
What it is
A short JavaScript video from Fun Fun Function, presented by Mattias Petter Johansson, explaining currying: turning a multi-argument function into a chain of single-argument functions. It is part 6 of a functional programming series and points to lodash's curry function.
What people said
Recommends kind user · 2017-03 · Stack Overflow · on “How to Write a function where the output for both “sum(2,3)” and “sum(2)(3)” will be 5”
Syntax as follows: sum(2)(3) is related with currying . It means that you need to return another function inside the main function to get the desired result. Here's a nice video about currying on [this video] , from which I've learned a lot. function sum(a){ if (arguments.length == 1) { return function(b){ return a + b; } } else { return Object.keys(arguments).reduce((a,b) => arguments[a] + arguments[b]); } } console.log(sum(2,3)); console.log(sum(2)(3)); As Karl-André Gagnon [LINK] proposed, it can be also done with bind function. function sum(a){ if (arguments.length == 1) { return sum.bind(this, a) } else { return Object.keys(arguments).reduce((a,b) => arguments[a] + arguments[b]); } } console
Recommends Fcmam5 · 2020-10 · Stack Overflow · on “Functions returning functions understanding why it works”
Hi @NoamAris, no worries. We are here to help :) So, I meant by **assign**: Put the return value of a function (it could be a number, a string or even another function) in a variable. And if we assign a function to a variable, we can use it(the variable) as a function as well. Please check this video for example: [this video] or just look-up for currying in JavaScript, if you still find it confusing check this great playlist: [LINK]
Cites Somjit · 2017-03 · Stack Overflow · on “js - Higher order unary function - why is this needed?”
Here's where it starts : This below snippet doesn't work as expected: ['1', '2', '3'].map(parseInt) // [1, NaN, NaN] <- bad output A simple change remedies it: ['1', '2', '3'].map(a => parseInt(a)) // [ 1, 2, 3 ] <- good output But this book [LINK] I'm reading has a different remedy: Create a unary function, which takes in a function ( parseInt in our case) and if the function has n arguments, it returns a new function that has only a single argument. Here's how the book does it: const unary = (f) => { return f.length === 1 ? f : (args) => f(args) } ['1', '2', '3'].map(unary(parseInt)) // [ 1, 2, 3 ] <- good output again! My question is: How is this unary function is working? Any practical s
Cites JSmart523 · 2022-02 · Stack Overflow · on “JavaScript making something unnecessarily complicated”
A lot (and I mean a **lot**) of functional programming uses single-argument functions, so much so that people are used to it and do it out of habit. As code becomes more complex, this convention DOES become useful. See [this video]