# HG changeset patch # User jbe # Date 1408530211 -7200 # Node ID 2fad7f50076bf304cee8dc421866b52495787bb7 # Parent 431fa6946a61455a2d0bd163ed256e9462e9c166 Added coroutine based filter(func, ...) example to new file seqlua_ipairs_example.lua diff -r 431fa6946a61 -r 2fad7f50076b README --- a/README Wed Aug 20 06:47:14 2014 +0200 +++ b/README Wed Aug 20 12:23:31 2014 +0200 @@ -36,12 +36,12 @@ -- 2 b -- 3 c - function alphabet(from, to) + function alphabet() local letter = nil return function() if letter == nil then - letter = from - elseif letter == to then + letter = "a" + elseif letter == "z" then return nil else letter = string.char(string.byte(letter) + 1) @@ -63,40 +63,6 @@ -- 25 y -- 26 z - c = setmetatable( - { iter = alphabet("a", "f") }, - { __call = function(t) return t.iter() end } - ) - - for i, v in ipairs(c) do - print(i, v) - end - -- prints: - -- 1 a - -- 2 b - -- 3 c - -- 4 d - -- 5 e - -- 6 f - - g = coroutine.wrap(function() - coroutine.yield("Alice") - coroutine.yield("Bob") - for i = 1, 3 do - coroutine.yield("Person #" .. tostring(i)) - end - end) - - for i, v in ipairs(g) do - print(i, v) - end - -- prints: - -- 1 Alice - -- 2 Bob - -- 3 Person #1 - -- 4 Person #2 - -- 5 Person #3 - set = {apple = true, banana = true} for i, k, v in ipairs(pairs(set)) do print(i, k, v) @@ -106,6 +72,9 @@ -- 2 apple true -- (order of "apple" and "banana" may vary) +More examples for invoking the ``ipairs(...)`` function can be found in the +file ``seqlua_ipairs_example.lua``. + The function ``iterator(...)`` may be used to convert any table, any function, or any iterator triplet into a single function (possibly creating a closure): diff -r 431fa6946a61 -r 2fad7f50076b seqlua_ipairs_example.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/seqlua_ipairs_example.lua Wed Aug 20 12:23:31 2014 +0200 @@ -0,0 +1,110 @@ +require "seqlua" + +t = {"a", "b", "c"} + +for i, v in ipairs(t) do + print(i, v) +end +-- prints: +-- 1 a +-- 2 b +-- 3 c + +function alphabet(from, to) + local letter = nil + return function() + if letter == nil then + letter = from + elseif letter == to then + return nil + else + letter = string.char(string.byte(letter) + 1) + end + return letter + end +end + +f = alphabet("a", "z") + +for i, v in ipairs(f) do + print(i, v) +end +-- prints: +-- 1 a +-- 2 b +-- 3 c +-- ... +-- 25 y +-- 26 z + +c = setmetatable( + { iter = alphabet("a", "f") }, + { __call = function(t) return t.iter() end } +) + +for i, v in ipairs(c) do + print(i, v) +end +-- prints: +-- 1 a +-- 2 b +-- 3 c +-- 4 d +-- 5 e +-- 6 f + +g = coroutine.wrap(function() + coroutine.yield("Alice") + coroutine.yield("Bob") + for i = 1, 3 do + coroutine.yield("Person #" .. tostring(i)) + end +end) + +for i, v in ipairs(g) do + print(i, v) +end +-- prints: +-- 1 Alice +-- 2 Bob +-- 3 Person #1 +-- 4 Person #2 +-- 5 Person #3 + +function filter(f, iter, iter_s, iter_i) + return coroutine.wrap(function() + for i, v in ipairs(iter, iter_s, iter_i) do f(v) end + end) +end + +function filterfunc(v) + local type_v = type(v) + if type_v == "string" then + coroutine.yield(v) + elseif type_v == "number" then + for i = 1, v do + coroutine.yield(true) + end + end +end + +for v in filter(filterfunc, {"a", "b", 3, "c"}) do + print(v) +end +-- prints: +-- a +-- b +-- true +-- true +-- true +-- c + +set = {apple = true, banana = true} +for i, k, v in ipairs(pairs(set)) do + print(i, k, v) +end +-- prints: +-- 1 banana true +-- 2 apple true +-- (order of "apple" and "banana" may vary) +