seqlua
diff README @ 56:c3976eacc6ab
Clarified/simplified examples in README
author | jbe |
---|---|
date | Wed Aug 27 00:31:43 2014 +0200 (2014-08-27) |
parents | 37c2841c6e8c |
children |
line diff
1.1 --- a/README Wed Aug 27 00:21:04 2014 +0200 1.2 +++ b/README Wed Aug 27 00:31:43 2014 +0200 1.3 @@ -101,8 +101,24 @@ 1.4 macros to iterate over values in the same manner as a generic loop statement 1.5 with ``ipairs`` would do. 1.6 1.7 -Note that in case of repeated or nested loops, using function iterators may not 1.8 -be feasible: 1.9 +This extension doesn't aim to supersede Lua's concept of iterator functions. 1.10 +While metamethods (see section "Respected metamethods" below) may be used to 1.11 +customize iteration behavior on values, this extension isn't thought to replace 1.12 +the common practice to use function closures as iterators. Consider the 1.13 +following example: 1.14 + 1.15 + function write_lines(lines) 1.16 + for i, line in ipairs(lines) do 1.17 + io.stdout:write(line) 1.18 + io.stdout:write("\n") 1.19 + end 1.20 + end 1.21 + local result = sql_query("SELECT * FROM actor ORDER BY birthdate") 1.22 + -- assert(type(result:get_column_entries("name")) == "function") 1.23 + write_lines(result:get_column_entries("name")) 1.24 + 1.25 +Note, however, that in case of repeated or nested loops, using function 1.26 +iterators may not be feasible: 1.27 1.28 function print_list_twice(seq) 1.29 for i = 1, 2 do 1.30 @@ -113,22 +129,7 @@ 1.31 end 1.32 print_list_twice(io.stdin:lines()) -- won't work as expected 1.33 1.34 -Also note that this extension doesn't aim to supersede Lua's concept of 1.35 -iterator functions. While metamethods (see section "Respected metamethods" 1.36 -below) may be used to customize iteration behavior on values, this extension 1.37 -isn't thought to replace the common practice to use function closures as 1.38 -iterators. Consider the following example: 1.39 - 1.40 - local result = sql_query("SELECT * FROM actor ORDER BY birthdate") 1.41 - write_lines(result:get_column_entries("name")) 1.42 - 1.43 -The ``get_column_entries`` method can return a simple function closure that 1.44 -returns the next entry in the "name" column (returning ``nil`` to indicate the 1.45 -end). Such a closure can then be passed to another function that iterates 1.46 -through a sequence of values by invoking ``ipairs`` with the general for-loop 1.47 -(as previously shown). 1.48 - 1.49 -Where desired, it is also possible to use metamethods to customize iteration 1.50 +Where desired, it is possible to use metamethods to customize iteration 1.51 behavior: 1.52 1.53 function print_rows(rows)