miércoles, 10 de julio de 2013

Iterating through closures

At 3scale we're developing a product using lua.

It's kind of fun to work in a new project and even more to work with a small and malleable language (and even *more* working with a top-notch lua guy).

Part of the fun working with lua is that the language is so minimal that you have to build your own helpers for some functions that you'd have in other languages, but lua makes it really easy and straighforward.

Here's an example that just appeared when I was writing some tests (using busted, of course)

                local f = function()
                  local c = false
                  return function()
                    c = not c
                    return c
                local l = f()

It's a plain simple closure, I know, but maybe it'll be ilustrative to some ppl not used to this approach.

It's just an iterator that returns true/false as you keep calling it.

You know, closures are the poor-man's objects, and objects are the poor-man's closures.

No hay comentarios: