# HG changeset patch # User jbe # Date 1409092303 -7200 # Node ID c3976eacc6ab13446cba1f61faed4553e191fcf7 # Parent 37c2841c6e8c106f36504901b9b07b4ccf41ac8c Clarified/simplified examples in README diff -r 37c2841c6e8c -r c3976eacc6ab README --- a/README Wed Aug 27 00:21:04 2014 +0200 +++ b/README Wed Aug 27 00:31:43 2014 +0200 @@ -101,8 +101,24 @@ macros to iterate over values in the same manner as a generic loop statement with ``ipairs`` would do. -Note that in case of repeated or nested loops, using function iterators may not -be feasible: +This extension doesn't aim to supersede Lua's concept of iterator functions. +While metamethods (see section "Respected metamethods" below) may be used to +customize iteration behavior on values, this extension isn't thought to replace +the common practice to use function closures as iterators. Consider the +following example: + + function write_lines(lines) + for i, line in ipairs(lines) do + io.stdout:write(line) + io.stdout:write("\n") + end + end + local result = sql_query("SELECT * FROM actor ORDER BY birthdate") + -- assert(type(result:get_column_entries("name")) == "function") + write_lines(result:get_column_entries("name")) + +Note, however, that in case of repeated or nested loops, using function +iterators may not be feasible: function print_list_twice(seq) for i = 1, 2 do @@ -113,22 +129,7 @@ end print_list_twice(io.stdin:lines()) -- won't work as expected -Also note that this extension doesn't aim to supersede Lua's concept of -iterator functions. While metamethods (see section "Respected metamethods" -below) may be used to customize iteration behavior on values, this extension -isn't thought to replace the common practice to use function closures as -iterators. Consider the following example: - - local result = sql_query("SELECT * FROM actor ORDER BY birthdate") - write_lines(result:get_column_entries("name")) - -The ``get_column_entries`` method can return a simple function closure that -returns the next entry in the "name" column (returning ``nil`` to indicate the -end). Such a closure can then be passed to another function that iterates -through a sequence of values by invoking ``ipairs`` with the general for-loop -(as previously shown). - -Where desired, it is also possible to use metamethods to customize iteration +Where desired, it is possible to use metamethods to customize iteration behavior: function print_rows(rows)