# HG changeset patch # User jbe # Date 1408931985 -7200 # Node ID de5e7dd93863a80a05fcb3165da178abc354b790 # Parent 1a0ce77d9e4b03d947b2f4aee9e8e6146b5063c4 Re-added example for customized iteration behavior diff -r 1a0ce77d9e4b -r de5e7dd93863 README --- a/README Mon Aug 25 03:51:00 2014 +0200 +++ b/README Mon Aug 25 03:59:45 2014 +0200 @@ -111,8 +111,25 @@ (as previously shown). Where desired, it is also possible to use metamethods to customize iteration -behavior. This extension, however, doesn't respect the ``__len`` metamethod due -to the following reasons: +behavior: + + function print_rows(rows) + for i, row in ipairs(rows) do + print_row(row) + end + end + local result = sql_query("SELECT * FROM actor ORDER BY birthday") + + -- we may rely on the ``__index`` or ``__ipairs`` metamethod to + -- iterate through all result rows here: + print_rows(result) -- no need to use ":rows()" or a similar syntax + + -- but we can also still pass individual result rows to the + -- print_rows function: + print_rows{result[1], result[#result]} + +This extension, however, doesn't respect the ``__len`` metamethod due to the +following reasons: * An efficient implementation where ``for i, v in ipairs(tbl) do ... end`` does neither create a closure nor repeatedly evaluate ``#tbl`` seems to be