LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

Feature request: Conditional function chaining

Open ceremcem opened this issue 5 years ago • 1 comments

I think it would be nice to have conditional function chaining, like the following:

a
  .x!
  .y! if foo
  .z!

...which will be compiled to:

var x$;
x$ = a;
x$ = x$.x();
if (foo) {
  x$ = x$.y();
}
x$ = x$.z();

This feature will be useful when we need a conditional middle step on a long function chain.

ceremcem avatar Aug 05 '20 13:08 ceremcem

For comparison, currently possible patterns:

  1. Cascade with conditional:

    a
      ..x!
        (if foo then ..y! else ..)
          ..z!
    
    var x$, y$, z$;
    x$ = a;
    y$ = x$.x();
    z$ = foo ? y$.y() : y$;
    z$.z();
    
  2. Pipe-chaining accessor functions:

    a
      |> (.x!)
      |> -> if foo then it.y! else it
      |> (.z!)
    
    (function(it){
      return it.z();
    })(
    function(it){
      if (foo) {
        return it.y();
      } else {
        return it;
      }
    }(
    function(it){
      return it.x();
    }(
    a)));
    

The suggested syntax would be clearer.

anko avatar Aug 05 '20 19:08 anko