简体   繁体   中英

Create JavaScript object from value with multiple operations using Ramda

How would I best create this function in 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.

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)
]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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