如何使用FP'将两个函数组合成1;s compose()javascript?
如何使用FP的如何使用FP'将两个函数组合成1;s compose()javascript?,javascript,function,lambda,functional-programming,function-composition,Javascript,Function,Lambda,Functional Programming,Function Composition,如何使用FP的compose() 以下是现场代码: 我有3个纯函数: // groups By some unique key const groupBy = function(xs, key) { return xs.reduce(function(rv, x) { (rv[x[key]] = rv[x[key]] || []).push(x); return rv; }, {}); }; // ungroups the group by const ungroup
compose()
以下是现场代码:
我有3个纯函数:
// groups By some unique key
const groupBy = function(xs, key) {
return xs.reduce(function(rv, x) {
(rv[x[key]] = rv[x[key]] || []).push(x);
return rv;
}, {});
};
// ungroups the group by
const ungroup = (obj) => {
return Object.keys(obj)
.map(x => obj[x]);
};
// flatten array
const flatten = (arrs) => {
return arrs.reduce((acc, item) => acc.concat(item), [])
}
以及一个函数实用程序,用于从
最后,我需要一个通过compose()
创建的ungroup和flant
函数
大致如下:
const ungroupAndFlatten = compose(ungroup, flatten) // Usage doesn't work.
console.log(ungroupAndFlatten(obj))
示例代码:
const arrs = [
{name: 'abc', effectiveDate: "2016-01-01T00:00:00+00:00"},
{name: 'abcd', effectiveDate: "2016-02-01T00:00:00+00:00"},
{name: 'abcd', effectiveDate: "2016-09-01T00:00:00+00:00"},
{name: 'abc', effectiveDate: "2016-04-01T00:00:00+00:00"},
{name: 'abc', effectiveDate: "2016-05-01T00:00:00+00:00"},
];
const groupedByName = groupBy(arrs, 'name');
// Example Output
//
// var obj = {
// abc: [
// { name: 'abc', effectiveDate: '2016-01-01T00:00:00+00:00' },
// { name: 'abc', effectiveDate: '2016-04-01T00:00:00+00:00' },
// { name: 'abc', effectiveDate: '2016-05-01T00:00:00+00:00' }
// ],
// abcd: [
// { name: 'abcd', effectiveDate: '2016-02-01T00:00:00+00:00' },
// { name: 'abcd', effectiveDate: '2016-09-01T00:00:00+00:00' }
// ]
// }
const ungroupAndFlatten = compose(ungroup, flatten) // Usage doesn't work.
console.log(ungroupAndFlatten(groupedByName))
// Output:
// var arrs = [
// {name: 'abc', effectiveDate: "2016-01-01T00:00:00+00:00"},
// {name: 'abcd', effectiveDate: "2016-02-01T00:00:00+00:00"},
// {name: 'abcd', effectiveDate: "2016-09-01T00:00:00+00:00"},
// {name: 'abc', effectiveDate: "2016-04-01T00:00:00+00:00"},
// {name: 'abc', effectiveDate: "2016-05-01T00:00:00+00:00"},
// ];
可以在单个函数中解组和展开
const ungroupAndFlatten = (obj) => Object.keys(obj).reduce((a, k) => {obj[k].forEach(e => a.push(e)); return a}, [])
我相信您犯了一个简单的错误,您将ungroup设置为f函数,而它是g函数:
const ungroupAndFlatten = compose(flatten, ungroup)
切换“解组”和“展平”,所有操作都将正常运行函数组合
函数compose
具有从右到左的求值顺序
还有另一个名为pipe
的函数,它从左到右求值
你的代码
正如前面提到的,您已经颠倒了参数顺序
为了解决你的问题,我们可以
- 切换参数的顺序
const ungroupAndFlatten = compose(
flatten,
ungroup
)
该死的,你是对的!lol奇怪的是,它是从右到左,而不是从左到右。@MatthewHarwood它是从右到左的,因为这就是它在数学中的工作方式。@F你能让“数学就是这样”更清楚一点吗?lol哪个数学?我看到compose在执行时是由内而外的,但是compose的API是否试图模仿它呢?右是内部吗?@MatthewHarwood从右到左的顺序占了上风,因为这种符号在数学上有很好的对称性:
。按照相反的顺序,您将得到(f∘g) (z)=f(g(z))
。这就是全部。(g∗f) (z)=f(g(z))
/* * 1. take x * 2. execute g(x) * 3. take the return value of g(x) and execute f with it */ const compose = (f, g) => x => f(g(x))
const ungroupAndFlatten = compose( flatten, ungroup )const pipe = (g, f) => x => f(g(x))