简体   繁体   English

使用Ramda通过多次操作从值创建JavaScript对象

[英]Create JavaScript object from value with multiple operations using Ramda

How would I best create this function in Ramda ? 我如何最好地在Ramda中创建此功能?

function get_parts (buffer) {
  return {
    a: buffer.readInt16LE(0),
    b: buffer.slice(2, 4)
  }
}

get_parts(new Buffer('abcd'))

The aim is to maintain the function call style and specify (and join) the operations in the simplest way possible. 目的是保持函数调用样式并以最简单的方式指定(并加入)操作。

I don't see a real reason to change that function. 我看不出更改该功能的真正原因。

It's clean, readable, and well expresses what you're trying to do. 它干净,易读,并且可以很好地表达您要执行的操作。 Assuming you still want to pass it new Buffer('abcd') or some equivalent, I can't see that there's anything to do. 假设您仍然想向其传递new Buffer('abcd')或其他等效项,那么我看不到有任何事情要做。

If you're looking to make a points-free version of it, that certainly can be done, but I would not think it advisable. 如果您要制作它的无积分版本,那当然可以做到,但是我不建议这样做。 That's useful when it makes the code more readable, but it won't do so here. 当它使代码更具可读性时,这很有用,但此处不会这样做。 Here's my first attempt, and it's not pretty: 这是我的第一次尝试,效果不佳:

var get_parts = R.converge(R.unapply(R.zipObj(['a', 'b'])), [ 
                           R.invoker(1, 'readInt16LE')(0), 
                           R.invoker(2, 'slice')(2, 4)
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM