seqlua

diff seqlua_ipairs_example.lua @ 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
children ed7a82f85e6f
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/seqlua_ipairs_example.lua	Wed Aug 20 12:23:31 2014 +0200
     1.3 @@ -0,0 +1,110 @@
     1.4 +require "seqlua"
     1.5 +
     1.6 +t = {"a", "b", "c"}
     1.7 +
     1.8 +for i, v in ipairs(t) do
     1.9 +  print(i, v)
    1.10 +end
    1.11 +-- prints:
    1.12 +--  1   a
    1.13 +--  2   b
    1.14 +--  3   c
    1.15 +
    1.16 +function alphabet(from, to)
    1.17 +  local letter = nil
    1.18 +  return function()
    1.19 +    if letter == nil then
    1.20 +      letter = from
    1.21 +    elseif letter == to then
    1.22 +      return nil
    1.23 +    else
    1.24 +      letter = string.char(string.byte(letter) + 1)
    1.25 +    end
    1.26 +    return letter
    1.27 +  end
    1.28 +end
    1.29 +
    1.30 +f = alphabet("a", "z")
    1.31 +
    1.32 +for i, v in ipairs(f) do
    1.33 +  print(i, v)
    1.34 +end
    1.35 +-- prints:
    1.36 +--  1   a
    1.37 +--  2   b
    1.38 +--  3   c
    1.39 +--  ...
    1.40 +--  25  y
    1.41 +--  26  z
    1.42 +
    1.43 +c = setmetatable(
    1.44 +  { iter = alphabet("a", "f") },
    1.45 +  { __call = function(t) return t.iter() end }
    1.46 +)
    1.47 +
    1.48 +for i, v in ipairs(c) do
    1.49 +  print(i, v)
    1.50 +end
    1.51 +-- prints:
    1.52 +--  1   a
    1.53 +--  2   b
    1.54 +--  3   c
    1.55 +--  4   d
    1.56 +--  5   e
    1.57 +--  6   f
    1.58 +
    1.59 +g = coroutine.wrap(function()
    1.60 +  coroutine.yield("Alice")
    1.61 +  coroutine.yield("Bob")
    1.62 +  for i = 1, 3 do
    1.63 +    coroutine.yield("Person #" .. tostring(i))
    1.64 +  end
    1.65 +end)
    1.66 +
    1.67 +for i, v in ipairs(g) do
    1.68 +  print(i, v)
    1.69 +end
    1.70 +-- prints:
    1.71 +--  1   Alice
    1.72 +--  2   Bob
    1.73 +--  3   Person #1
    1.74 +--  4   Person #2
    1.75 +--  5   Person #3
    1.76 +
    1.77 +function filter(f, iter, iter_s, iter_i)
    1.78 +  return coroutine.wrap(function()
    1.79 +    for i, v in ipairs(iter, iter_s, iter_i) do f(v) end
    1.80 +  end)
    1.81 +end
    1.82 +
    1.83 +function filterfunc(v)
    1.84 +  local type_v = type(v)
    1.85 +  if type_v == "string" then
    1.86 +    coroutine.yield(v)
    1.87 +  elseif type_v == "number" then
    1.88 +    for i = 1, v do
    1.89 +      coroutine.yield(true)
    1.90 +    end
    1.91 +  end
    1.92 +end
    1.93 +
    1.94 +for v in filter(filterfunc, {"a", "b", 3, "c"}) do
    1.95 +  print(v)
    1.96 +end
    1.97 +-- prints:
    1.98 +--  a
    1.99 +--  b
   1.100 +--  true
   1.101 +--  true
   1.102 +--  true
   1.103 +--  c
   1.104 +
   1.105 +set = {apple = true, banana = true}
   1.106 +for i, k, v in ipairs(pairs(set)) do
   1.107 +  print(i, k, v)
   1.108 +end
   1.109 +-- prints:
   1.110 +--  1   banana  true
   1.111 +--  2   apple   true
   1.112 +-- (order of "apple" and "banana" may vary)
   1.113 +

Impressum / About Us