W
W
webe2018-11-10 13:53:36
JavaScript
webe, 2018-11-10 13:53:36

How to improve function call code?

there is a code How to make it more beautiful? const result = f0(f1(f2(f3(1000)),1),'test')

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AngReload, 2018-11-10
@AngReload

It seems that functional pipelines from libraries like Underscore, Ramda or whatever could be used here. You can also do transpilation using the Babel pipeline operator , which has not yet reached the standard. It all works like this:

// где-то в библиотеке
function pipe(...fns) {
  return function (arg) {
    let result = arg;
    for (let i = 0; i < fns.length; i++) {
      result = fns[i](result);
    }
    return result;
  }
}

// в коде
const result = pipe(
  f3,
  f2,
  _ => f1(_, 1),
  _ => f0(_, 'test')
)(1000);

But I think that the code will become more obvious if it is simply broken into several lines:
const result3 = f3(1000)
const result2 = f2(result1)
const result1 = f1(result2, 1)
const result0 = f0(result2, 'test')

S
SANTA2112, 2018-11-11
@SANTA2112

use compose

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question