
Company News
Socket Joins the OpenJS Foundation
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.
compose-function
Advanced tools
Installation |
Usage |
Related |
License
logo by Justin Mezzell
Compose a new function from smaller functions `f(g(x))`
npm install compose-function
import compose from 'compose-function';
const composition = compose(sqr, add2); // sqr(add2(x))
composition(2) // => 16
compose(sqr, inc)(2); // => 9
compose(inc, sqr)(2); // => 5
import compose from 'compose-function';
import { curry, _ } from 'curry-this';
const add = (x, y) => x + y;
// add(6, sqr(add(2, x)))
compose(
add::curry(6),
sqr,
add::curry(2),
);
// map(filter(list, even), sqr)
compose(
map::curry(_, sqr),
filter::curry(_, even),
)([1,2,3,4,5,6,7,8]) // => [4, 16, 36, 64]
:: huh?If you’re wondering what the :: thing means, you’d better read this excellent overview by @jussi-kalliokoski or have a look at the function bind syntax proposal.
Or checkout the curry-this docs.
MIT © Christoph Hermann
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including function composition. The `_.flow` and `_.flowRight` methods in Lodash offer similar functionality to `compose` and `pipe` in compose-function.
Ramda is a functional programming library for JavaScript that emphasizes immutability and side-effect-free functions. It provides `R.compose` and `R.pipe` functions, which are similar to the compose-function package but come with additional functional programming utilities.
Redux is a state management library for JavaScript applications. It includes a `compose` function that is often used to apply multiple middleware functions in a Redux store. While its primary focus is state management, its `compose` function can be used similarly to the compose-function package.
FAQs
Compose new functions f(g(x))
The npm package compose-function receives a total of 685,514 weekly downloads. As such, compose-function popularity was classified as popular.
We found that compose-function demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.

Security News
A compromised npm publish token was used to push a malicious postinstall script in cline@2.3.0, affecting the popular AI coding agent CLI with 90k weekly downloads.