如何通过换能器功能组成对象的变换 [英] How to functional compose transforms of objects via transducers

查看:105
本文介绍了如何通过换能器功能组成对象的变换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过

I'm trying to learn transducers via egghead and I think I got it until we try to compose object transformation. I have an example below that doesn't work

const flip = map(([k,v]) => ({[v]: k}));
const double = map(([k,v]) => ({[k]: v + v}));
seq(flip, {one: 1, two: 2}); /*?*/ {1: 'one', 2: 'two'}
seq(double, {one: 1, two: 2}); /*?*/ {'one': 2, 'two: 4}

但是如果我撰写失败了:

but if I compose it fails:

seq(compose(flip, double), {one: 1, two: 2}); /*?*/ {undefined: NaN}
seq(compose(double, flip), {one: 1, two: 2}); /*?*/ {undefined: undefined} 

如何使用具有fp成分的换能器处理对象?

How can I work with objects using transducers with fp composition?

有很多样板,所以我真的建议您看一下现场代码示例,以复习诸如compose,seq等之类的实用程序.

There is quite a bit of boiler plate so I really suggest looking at the live code example to review the utils like compose, seq etc.

推荐答案

首先感谢您完成本课程. 您在编写时遇到了麻烦,因为我们在预期的输入和输出之间存在冲突的数据类型.

first off thanks for going through the course. You're having trouble composing because we've got clashing data types between the expected inputs and outputs.

在组合翻转和双精度时,seq帮助器将调用transduce帮助器函数,该函数会将您的输入对象转换为[k,v]条目的数组,以便可以对其进行迭代.它还使用objectReducer辅助函数将您的组合转换用作内部reducer,它只是执行Object.assign来保持累积量.

When composing flip and double, the seq helper calls the transduce helper function which will convert your input object to an array of [k,v] entries so that it can iterate through it. It also calls your composed transform with the objectReducer helper to be used as the inner reducer, which just does an Object.assign to keep building up the accumulation.

然后,它遍历[k,v]条目,将它们传递给组成的reducer,但是要确保变换之间的数据类型兼容由您自己决定.

It then iterates through the [k,v] entries, passing them to your composed reducer, but it's up to you to ensure you keep the data types compatible between your transforms.

在您的示例中,double将获得flip的返回值,但是double需要一个[k,v]数组,并且flip返回一个对象.

In your example, double will get the return value of flip, but double expects a [k,v] array, and flip returns an object.

因此,您必须执行以下操作:

So you would have to do something like this:

const entriesToObject = map(([k,v]) => {
  return {[k]:v};
});
const flipAndDouble = compose(
  map(([k,v]) => {
    return [k,v+v];
  }),
  map(([k,v]) => {
    return [v,k];
  }),
  entriesToObject,
);

//{ '2': 'one', '4': 'two', '6': 'three' }​​​​​

这有点令人困惑,因为您必须确保最后一步返回一个对象而不是[k,v]数组.这样,执行Object.assignobjReducer将正常工作,因为它期望将对象作为值. 这就是为什么我在上面的entriesToObject中添加了

It's a bit confusing since you have to ensure the last step returns an object and not a [k,v] array. This is so the objReducer that performs the Object.assign will work correctly since it expects an object as the value. This is why I added in entriesToObject above.

如果objReducer已更新为处理[k,v]数组以及对象 作为值,那么您也可以从最后一步继续返回[k,v]数组,这是一种更好的方法

If the objReducer was updated to handle [k,v] arrays as well as objects as values then you could keep returning [k,v] arrays from your last step as well, which is a much better approach

您可以在此处看到如何重写objReducer的示例: https://github.com/jlong​​ster/transducers.js/blob/master/transducers.js#L766

You can see an example of how the objReducer could be rewritten here: https://github.com/jlongster/transducers.js/blob/master/transducers.js#L766

对于生产用途,如果使用该换能器库,则可以将输入和输出始终视为[k,v]数组,这是一种更好的方法.对于您自己的学习,您可以尝试根据该链接修改objReducer,然后应该可以从上面的组合物中删除entriesToObject.

For production use, if you use that transducer library, you can just keep treating your inputs and outputs as [k,v] arrays, which is a much better approach. For your own learning, you could try modifying the objReducer based on that link and you should then be able to remove entriesToObject from the composition above.

希望有帮助!

这篇关于如何通过换能器功能组成对象的变换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆