BY Simmi Kava2 Aug 2021 Edit
JavaScript: CURRYING

What is currying in JS?

Currying is a process of evaluating a function with multiple arguments and turning it into a sequence of functions, each with single argument only i.e converting an n-ary function into the unary function form.

Why use currying?

  1. Code reusability
  2. To achieve Function Composition (A process of combining simple functions to build more complicated ones)
  3. Easy to maintain as our code implementation will be smaller and more readable.

Can you explain it with an Example?

Certainly,

Let's look at an example to convert an input number to string using the radix provided.

//Without Currying
var print = (number,radix) => number.toString(radix);
console.log(print(123,16)); //7b
console.log(print(123,2)); //1111011
//With Currying
var print = (radix)  => (number) => number.toString(radix);
var hex = print(16);
var binary = print(2);
console.log(hex(123)); //"7b"
console.log(binary(123)); //"1111011"

Here, print is a function, which takes 1 argument which is radix, (16 for hex and 2 for binary etc.). number is the input number which needs to be converted and the lambda function will print the input number in the given numeral format.

BY Simmi Kava
LikeCommentSave
LikeCommentSaveShare
0
Categories
General
YantraJS
Developer Guides
Tutorials
Web Atoms Updates

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
Simmi Kava
commented this post.
Simmi Kava
liked this post.
Show more
ARCHIVES
2024
2023
2022
2021
TAGS
javascript (56)
developer (25)
javascriptdeveloper (16)
Xamarin.Forms (16)
Html (14)
typescript (12)
webatoms (12)
xamarin (11)
coding (10)
web-atoms (10)
arrays (9)
android (8)
javascript-developer (8)
csharp (7)
dotnet (7)
css (6)
update (6)
dotnet-standard (5)
function (5)
iOS (5)
methods (4)




Web Atoms: JSX (TSX + TypeScript) for Xamarin.Forms, Hot Reload Your App in Production Environment

PlaygroundSamples Repository