Published
Edited
Jun 17, 2020
Insert cell
Insert cell
md`
Functional programming is not all about functions, it's a collection of procedures
`
Insert cell
md`
The spread adapter
`
Insert cell
{
const spreadArgs = fn => args => fn(...args);
const f = (x, y, z, w, p) => x + y + z + w + p;

const test = spreadArgs(f);
let testArr = [1, 2, 3, 4, 5];
return test(testArr);
}
Insert cell
Insert cell
{
const spreadArgs = fn => {
console.log(fn); // f(x, y, z, w, p) is passed in
return args => {
console.log(...args); // ['1', 2, 3, 4, 5]
return fn(...args); // like native .apply in JS, the spread ... operator transforms the array into individual arguments to be passed into our function f.
};
};

const f = (x, y, z, w, p) => {
return x + y + z + w + p;
};

const test = spreadArgs(f);
let testArr = [1, 2, 3, 4, 5];
return test(testArr);
}
Insert cell
Insert cell
{
const spreadArgs = fn => args => fn(...args);
const f = (x, y, z, w, p) => x + y + z + w + p;

let array = [1, 2, 3, 4, 5];
const testArr = spreadArgs(f)(array);

return testArr;
}
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more