754 B
754 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Juxtapose functions | snippet | javascript |
|
dark-leaves-3 | 2020-10-20T23:29:39+03:00 |
Takes several functions as argument and returns a function that is the juxtaposition of those functions.
- Use
Array.prototype.map()to return afnthat can take a variable number ofargs. - When
fnis called, return an array containing the result of applying eachfnto theargs.
const juxt = (...fns) => (...args) => [...fns].map(fn => [...args].map(fn));
juxt(
x => x + 1,
x => x - 1,
x => x * 10
)(1, 2, 3); // [[2, 3, 4], [0, 1, 2], [10, 20, 30]]
juxt(
s => s.length,
s => s.split(' ').join('-')
)('30 seconds of code'); // [[18], ['30-seconds-of-code']]