Comments (6)
For table comprehension, the idea of calling an anonymous function is employed to avoid a pollution of the current scope. Though this makes much sense in Javascript, it being the only way to restrict scoping, the do-end-block is the right way to achieve this in Lua. The generated code would thus be more efficient and perhaps more readable also. An example:
MoonScript:
items = { 1, 2, 3, 4 }
doubled = [item * 2 for i, item in ipairs items]
Lua:
local items = {
1,
2,
3,
4
}
local doubled = { }
do
local _len_0 = 0
for i, item in ipairs(items) do
_len_0 = _len_0 + 1
doubled[_len_0] = item * 2
end
end
Additionally, in this example _len_0 is useless. The code can further be reduced:
local items = {
1,
2,
3,
4
}
local doubled = { }
for i, item in ipairs(items) do
doubled[i] = item * 2
end
I wonder, why all the fuzz and noise in the generated code. Is this really necessary?
from moonscript.
Because list comprehensions are value expressions, there are places they can go where statements can not go. A do
and end
block will not work in the general case. Right now the context does change based on how it is used, but it isn't perfect yet. In the end I hope to remove all anonymous function creation when possible for performance.
In your example you use ipairs
to create a unused i
variable. The idiomatic moonscript is:
doubled = [i*2 for i in *items]
Thanks for the suggestions.
In the future I hope to have a tutorial in addition to the reference manual, so I can keep the reference manual terse and have it show the analogous Lua for advanced usage.
I've also been talking to someone over email about creating an online compiler to mess around with. That would be cool to have.
from moonscript.
@leafo
There is a Lua interpret converted to Javascript generated by Emscripten. It should not be that difficult to create an online compiler although the size of the interpret is 1.5 MB
from moonscript.
Just a little update, I've created an online compiler with Emscripten: http://moonscript.org/compiler/
It's a bit slow, but it works.
from moonscript.
Maybe you could use the same code repl.it uses. I have tried to run your example code http://repl.it/Bgm and it is much faster.
https://github.com/replit/jsrepl/tree/master/extern/lua
from moonscript.
Yeah, I have seen that. I need to look into how their interpreter is so much faster.
I can't just use theirs because I also need to compile lpeg to javascript alongside lua. They also seem to have gotten around the error handling problem. Emscripten, as far as I know, doesn't support longjmp, and lua's error handling depends on it. That's why I can't detect what errors are when they happen with my current version.
from moonscript.
Related Issues (20)
- self documenting function HOT 1
- How to use lua's `load` function HOT 6
- Fill operator HOT 1
- Document that @name is a method call if self.name is a function HOT 4
- no long comments? HOT 2
- The option --output-to works in a weird way. It creates the source directory inside the target directory.
- Error: Module "alt_getopt" not found HOT 1
- Any chance we could get a existance operator and a pipe operator HOT 1
- How can I call ls.NewClosure and set local variable HOT 1
- Support for "goto" method
- installing moonscript bug HOT 2
- NOT compatible with lua 5.1 HOT 3
- Any method to use if statement in class method define? HOT 4
- Is it possible to add utf 8 support for names ?
- How to use latest version of code on Windows?
- Syntax error should throw for invalid interpolation
- destructing assignment works wrong with for generator HOT 1
- moonc doesnt output to -t when using -w
- Destructured table literals generate syntactically invalid subscripts
- Add "Prompt" function for user input HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from moonscript.