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

POPULAR POSTS
17 Mar 2021
LATEST ACTIVITY
srikanth.naidu
liked this post.
malli
saved this post.
Show more
ARCHIVES
2021
2020
2019
2018
TAGS
javascript (41)
developer (25)
javascriptdeveloper (15)
coding (10)
Xamarin.Forms (10)
webatoms (8)
arrays (7)
xamarin (7)
android (6)
typescript (6)
function (5)
csharp (4)
iOS (4)
methods (4)
object (4)
update (4)
web-atoms (4)
arrayoperation (3)
decorators (3)
programming (3)
prototype (3)




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

PlaygroundSamples Repository