String Interpolation

You can mix expressions into string literals using #{} syntax.

MoonScript:

print "I am #{math.random! * 100}% sure."

Lua:

print("I am " .. tostring(math.random() * 100) .. "% sure.")

String interpolation is only available in double quoted strings. For Loop

There are two for loop forms, just like in Lua. A numeric one and a generic one:

MoonScript:

for i = 10, 20
  print i

for k = 1,15,2 -- an optional step provided
  print k

for key, value in pairs object
  print key, value

Lua:

for i = 10, 20 do
  print(i)
end
for k = 1, 15, 2 do
  print(k)
end
for key, value in pairs(object) do
  print(key, value)
end

The slicing and * operators can be used, just like with comprehensions:

for item in *items[2,4]
  print item

Lua:

local _list_0 = items
local _max_0 = 4
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0 do
  local item = _list_0[_index_0]
  print(item)
end

A shorter syntax is also available for all variations when the body is only a single line:

for item in *items do print item

for j = 1,10,3 do print j

Lua:

local _list_0 = items
for _index_0 = 1, #_list_0 do
  local item = _list_0[_index_0]
  print(item)
end
for j = 1, 10, 3 do
  print(j)
end

A for loop can also be used as an expression. The last statement in the body of the for loop is coerced into an expression and appended to an accumulating array table.

Doubling every even number:

doubled_evens = for i=1,20
  if i % 2 == 0
    i * 2
  else
    i

Lua:

local doubled_evens
do
  local _accum_0 = { }
  local _len_0 = 1
  for i = 1, 20 do
    if i % 2 == 0 then
      _accum_0[_len_0] = i * 2
    else
      _accum_0[_len_0] = i
    end
    _len_0 = _len_0 + 1
  end
  doubled_evens = _accum_0
end

You can also filter values by combining the for loop expression with the continue statement.

For loops at the end of a function body are not accumulated into a table for a return value (Instead the function will return nil). Either an explicit return statement can be used, or the loop can be converted into a list comprehension.

MoonScript:

func_a = -> for i=1,10 do i
func_b = -> return for i=1,10 do i

print func_a! -- prints nil
print func_b! -- prints table object

Lua:

local func_a
func_a = function()
  for i = 1, 10 do
    local _ = i
  end
end
local func_b
func_b = function()
  return (function()
    local _accum_0 = { }
    local _len_0 = 1
    for i = 1, 10 do
      _accum_0[_len_0] = i
      _len_0 = _len_0 + 1
    end
    return _accum_0
  end)()
end
print(func_a())
print(func_b())

This is done to avoid the needless creation of tables for functions that don’t need to return the results of the loop. While Loop

The while loop also comes in two variations:

MoonScript:

i = 10
while i > 0
  print i
  i -= 1

while running == true do my_function!

Lua:

local i = 10
while i > 0 do
  print(i)
  i = i - 1
end
while running == true do
  my_function()
end

Like for loops, the while loop can also be used an expression. Additionally, for a function to return the accumulated value of a while loop, the statement must be explicitly returned.

results matching ""

    No results matching ""