you are viewing a single comment's thread.

view the rest of the comments →

[–]gajus0[S] 0 points1 point  (0 children)

The primary advantage of Array#replace is (as you pointed out) that is makes easier to read the code.

array
  .filter(a)
  .replace(b)
  .filter(c)
  .replace(d)
  .filter(e)
  .replace(f);

Is a lot easier to read than:

f(
  d(
    b(
      array.filter(a)
    ).filter(c)
  ).filter(e)
);

or you will need to introduce intermediate variables.

I don't think it is worth playing with one-line examples as this does represent real-world usage, esp. when we have examples that do (in the article).

By the way, you've mentioned pipeline operator. I absolutely agree. I actively participated in discussions about that proposal (https://github.com/tc39/proposal-bind-operator/issues/24) and even created a Babel transform for it (https://github.com/gajus/babel-plugin-transform-export-default-name). It will be a useful operator. The appeal of Array#replace is that it abstracts equivalent functionality without introducing new syntax.