tc39 / proposal-partial-application

Proposal to add partial application to ECMAScript
https://tc39.es/proposal-partial-application/
BSD 3-Clause "New" or "Revised" License
1.02k stars 25 forks source link

Pipeline + Partial Application + await/yield #16

Closed azz closed 3 years ago

azz commented 7 years ago

Context: https://github.com/tc39/proposal-pipeline-operator/issues/53

Does it make sense to introduce a way to integrate the syntax of this proposal and the pipeline proposal in the case of async functions?

If it was legal to place await within a partially applied function when part of a pipeline, then:

const foo = await (1
  |> addOne(await?)
  |> double(await?)
);

would be loosely equivalent to:

const foo = await (1
  |> async x => await addOne(x)
  |> async x => await double(x)
);

which in turn is loosely:

const foo = (
  await double(
    await addOne(
      1
    )
  )
);

Syntax alternative:

const foo = 1
  |> await addOne(?)
  |> await double(?)
gilbert commented 7 years ago

I think with the current proposal semantics, it would be translated this way:

double(await ?)
//=>
double(x => await x)

...because the ? operator only applies to the immediately surrounding invocation (hence the motivation for #13).

rbuckton commented 7 years ago

If partial application results in eager evaluation, I would need to add a special form for ? to allow await ? to work, but in that case the semantics are more like:

const foo = await (1
  |> (async x => addOne(await x))
  |> (async y => double(await y))
);

Another approach might be to have some syntax for an async pipeline, similar to for await, e.g.:

const foo = 1
  await |> addOne(?)
  await |> double(?);
phaux commented 7 years ago

You can easily achieve the same thing without a special syntax:

const foo = await p
  .then(addOne(?))
  .then(double(?))
azz commented 7 years ago

Wouldn't addOne(?) be the same as x => addOne(x) ?

dead-claudia commented 6 years ago

Yes, but the point is that with promises, it's moderately a moot point because we already have then.

rbuckton commented 3 years ago

Given that the pipeline proposal has moved forward with Hack-style, and the new syntax and semantics from #49, this is no longer an issue for partial application.

Since partial application is eager, f~(await p, ?) would perform the following steps:

  1. Evaluate f (we'll call that _f for the purpose of this algorithm)
  2. Evaluate p (as _p)
  3. Evaluate await _p (as _await_p)
  4. Produce a partially applied function for _f~(_await_p, ?)

The same would apply to yield.

I've added these along with desugaring examples to EXAMPLE.md in the repository root.