LiveScript icon indicating copy to clipboard operation
LiveScript copied to clipboard

ES6 Iterators

Open khoerling opened this issue 10 years ago • 30 comments

I realize they conflict with existing syntax, though LiveScript is difficult to use Immutable.JS and similar libraries that work with for .. of iterators. Is support coming in a future release?

khoerling avatar Feb 16 '15 01:02 khoerling

We could probably add "for x from y!" Instead. Not great, but better than breaking all existing LS code

vendethiel avatar Feb 16 '15 06:02 vendethiel

+2 on "for x from y!"

khoerling avatar Feb 16 '15 20:02 khoerling

I was just about to create a new ticket about this. LiveScript needs the ability to easily iterate through iterators created by generator-functions/coroutines. From what I understand, the following syntax is currently used in ES6:

function *g() { yield 1; yield 2; yield 3; }
for (var i of g()) { console.log(i); }

So, I think for x from g! is a good idea.

It'd be wonderful being able to write something like:

[ x + 1 for x from g! ]

I would also like to strongly suggest additional syntax to compose generator functions using comprehensions. This is important if you have infinite lists or data streaming in. Consider the following:

numbers = ->*
    cnt = 0
    loop then yield ++cnt

even-numbers = *[ x for x from numbers! when x % 2 == 0 ] 

/*
where above creates a generator function similar to:
even-numbers = ->*
    for x from numbers!
        if x % 2 == 0 then yield x
*/

for x from even-numbers! then console.log x

I love writing things in terms of infinite lists, so I'm willing to do work to help put this in, if necessary.

senditu avatar Mar 15 '15 08:03 senditu

The "from" keyword conflicts with existing range behavior from coffee. How about: for x of* y!

khoerling avatar Mar 16 '15 01:03 khoerling

I have a proof of concept working with for x from xs then ...

gkz avatar Apr 10 '15 00:04 gkz

I actually think it's too confusing to use from because of for from 5 to 10. Thoughts?

vendethiel avatar Apr 10 '15 05:04 vendethiel

I can't think of a better keyword. of* feels to odd to me. Ideas?

gkz avatar Apr 10 '15 06:04 gkz

what about for x given xs then ...?

blvz avatar Apr 10 '15 08:04 blvz

given sounds too much like it's denoting a predicate - eg. an alias for when or something

gkz avatar Apr 10 '15 19:04 gkz

for x at xs?

blvz avatar Apr 10 '15 19:04 blvz

for x akin z! lol

gabeio avatar Apr 10 '15 22:04 gabeio

@gkz since you were able to add contextual "for x from y" syntax, is it possible to use similar contexts and be more natural ES6: "for x of y"?

khoerling avatar Apr 10 '15 23:04 khoerling

While we're talking about this, I'd still like to suggest we add syntax to create generator function using list comprehensions. E.g.:

even-numbers = *[ x for x from [1...] when x % 2 == 0 ] 
for x from even-numbers! then console.log x

senditu avatar Apr 11 '15 00:04 senditu

How about using something similar to the keyword foreach like with C# IEnumerables?

#if we use foreach, then it'd be more obvious we're working with generation functions
foreach x in func! then console.log x #similar to c#
foreach x of func! then console.log x #using a keyword each, along with the ES6 of

for each x of func! then console.log x 
#we could also add each as a secondary keyword to be
#used with for instead of creating a second 'foreach' keyword

senditu avatar Apr 17 '15 21:04 senditu

How would in and of differ in a for each?

gkz avatar Apr 17 '15 21:04 gkz

@gkz Oh, they're just examples. We could go with either one for the final. Personally, I'd go with foreach x in func! then console.log x, but that's mostly because I work with .NET a lot. I thought it might be easier to read this way.

senditu avatar Apr 17 '15 21:04 senditu

Where possible, I believe staying closer to ES6 forms is good.

How difficult would it be to support for i of gen!? I see a conflict between gen being a generator or a ordinary function that returns something, but can it be detected or otherwise worked around?

phanimahesh avatar May 02 '15 14:05 phanimahesh

Possible to squeeze this into 1.4.1? :)

khoerling avatar May 13 '15 19:05 khoerling

Um... for ... of conflicts with existing syntax:

for key of object
    # for (var prop in object)
    void

map = (f) -> {[k, f v] for own k, v of o}

dead-claudia avatar May 24 '15 13:05 dead-claudia

I'd be very happy if I could use 'for ... from' soon (in the next LiveScript release)!

ghost avatar Jun 12 '15 18:06 ghost

@pmros +1

dead-claudia avatar Jun 19 '15 04:06 dead-claudia

By the way, this is effectively blocking my transition to LiveScript for a specific project of mine

dead-claudia avatar Jun 19 '15 05:06 dead-claudia

At this point everyone's voice has been heard. Gkz should decide the best syntax to move this forward.

khoerling avatar Jun 20 '15 18:06 khoerling

@gkz?

vendethiel avatar Jan 03 '16 17:01 vendethiel

More ideas: for x iterating f!, iterate x in f!

summivox avatar Jan 09 '16 23:01 summivox

For anyone interested in implementing for-of loop using existing LiveScript syntax,

entries = map.entries!
until (iterator = entries.next!).done
  [key, value] = iterator.value
  ...

is equivalent to

for ([key, value] of map.entries()) {
    ...
}

hakatashi avatar Oct 11 '16 05:10 hakatashi

Almost. There's other more subtle semantics, like with return automatically called after leaving the loop.

On Tue, Oct 11, 2016, 01:15 Koki Takahashi [email protected] wrote:

For anyone interested in implementing for-of loop using existing LiveScript syntax,

entries = map.entries!until (iterator = entries.next!).done [key, value] = iterator.value ...

is equivalent to

for ([key, value] of map.entries()) { ... }

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/gkz/LiveScript/issues/667#issuecomment-252815865, or mute the thread https://github.com/notifications/unsubscribe-auth/AERrBNFOvFyXyvZQqEDfCuZp6R3-T03gks5qyxtigaJpZM4Dgxo- .

dead-claudia avatar Oct 11 '16 21:10 dead-claudia

any update?

zsp042 avatar Sep 17 '19 08:09 zsp042

Nope! A champion to move this forward is needed—first, a syntax that doesn't conflict with existing LS constructions and ideally doesn't consume another keyword must be chosen; second, a PR must be written. I have no plans to implement this myself at this time but if a champion appears, I am happy to help them achieve both of the above.

rhendric avatar Sep 17 '19 16:09 rhendric

While I adore LiveScript and had used this wonderfully terse language for years, due to outstanding issues like this, my team and I decided that vanilla ES6 is simply good enough with an added benefit: everyone knows it 🤷‍♂ Way to keep the spirit alive, wish I had a more helpful pull-request to contribute...

khoerling avatar Sep 24 '19 04:09 khoerling