2632. Curry | LeetCode javascript challenge day 10 | 14 May 2023
Leetcode Javascript Day-10 challenge - ` 2632. Curry`. Problem -https://leetcode.com/problems/curry _________________ # Content: * Video * Intuition * Benefits of currying * Approach * Complexitie with explanation * Code in JS * Code in TS * Important links # video [Video explanation](https://youtu.be/YnvIObEO3nU) https://youtu.be/YnvIObEO3nU # Intuition * The function curry takes an function `fn` * It return a curried version of `fn` * Now, we know a curried function can take multiple arguments one at a time # Currying: Currying is a technique in functional programming that involves transforming a function that takes multiple arguments into a series of functions that take one argument each. The resulting series of functions can be called one after another, each taking a single argument, until all the arguments have been provided and the original function is executed. In JavaScript, currying is typically achieved using closures. When a function is curried, it returns a new function that "remembers" the values of any arguments that have already been provided. Here's an example: ``` function add(x) { return function(y) { return x + y; }; } const add5 = add(5); console.log(add5(3)); // Outputs 8 ``` In this example, add is a curried function that takes one argument (x) and returns a new function that takes another argument (y). When we call add(5), we get back a new function that adds 5 to its argument. We then call this new function with add5(3), which returns 8. Currying can be useful in situations where you need to partially apply a function with some arguments, and then use the partially applied function later. This can help to reduce the complexity of your code and make it more reusable. Currying is not the same as partial application, although the two concepts are often used together. Partial application involves supplying some, but not all, of the arguments to a function, whereas currying involves transforming a function to take one argument at a time. # Benefits of currying * Currying is a checking method to make sure that you get everything you need before you proceed * It helps you to avoid passing the same variable again and again * It divides your function into multiple smaller functions that can handle one responsibility. This makes your function pure and less prone to errors and side effects * It is used in functional programming to create a higher-order function This could be personal preference, but I love that it makes my code readable 📝 Note: Currying doesn’t call a function. It just transforms it. # Approach * First get the arguments passed to the function in a local variable `localArgs` * You can get all the argument passed to a function by `...args`, it's spread operator (explained in video) * Start adding them to our local array * Check if length of local variable is equal to arguments passed to the function `fn.length` * if it's equal, return the curried function * else keep calling the function with the arguments * again use spread operator to pass the arguments to `fn` # Complexity - Time complexity: - Space complexity: # Code ```javascript [] function curry(fn) { const localArgs = []; return function curried(...args) { localArgs.push(...args); if (localArgs.length ( less than operator) fn.length){ return curried }; return fn(...localArgs); }; }; /** * function sum(a, b) { return a + b; } * const csum = curry(sum); * csum(1)(2) // 3 */ // f(a, b, c) ``` # Typescript ```typescript [] function curry(fn:Function):Function { const localArgs = []; return function curried(...args) { localArgs.push(...args); if (localArgs.length ( less than operator) fn.length){ return curried }; return fn(...localArgs); }; }; ``` # Important links * [Join 100+ other JS lovers](https://discord.gg/2BxFN63EFc) 🚀 * [Reach out to me on Linkedin.](https://www.linkedin.com/in/anshulontech/) 🙏🏻  --------------------------------------------------------------------------------------------------------------- Follow us on: LinkedIn: https://www.linkedin.com/in/anshulontech Instagram: https://www.instagram.com/anshulontech/ Twitter: https://twitter.com/anshulOntech 🚀🚀 Join the community of 100+ javascript devs, learn and grow 🚀🚀 Telegram: https://t.me/joinAnshulOnTech Discord: https://discord.gg/2BxFN63EFc Hit the link button, comment on your opinions in the comments below, hit the subscribe button, and turn on notifications to get regular updates. #anshulontech #anshulintech #JavaScript30 #LeetCodeChallenge #ProblemSolving #JavaScript #React #NodeJS #Angular #ReactNative #CodingChallenge #Algorithm #DataStructures #WebDevelopment #FrontendDevelopment #BackendDevelopment #FullStackDevelopment
Download
0 formatsNo download links available.