What is just-curry-it?
The just-curry-it npm package is a utility for currying functions. Currying is a functional programming technique where a function with multiple arguments is transformed into a sequence of functions each taking a single argument. This package simplifies the process of currying functions in JavaScript.
What are just-curry-it's main functionalities?
Basic Currying
This feature allows you to transform a function that takes multiple arguments into a curried function. The curried function can then be called with one argument at a time, or with multiple arguments at once.
const curry = require('just-curry-it');
function add(a, b, c) {
return a + b + c;
}
const curriedAdd = curry(add);
console.log(curriedAdd(1)(2)(3)); // 6
console.log(curriedAdd(1, 2)(3)); // 6
console.log(curriedAdd(1)(2, 3)); // 6
Partial Application
This feature allows for partial application of functions. You can fix some arguments of the function and get a new function that takes the remaining arguments.
const curry = require('just-curry-it');
function multiply(a, b, c) {
return a * b * c;
}
const curriedMultiply = curry(multiply);
const multiplyBy2 = curriedMultiply(2);
console.log(multiplyBy2(3, 4)); // 24
console.log(multiplyBy2(3)(4)); // 24
Other packages similar to just-curry-it
lodash
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including currying. The `_.curry` method in Lodash offers similar functionality to just-curry-it but comes with the added benefit of a comprehensive suite of other utilities.
ramda
Ramda is a functional programming library for JavaScript that emphasizes immutability and side-effect-free functions. It provides a `R.curry` function that offers currying capabilities similar to just-curry-it, but it is part of a larger set of functional programming tools.
curry
The `curry` package is a lightweight utility specifically designed for currying functions. It offers similar functionality to just-curry-it but is focused solely on currying without additional utilities.
just-curry-it
Part of a library of zero-dependency npm modules that do just do one thing.
Guilt-free utilities for every occasion.
🍦 Try it
npm install just-curry-it
yarn add just-curry-it
Return a curried function
import curry from 'just-curry-it';
function add(a, b, c) {
return a + b + c;
}
curry(add)(1)(2)(3);
curry(add)(1)(2)(2);
curry(add)(2)(4, 3);
function add(...args) {
return args.reduce((sum, n) => sum + n, 0)
}
var curryAdd4 = curry(add, 4)
curryAdd4(1)(2, 3)(4);
function converter(ratio, input) {
return (input*ratio).toFixed(1);
}
const curriedConverter = curry(converter)
const milesToKm = curriedConverter(1.62);
milesToKm(35);
milesToKm(10);