gkz / LiveScript

LiveScript is a language which compiles to JavaScript. It has a straightforward mapping to JavaScript and allows you to write expressive code devoid of repetitive boilerplate. While LiveScript adds many features to assist in functional style programming, it also has many improvements for object oriented and imperative programming.
http://livescript.net
MIT License
2.32k stars 156 forks source link

ES6 Iterators #667

Open khoerling opened 9 years ago

khoerling commented 9 years ago

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?

vendethiel commented 9 years ago

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

khoerling commented 9 years ago

+2 on "for x from y!"

senditu commented 9 years ago

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.

khoerling commented 9 years ago

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

gkz commented 9 years ago

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

vendethiel commented 9 years ago

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

gkz commented 9 years ago

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

blvz commented 9 years ago

what about for x given xs then ...?

gkz commented 9 years ago

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

blvz commented 9 years ago

for x at xs?

gabeio commented 9 years ago

for x akin z! lol

khoerling commented 9 years ago

@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"?

senditu commented 9 years ago

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 commented 9 years ago

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
gkz commented 9 years ago

How would in and of differ in a for each?

senditu commented 9 years ago

@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.

phanimahesh commented 9 years ago

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?

khoerling commented 9 years ago

Possible to squeeze this into 1.4.1? :)

dead-claudia commented 9 years ago

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}
ghost commented 9 years ago

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

dead-claudia commented 9 years ago

@pmros +1

dead-claudia commented 9 years ago

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

khoerling commented 9 years ago

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

vendethiel commented 8 years ago

@gkz?

summivox commented 8 years ago

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

hakatashi commented 8 years ago

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()) {
    ...
}
dead-claudia commented 8 years ago

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 notifications@github.com 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- .

zsp042 commented 5 years ago

any update?

rhendric commented 5 years ago

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.

khoerling commented 5 years ago

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...