Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 29, 2022 11:50 am GMT

Basic Curry Functions in JavaScript

In this post, we will dive into Curry functions with JavaScript, with an example of a basic Curry function.

In the previous post named Curry Functions in JavaScript, we discussed what arity, unary and Curry functions are, and how they are related. We'll use these concepts in this article and the upcoming ones.

Basic Example

In the example below, we define a createMessage() unary function that returns a series of nested unary functions.

function createMessage(greeting) {  return function(name) {    return function(message) {      return `${greeting}, ${name}! ${message}`;    };  };};

The outer function takes one argument and returns the first nested function, which when invoked with an argument will return its nested function, an so on.

Notice, the return value of our interest is in the final return statement, which we could have achieved with a multi-argument function:

function createMessage(greeting, name, message) {  return `${greeting}, ${name}! ${message}`;}

Currying made it into a series of returned unary functions. We can now call createMessage() with one argument at a time, sequentially:

console.log(createMessage('Hi')('Haskell')(`What's up?`));// Hi, Haskell! What's up?

Partials

Curry functions make it very convenient to generate partial functions:

const greet = createMessage('Hello');const goodMorning = createMessage('Good Morning');const greetHaskell = greet('Haskell');const greetAbdullah = greet('Abdullah');const wishHaskell = goodMorning('Haskell');

Note here that, we are not returning the final return statement, not as yet.

console.log(greet);/*Returnsf (name) {  return function(message) {    return `${greeting}, ${name}! ${message}`;  }; }*/console.log(greetHaskell);/*Returns f (message) {    return `${greeting}, ${name}! ${message}`;  }*/

We have to call each returned function, one by one, sequentially - until we provide all the arguments.

const askHaskell = greetHaskell('Where are you now?');console.log(askHaskell);// Hello, Haskell! Where are you now?

Curry with Arrow Functions

Arrow functions make it very easy to write Curry functions. The above function can be written in one line:

const createMessage = greet => name => message => `${greet}, ${name}! ${message}`;const messageHaskell = createMessage('Hi')('Haskell')('How are you?');console.log(messageHaskell); // Hi, Haskell! How are you?

In the next article, Currying Existing Functions with JavaScript, we'll start exploring more advanced Curry functions that involves transforming an already defined function into a Curry function.


Original Link: https://dev.to/anewman15/basic-curry-functions-in-javascript-2pf

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To