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

New syntax for lazy getters #1223

Closed
aaditmshah opened this Issue Jun 11, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@aaditmshah

aaditmshah commented Jun 11, 2018

Lazy getters are really useful in functional programming. They are used to prevent stackoverflows (via tail recursion modulo cons), and they can be used to create infinite data structures. However, they are very verbose. Consider the following lazy take function:

const take = (n, xs) => n === 0 ? null : xs && {
    head: xs.head,
    get tail() {
        delete this.tail;
        return this.tail = take(n - 1, xs.tail);
    }
};

It would be nice to have syntactic sugar to create the lazy getter:

const tail = (n, xs) => n === 0 ? xs && {
    head: xs.head,
    lazy tail: take(n - 1, xs.tail)
};

This syntax could also be used to succinctly tie the knot to create cyclic data structures:

const xs = { head: 0, lazy tail: ys },
      ys = { head: 1, lazy tail: xs };

const zeros = { head: 0, lazy tail: zeros };

This enables a limited form of letrec without actually changing the semantics of let, const or var.

@ljharb

This comment has been minimized.

Show comment
Hide comment
@ljharb

ljharb Jun 11, 2018

Member

Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md for how to best suggest new ideas.

Member

ljharb commented Jun 11, 2018

Please see https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md for how to best suggest new ideas.

@ljharb ljharb closed this Jun 11, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment