Sunday, February 22, 2015

Seven More Languages In Seven Weeks - Lua Day 1

Our adventures in Lua begin with a preamble singing the praises of "rich syntax and proper semantics", "famously clean code" and a "quick, portable little language" that outdoes the competition. Sounds great, where can I learn more? We are then told of Lua's fabulous tables, which are infinitely more elegant and wieldy than the given example of a CSV file. They look a little like this:

Monster {
  name = "knight",
  treasure = {-1000, 200}
}

and can even more marvellously pass around first class functions, like so:

Monster {
  name = "cobra",
  speed = function() return 10 * damage_to_player() end
}

But hang on, don't these look an awfully lot like JavaScript objects? Mention on the following page of "the prototype style of object-oriented programming" does make me suspect that we could be dealing with something a lot like JS here. A sidebar later in the chapter provides a head-to-head comparison between Lua and JS where Lua's support for tail call optimization allows it to recursively reverse a large string, where trying to do the same in JavaScript results in stack error.

Oh well, a language that's like JavaScript but better could be of use to us, or at least help us get better at JS (since, by historical accident or otherwise, it seems like JS is here to stay). Let's see where Lua takes us.

The first day is something of a gentle stroll through the usual basics. Lua turns out to be dynamically typed, with logical expressions that short-circuit, and functions as first-class values. Functions are flexible about the number of parameters they're sent, can be variadic, can return multiple values. All of these things seem elegant and sensible. A little more alarmingly, variables are global by default, and you have to use a keyword to make them local: apparently this is For Good Reasons, but wiser heads than mine are going to need to explain them. Whatever, I'm sure it can't be any crazier than JavaScript variable scope.

Nothing too outrageous in the exercises, I don't think. Some preliminary forays into the functional programming that languages with first class programming facilitates - I guess the main thing to say is, doesn't the simple elimination of all the semicolons and curly braces make Lua look a hundred times nicer than JS?

ETA: Though I suppose it is worth mentioning that I initially formulated first_n_primes_ending_in_3 in terms of naively checking each successive number from 2 upwards for "ends_in_3" as well as "is_prime"... but that does seem like a lot of unnecessary work when we know that a full 90% of numbers do not end in 3, and we have a quick way of iterating through the ones that do...

Exercises:


function ends_in_3(num)
  return num % 10 == 3
end

function is_prime(num)
  for i = 2, math.sqrt(num) do
    if num % i == 0 then return false end
  end
  return true
end

function first_n_primes_ending_in_3(n)
  local candidate = 3
  while n > 0 do
    if is_prime(candidate) then
      print(candidate)
      n = n - 1
    end
    candidate = candidate + 10
  end
end

function for_loop(a, b, f)
  while b >= a do
   f(a)
   a = a + 1
  end
end

function reduce(max, init, f)
  local current = 1
  while max >= current do
   init = f(init, current)
   current = current + 1
  end
  return init
end

function factorial(n)
  return reduce(n, 1, function(x, y) return x * y end)
end

No comments:

Post a Comment