JavaScript Currying: The Secret Sauce to Smoother, Cleaner Code
2024-8-5 05:43:4 Author: hackernoon.com(查看原文) 阅读量:20 收藏

Currying is a technique in JavaScript, where a function with multiple arguments is transformed into a series of functions, each taking one argument.

Why Currying?

  • Flexibility: It allows us to create new functions from existing functions, presetting some arguments and leaving others open.
  • Reusability: Functions can be reused with some arguments pre-filled.
  • Simplicity: It makes it easier to code when dealing with complex logic.

How to Achieve Currying in JavaScript

Currying in JavaScript can be achieved in two ways:

  • Using Bind

  • Using Closures

Let us explore both implementations using a simple multiplication function that takes two parameters num1 and num2. Now I need a function that multiplies a number by two, which means it requires a single parameter and the provided number will be multiplied by 2. Let us use currying to solve this.

Bind implementation

Let's have a function that multiplies two numbers:

let multiply = (num1, num2) => {
    return num1 * num2
}

This is how we can use currying to implement our second function multiplyBy2.

let multiplyBy2 = multiply.bind(this, 2)

This is how the functions can be invoked

multiply(2, 3) // Returns 6
multiplyBy2(4) // Returns 8

Closure implementation

In case of closure, we will need to have to modify the multiply function in such a way that it has an inner function for the second argument.

let multiply = function(x) {
    return function(y) {
        return x*y
    }
}

Here the inner function has access to the scope of its parent function. This is called closure in JavaScript. Let's implement our multiplyBy2 function here.

let multiplyBy2 = multiply(2)

This is how the functions can be invoked:

multiply(2)(3) // Returns 6
multiplyBy2(4) // Returns 8

This is how Currying can be implemented in JavaScript.

Use Cases for Currying

One real-world example where we can use Currying is with logging function implementation. The function will take two arguments Level and Message. We can use Currying to create functions for different log levels. Let us see how we can implement it.

const logger = (level) => (message) => `[${level}] ${message}`

// Currying
const debug = logger('debug')
const info = logger('info')
const warning = logger('warning')
const error =  logger('error')

// Functions invocation
debug("Checking if the values is coming")  // Returns "[debug] Checking if the values is coming"
info("API called successfully")  // Returns "[info] API called successfully"
warning("Server returned a warning message")  // Retuns "[warning] Server returned a warning message"
error("Something went wrong while calling the API")  // returns "[error] Something went wrong while calling the API"

Thank you for reading!

Appreciate your time and hope you found this post valuable.

Give currying a try in your next JavaScript project and let me know how it improves your coding experience in the comments below!


文章来源: https://hackernoon.com/javascript-currying-the-secret-sauce-to-smoother-cleaner-code?source=rss
如有侵权请联系:admin#unsafe.sh