seqlua

diff README @ 8:144f0bddee2b

Pass value as argument to __call metamethod
author jbe
date Wed Aug 20 04:24:08 2014 +0200 (2014-08-20)
parents 2cb22d01fdd0
children bb2cf91174ac
line diff
     1.1 --- a/README	Wed Aug 20 01:59:55 2014 +0200
     1.2 +++ b/README	Wed Aug 20 04:24:08 2014 +0200
     1.3 @@ -35,12 +35,12 @@
     1.4      --  2   b
     1.5      --  3   c
     1.6  
     1.7 -    function alphabet()
     1.8 +    function alphabet(from, to)
     1.9        local letter = nil
    1.10        return function()
    1.11          if letter == nil then
    1.12 -          letter = "a"
    1.13 -        elseif letter == "z" then
    1.14 +          letter = from
    1.15 +        elseif letter == to then
    1.16            return nil
    1.17          else
    1.18            letter = string.char(string.byte(letter) + 1)
    1.19 @@ -49,7 +49,7 @@
    1.20        end
    1.21      end
    1.22  
    1.23 -    f = alphabet()
    1.24 +    f = alphabet("a", "z")
    1.25  
    1.26      for i, v in ipairs(f) do
    1.27        print(i, v)
    1.28 @@ -62,6 +62,21 @@
    1.29      --  25  y
    1.30      --  26  z
    1.31  
    1.32 +    c = setmetatable(
    1.33 +      { iter = alphabet("a", "f") },
    1.34 +      { __call = function(t) return t.iter() end }
    1.35 +    )
    1.36 +
    1.37 +    for i, v in ipairs(c) do
    1.38 +      print(i, v)
    1.39 +    end
    1.40 +    -- prints:
    1.41 +    --  1   a
    1.42 +    --  2   b
    1.43 +    --  3   c
    1.44 +    --  4   e
    1.45 +    --  5   f
    1.46 +
    1.47      set = {apple = true, banana = true}
    1.48      for i, k, v in ipairs(pairs(set)) do
    1.49        print(i, k, v)
    1.50 @@ -145,9 +160,3 @@
    1.51  value is replaced by a closure calling the ``__call`` metamethod.
    1.52  
    1.53  
    1.54 -TODO
    1.55 -----
    1.56 -
    1.57 -* pass the original value to the ``__call`` metamethod as first argument
    1.58 -
    1.59 -

Impressum / About Us