seqlua
changeset 19:2fad7f50076b
Added coroutine based filter(func, ...) example to new file seqlua_ipairs_example.lua
author | jbe |
---|---|
date | Wed Aug 20 12:23:31 2014 +0200 (2014-08-20) |
parents | 431fa6946a61 |
children | ed7a82f85e6f |
files | README seqlua_ipairs_example.lua |
line diff
1.1 --- a/README Wed Aug 20 06:47:14 2014 +0200 1.2 +++ b/README Wed Aug 20 12:23:31 2014 +0200 1.3 @@ -36,12 +36,12 @@ 1.4 -- 2 b 1.5 -- 3 c 1.6 1.7 - function alphabet(from, to) 1.8 + function alphabet() 1.9 local letter = nil 1.10 return function() 1.11 if letter == nil then 1.12 - letter = from 1.13 - elseif letter == to then 1.14 + letter = "a" 1.15 + elseif letter == "z" then 1.16 return nil 1.17 else 1.18 letter = string.char(string.byte(letter) + 1) 1.19 @@ -63,40 +63,6 @@ 1.20 -- 25 y 1.21 -- 26 z 1.22 1.23 - c = setmetatable( 1.24 - { iter = alphabet("a", "f") }, 1.25 - { __call = function(t) return t.iter() end } 1.26 - ) 1.27 - 1.28 - for i, v in ipairs(c) do 1.29 - print(i, v) 1.30 - end 1.31 - -- prints: 1.32 - -- 1 a 1.33 - -- 2 b 1.34 - -- 3 c 1.35 - -- 4 d 1.36 - -- 5 e 1.37 - -- 6 f 1.38 - 1.39 - g = coroutine.wrap(function() 1.40 - coroutine.yield("Alice") 1.41 - coroutine.yield("Bob") 1.42 - for i = 1, 3 do 1.43 - coroutine.yield("Person #" .. tostring(i)) 1.44 - end 1.45 - end) 1.46 - 1.47 - for i, v in ipairs(g) do 1.48 - print(i, v) 1.49 - end 1.50 - -- prints: 1.51 - -- 1 Alice 1.52 - -- 2 Bob 1.53 - -- 3 Person #1 1.54 - -- 4 Person #2 1.55 - -- 5 Person #3 1.56 - 1.57 set = {apple = true, banana = true} 1.58 for i, k, v in ipairs(pairs(set)) do 1.59 print(i, k, v) 1.60 @@ -106,6 +72,9 @@ 1.61 -- 2 apple true 1.62 -- (order of "apple" and "banana" may vary) 1.63 1.64 +More examples for invoking the ``ipairs(...)`` function can be found in the 1.65 +file ``seqlua_ipairs_example.lua``. 1.66 + 1.67 The function ``iterator(...)`` may be used to convert any table, any function, 1.68 or any iterator triplet into a single function (possibly creating a closure): 1.69
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/seqlua_ipairs_example.lua Wed Aug 20 12:23:31 2014 +0200 2.3 @@ -0,0 +1,110 @@ 2.4 +require "seqlua" 2.5 + 2.6 +t = {"a", "b", "c"} 2.7 + 2.8 +for i, v in ipairs(t) do 2.9 + print(i, v) 2.10 +end 2.11 +-- prints: 2.12 +-- 1 a 2.13 +-- 2 b 2.14 +-- 3 c 2.15 + 2.16 +function alphabet(from, to) 2.17 + local letter = nil 2.18 + return function() 2.19 + if letter == nil then 2.20 + letter = from 2.21 + elseif letter == to then 2.22 + return nil 2.23 + else 2.24 + letter = string.char(string.byte(letter) + 1) 2.25 + end 2.26 + return letter 2.27 + end 2.28 +end 2.29 + 2.30 +f = alphabet("a", "z") 2.31 + 2.32 +for i, v in ipairs(f) do 2.33 + print(i, v) 2.34 +end 2.35 +-- prints: 2.36 +-- 1 a 2.37 +-- 2 b 2.38 +-- 3 c 2.39 +-- ... 2.40 +-- 25 y 2.41 +-- 26 z 2.42 + 2.43 +c = setmetatable( 2.44 + { iter = alphabet("a", "f") }, 2.45 + { __call = function(t) return t.iter() end } 2.46 +) 2.47 + 2.48 +for i, v in ipairs(c) do 2.49 + print(i, v) 2.50 +end 2.51 +-- prints: 2.52 +-- 1 a 2.53 +-- 2 b 2.54 +-- 3 c 2.55 +-- 4 d 2.56 +-- 5 e 2.57 +-- 6 f 2.58 + 2.59 +g = coroutine.wrap(function() 2.60 + coroutine.yield("Alice") 2.61 + coroutine.yield("Bob") 2.62 + for i = 1, 3 do 2.63 + coroutine.yield("Person #" .. tostring(i)) 2.64 + end 2.65 +end) 2.66 + 2.67 +for i, v in ipairs(g) do 2.68 + print(i, v) 2.69 +end 2.70 +-- prints: 2.71 +-- 1 Alice 2.72 +-- 2 Bob 2.73 +-- 3 Person #1 2.74 +-- 4 Person #2 2.75 +-- 5 Person #3 2.76 + 2.77 +function filter(f, iter, iter_s, iter_i) 2.78 + return coroutine.wrap(function() 2.79 + for i, v in ipairs(iter, iter_s, iter_i) do f(v) end 2.80 + end) 2.81 +end 2.82 + 2.83 +function filterfunc(v) 2.84 + local type_v = type(v) 2.85 + if type_v == "string" then 2.86 + coroutine.yield(v) 2.87 + elseif type_v == "number" then 2.88 + for i = 1, v do 2.89 + coroutine.yield(true) 2.90 + end 2.91 + end 2.92 +end 2.93 + 2.94 +for v in filter(filterfunc, {"a", "b", 3, "c"}) do 2.95 + print(v) 2.96 +end 2.97 +-- prints: 2.98 +-- a 2.99 +-- b 2.100 +-- true 2.101 +-- true 2.102 +-- true 2.103 +-- c 2.104 + 2.105 +set = {apple = true, banana = true} 2.106 +for i, k, v in ipairs(pairs(set)) do 2.107 + print(i, k, v) 2.108 +end 2.109 +-- prints: 2.110 +-- 1 banana true 2.111 +-- 2 apple true 2.112 +-- (order of "apple" and "banana" may vary) 2.113 +