user2693928
user2693928

Reputation:

JavaScript brackets many functions

I have 4 functions calling one after another:

var res = f1(f2(f3(f4(f5(a, b), 1, 2, true))))

Is there any cleaner way to refactor this. I want to be able to see the parameters and the code should be minimal.

I can do it like this:

let s1 = f5(a, b);
let s2 = f4(s1, 1, 2, true);
let res = f1(f2(f3(s2)))

But i dont want to introduce another variables.

can i do it short and easy to read/refactor.

Thanks

Upvotes: 0

Views: 82

Answers (5)

wang
wang

Reputation: 1780

// consider this function as small library or hidden part.
// it's composition/high-order function
var f = (func, ...rest) => {
    if(typeof func === "function") {
        if(this.__res)
            this.__res = func(this.__res, ...rest)
        else
            this.__res = func(...rest)
        return f
    } else {
        var result = this.__res
        this.__res = undefined
        return result
    }
}

// the real part you want is here
var res = f(f5, a, b)(f4, 1,2,true)(f3)(f2)(f1)()

I know you don't want to introduce another variables. but when you consider function f as hidden part of a library or module/package, f(f5, a, b)(f4, 1,2,true)(f3)(f2)(f1)() is more readable and clean, isn't it?

Upvotes: 0

Ahmad Ibrahim
Ahmad Ibrahim

Reputation: 1925

IMHO the most readable and clean way is to add another function that makes this sequence of function calls. I.e:

function f0 (a, b, c, d, e) { return f1(f2(f3(f4(f5(a, b), c, d, e)))); }

then simply call:

var res = f0(a, b, 1, 2, true);

Upvotes: 0

Pac0
Pac0

Reputation: 23174

var res = f1(f2(f3(f4(f5(a, b), 1, 2, true))))

Is there any cleaner way to refactor this. I want to be able to see the parameters and the code should be minimal.

That seems pretty minimal to me.

since f1, f2 and f3 have only their result as parameter, you could merge them into one function f123.

But I don't see how this would be really helpful for you as a developer.

5 functions calls is not that horrible.


I can do it like this:

let s1 = f5(a, b);
let s2 = f4(s1, 1, 2, true);
let res = f1(f2(f3(s2)))

But i dont want to introduce another variables.

I very much like the way with additional intermediate variables. this make the code easy to read for another developer (including yourself 2 weeks later from now)

I don't understand why it is a problem for you. Could you elaborate more ?


Another possiblity is to use some pipelining machinery. That would absolutely not be minimal code, but that could make the code clearer by making the logical order of operations more apparent.

For instance : Observable (RxJS)

Observable.of(f5(a,b))
          .map(r5 => f4(r5, 1, 2, true))
          .map(r4 => f3(r4))
          .map(r3 => f2(r3))
          .map(r2 => f1(r2))
          .subscribe(r => console.log(r))

But in a sense, you would have some "intermediate variables" represented by the parameter of the lambda expressions.

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386604

You could reduce from the right side, but it needs the nested function f4 with f5 as start value.

var x = [f1, f2, f3].reduceRight((x, f) => f(x), f4(f5(a, b), 1, 2, true));
//                                               ^^^^^^^^^^^^^^^^^^^^^^^^   start value
//      ^^^^^^^^^^^^                                                        functions

Upvotes: 0

Michael Bykovski
Michael Bykovski

Reputation: 174

var res = f1(
    f2(
        f3(
            f4(
                f5(a, b), 
                1, 
                2, 
                true
            )
        )
    )
);

or just save the results in vars...

var result_f5 = f5(a, b);
var result_f4 = f4(result_f5, 1, 2, true);
var result_f3 = f3(result_f4);
var result_f2 = f2(result_f3);
var result_f1 = f1(result_f2);

I prefer the second way... There is no other "quicker" or "cleaner" way

Upvotes: 0

Related Questions