The "Pipeline" Operator - Making multiple function calls look great

Gilbert B Garza gilbertbgarza at gmail.com
Tue Nov 10 16:54:29 UTC 2015


Hello, I'm a JavaScript programmer and instructor who loves functional
programming and writing concise, readable code. I think in general
JavaScript supports programming in a functional style quite well. However,
there is one small missing piece that I miss from other FP languages: the
simple-yet-useful pipeline operator.

Similar to F#, Elixir, Elm, and other FP languages, the pipeline operator
|> helps make multiple function invocations more readable. Basically,
`sqrt(64)` is equivalent to `64 |> sqrt`. For example, given the following
functions:

```js
function doubleSay (str) { return str + ", " + str; }
function capitalize (str) { return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) { return str + '!'; }
```

you could use the pipeline operator to expand your invocations for
readability:

```js
// old way:
// var result = exclaim(capitalize(doubleSay("hello")));

// new way:
var result = "hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

// or, if you like one-liners:
var result = "hello" |> doubleSay |> capitalize |> exclaim

result //=> "Hello, hello!"
```

You can see a few more examples, including an advanced example with
Promises, here: https://github.com/mindeavor/ES7-pipeline-operator

I'm inclined to think this feature is small and straight-forward to
implement. Other than the operator, there are no new semantics. The syntax
transformation is simple, and all existing code would remain unaffected.

Although small, this feature increases the expressiveness of JavaScript
significantly by opening more API design possibilities. You can see this in
the link I included above.

Thanks for reading. Any thoughts or comments?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20151110/571706d4/attachment.html>


More information about the es-discuss mailing list