Additional Feature BP
December 8, 2018 · View on GitHub
| Name | Status | Features | Purpose |
|---|---|---|---|
| Core Proposal | Stage 0 | Infix pipelines … |> …Lexical topic # | Unary function/expression application |
| Additional Feature BC | None | Bare constructor calls … |> new … | Tacit application of constructors |
| Additional Feature BA | None | Bare awaited calls … |> await … | Tacit application of async functions |
| Additional Feature BP | None | Block pipeline steps … |> {…} | Application of statement blocks |
| Additional Feature PF | None | Pipeline functions +> | Partial function/expression application Function/expression composition Method extraction |
| Additional Feature TS | None | Pipeline try statements | Tacit application to caught errors |
| Additional Feature NP | None | N-ary pipelines (…, …) |> …Lexical topics ##, ###, and ... | N-ary function/expression application |
Additional Feature BP
ECMAScript No-Stage Proposal. Living Document. J. S. Choi, 2018-12.
This document is not yet intended to be officially proposed to TC39 yet; it merely shows a possible extension of the Core Proposal in the event that the Core Proposal is accepted.
There is a TC39 proposal for do expressions at Stage 1. Smart pipelines do
not require do expressions. However, if do expressions also become
part of JavaScript, then, as with any other type of expression, a pipeline
step in topic style may be do expression, as long as the do expression
contains the topic reference #. The topic reference # is bound to the input
value, the do expression is evaluated, then the result of the do block
becomes the result of that pipeline step, and the lexical environment is reset –
all as usual.
In this manner, pipelines with do expressions act as a way to create a
“topic-context block”, similarly to Perl 6’s given block. Within this block,
statements may use the topic reference may be used as an abbreviation for the
same value. This can be useful for embedding side effects, if else
statements, try statements, and switch statements within pipelines.
They may be made even pithier with Additional Feature BP, explained later.
do expressions as topic-style pipeline steps might be so
useful, in fact, that it might be worth building them into the pipe operator
|> itself as an add-on feature. This additional feature – block pipelines
– adds an additional topic-style pipeline step syntax,
using blocks to stand for do expressions.
Additional Feature BP is formally specified in in the draft specification.
| With smart pipelines | Status quo |
|---|---|
Side effects may easily be embedded within block pipeline steps. |
|
|
|
|
|
The same early error rules that apply to any topic-style pipeline step apply
also to topic-style steps that are | |
|
As with all other additional features, Additional Feature BP is forward compatible with the Core Proposal. This compatibility includes pipeline steps that are object literals, which must be parenthesized.
Of course, object literals do not have to be parenthesized inside blocks. |
|
WHATWG Fetch Standard (Core Proposal + Additional Feature BP)
Revisiting an [example above from the WHATWG Fetch Standard][WHATWG Fetch + CP] shows how human comprehensibility could be further improved with Additional Feature BP.
| With smart pipelines | Status quo |
|---|---|
This pipeline version uses Core Proposal syntax only. |
|
|
And this pipeline version also uses Additional Feature BP. This allows the
use of an
It also allows the judicious use of variable/constant assignment where it would make the meaning of code clearer, rather than requiring unnecessary variables redundant with the names of functions. |
|
jQuery (Core Proposal + Additional Feature BP)
Revisiting the examples above from jQuery with Additional Feature BP shows how terseness could be further improved.
| With smart pipelines | Status quo |
|---|---|
This pipeline version uses Core Proposal syntax. |
From jquery/src/core/init.js. |
With Additional Feature BP, an |
From jquery/src/core/init.js. |
This pipeline version uses Core Proposal syntax only. Note that both
statements are of the form |
From jquery/src/core/init.js. |
This pipeline version uses Additional Feature BP. The common phrases |
From jquery/src/core/init.js. |
This is a example from jQuery’s codebase on which pipelines would not have been worth using without Additional Feature BP. |
|
Lodash (Core Proposal + Additional Feature BP)
| With smart pipelines | Status quo |
|---|---|
This pipeline version uses Core Proposal syntax only. |
|
This pipeline version also uses Additional Feature BP. |
|
This pipeline version uses Core Proposal syntax only. |
|
This pipeline version also uses Additional Feature BP. |
|
This pipeline version uses Core Proposal syntax only. |
|
This pipeline version also uses Additional Feature BP. |
|
[Unix pipe]: https://en.wikipedia.org/wiki/Pipeline_(Unix
[untangled flow]: ./goals.md#untangled-flow
[Visual Basic’s select statement]: https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/select-case-statement
[WebKit console variables]: https://webkit.org/blog/829/web-inspector-updates/
[WHATWG Fetch + CP]: ./core-real-examples.md#whatwg-fetch-standard
[WHATWG Fetch Standard]: https://fetch.spec.whatwg.org/
[WHATWG Streams + CP + BP + PF + NP]: ./additional-feature-np.md#whatwg-streams-standard-core-proposal--additional-features-bppfmt
[WHATWG Streams + CP + BP + PF]: ./additional-feature-pf.md#whatwg-streams-standard-core-proposal--additional-feature-bppf
[WHATWG Streams Standard]: https://stream.spec.whatwg.org/
[WHATWG-stream piping]: https://streams.spec.whatwg.org/#pipe-chains
[Wikipedia: term rewriting]: https://en.wikipedia.org/wiki/Term_rewriting
[zero runtime cost]: ./goals.md#zero-runtime-cost