Open khoerling opened 9 years ago
We could probably add "for x from y!" Instead. Not great, but better than breaking all existing LS code
+2 on "for x from y!"
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.
The "from" keyword conflicts with existing range behavior from coffee. How about: for x of* y!
I have a proof of concept working with for x from xs then ...
I actually think it's too confusing to use from
because of for from 5 to 10
. Thoughts?
I can't think of a better keyword. of*
feels to odd to me.
Ideas?
what about for x given xs then ...
?
given
sounds too much like it's denoting a predicate - eg. an alias for when
or something
for x at xs
?
for x akin z!
lol
@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"?
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
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
How would in
and of
differ in a for each
?
@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.
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?
Possible to squeeze this into 1.4.1? :)
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}
I'd be very happy if I could use 'for ... from' soon (in the next LiveScript release)!
@pmros +1
By the way, this is effectively blocking my transition to LiveScript for a specific project of mine
At this point everyone's voice has been heard. Gkz should decide the best syntax to move this forward.
@gkz?
More ideas: for x iterating f!
, iterate x in f!
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()) {
...
}
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- .
any update?
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.
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...
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?