[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: lua idiom: iteration with skips?
- From: David Given <dg@...>
- Date: Wed, 19 Apr 2006 18:29:18 +0100
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Gregg Reynolds wrote:
[...]
> So here's what I come up with using recursion in psuedo-code:
[...]
> if ( ... test output[j]... ) then
> process(i+m)
> else
> process(i+n)
> end
That'll work, but you need to say 'return process(i+m)' to allow tail
recursion to kick in, otherwise you'll use lots of stack space. Plus,
every time you call process() the system will have to generate a new
closure. This isn't particularly expensive, but it's more expensive than
a simple loop.
I'd be inclined just to do the simple, naïve thing:
local i = 1
while true do
...do action here...
if (test output[j]) then
i = i + m
else
i = i + n
end
end
(You don't mention any kind of termination condition.)
> My question is, what is the
> natural, idiomatic way to do this sort of thing in lua? I'm new to it,
> so I want to learn the lua way.
The Lua way is to do what *you* want. The language is sufficiently
flexible that it can be molded to suit your needs. There are things it's
good it, but it's fundamentally there for you pleasure. What idiom are
you comfortable using?
Sorry, that probably wasn't helpful...
(Just out of interest, is the byte-code compiler intelligent enough to
optimise 'while true do'... into a noop? Because it's a very common idiom.)
- --
+- David Given --McQ-+
| dg@cowlark.com | "Those that repeat truisms, are also forced to
| (dg@tao-group.com) | repeat them." --- Anonymous from Slashdot
+- www.cowlark.com --+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
iD8DBQFERnNtf9E0noFvlzgRAmmYAJ9VapcHbDZKQ6f40zZdkXV2OHOtRACglYy4
Lo4oQR2Q+w2nHHLW3eR8WdQ=
=HsA6
-----END PGP SIGNATURE-----