Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Constant stack usage #47

Closed
gabejohnson opened this issue Jul 11, 2017 · 5 comments
Closed

Constant stack usage #47

gabejohnson opened this issue Jul 11, 2017 · 5 comments
Labels

Comments

@gabejohnson
Copy link

The current proposal desugars

"hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

to

exclaim(capitalize(doubleSay("hello")));

If I want to reuse this pipeline I can write

const sayItAgainLouder = s => s |> doubleSay |> capitalize |> exclaim;

Now I get reuse, but I'm allocating three stack frames. This isn't a big deal unless I want to compose sayItAgainLouder with some other functions.

A simple alternative desugaring

[doubleSay, capitalize, exclaim].reduce((value, f) => f(value), "hello");

This decreases stack usage, but I imagine at the cost of speed.

It would be useful if this proposal required the compiler to maintain constant stack usage through either some sort of stream fusion or stack frame reuse.

@jasmith79
Copy link

jasmith79 commented Jul 12, 2017

I don't think that's within the intended uses: if you want to compose pipelines you'll be using compose rather than an infix operator. Ditto for long chains using lots of stack space: if your pipeline is ridiculously long then it should probably be built up using compose in pieces rather than one long chain that fills more than your text editor can display.

@gabejohnson
Copy link
Author

if you want to compose pipelines you'll be using compose rather than an infix operator.

I might agree with you @jasmith79 if there was a builtin compose that guaranteed constant stack usage.

I'm all for a built-in though. See fantasyland/ECMAScript-proposals#1

Ditto for long chains using lots of stack space: if your pipeline is ridiculously long then it should probably be built up using compose in pieces rather than one long chain that fills more than your text editor can display.

I haven't suggested this usage. I did mention composing functions with other composed functions.

@dtinth
Copy link

dtinth commented Jul 13, 2017

If I understand correctly,

exclaim(capitalize(doubleSay("hello")))

works like

let _tmp = "hello"
_tmp = doubleSay(_tmp)
_tmp = capitalize(_tmp)
_tmp = exclaim(_tmp)
_tmp

Therefore there is no nested stack frame?

@gabejohnson
Copy link
Author

gabejohnson commented Jul 13, 2017

@dtinth you're right. For some reason I was thinking the behavior was the same as how some pipe functions are implemented resulting in something like

(x => exclaim((x => capitalize((x => doubleSay(x))(x)))(x)))('hello')

which

exclaim(capitalize(doubleSay("hello")))

does not.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants