Code Monkey home page Code Monkey logo

Comments (6)

2bt avatar 2bt commented on July 24, 2024

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.

leafo avatar leafo commented on July 24, 2024

@2bt

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]

@yehnan

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.

mrshu avatar mrshu commented on July 24, 2024

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

leafo avatar leafo commented on July 24, 2024

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.

mrshu avatar mrshu commented on July 24, 2024

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.

leafo avatar leafo commented on July 24, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.