moonscript
moonscript copied to clipboard
This looks too great to exist...
load = (name) ->
with io.open(name)
while txt = \read
print txt
(i meant that you should really make with
statement interact with given object. This will not change basic syntax, but will allow with items["stick"]
-like statements)
Can you un-clickbait the title? Also, I don't believe MoonScript supports assignment with while
loops.
MoonScript really support it, e.g.
n = 0
foo = while n < 10
n += 1
n
foo
will be a table have 1...10 as values.
It's not documented in the reference, then. I checked under the section for while
- http://moonscript.org/reference/#the-language/while-loop - and couldn't see anything.
I believe you three are not talking about the same thing.
@RyanSquared said "this is wrong":
while txt = \read
print txt
You can already put any expression in a with
statement. It's not possible to put assignment in a while
loop.
Alternate syntax, that does work (in theory):
load = (name)->
with file = io.open name
print line for line in file\lines!
Please note that if you want this to be like Pythonic with
so that it's garbage collected when the with
line ends, that's not going to happen because with either syntax, the with
statement returns the file, meaning it's now in the scope of the function calling load()
.
@leafo, it is kinda strange.
@handicraftsman I still don't know what do you mean...
Would it be possible to compile while a = b
to something like this?
local a, _fn
_fn = function ()
return a = b
end
while _fn() do
...
end