seqlua

changeset 26:8ff86106e2fe

Curried function "filter" in README
author jbe
date Fri Aug 22 12:16:59 2014 +0200 (2014-08-22)
parents 44880bcfc323
children 18292ffaf2ac
files README
line diff
     1.1 --- a/README	Thu Aug 21 21:32:01 2014 +0200
     1.2 +++ b/README	Fri Aug 22 12:16:59 2014 +0200
     1.3 @@ -72,13 +72,15 @@
     1.4      print(string.concat(",", alphabet()))
     1.5      -- prints: a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
     1.6  
     1.7 -    function filter(f, seq)
     1.8 -      return coroutine.wrap(function()
     1.9 -        for i, v in ipairs(seq) do f(v) end
    1.10 -      end)
    1.11 +    function filter(f)
    1.12 +      return function(seq)
    1.13 +        return coroutine.wrap(function()
    1.14 +          for i, v in ipairs(seq) do f(v) end
    1.15 +        end)
    1.16 +      end
    1.17      end
    1.18  
    1.19 -    function filterfunc(v)
    1.20 +    number_to_trues = filter(function(v)
    1.21        local type_v = type(v)
    1.22        if type_v == "string" then
    1.23          coroutine.yield(v)
    1.24 @@ -87,9 +89,9 @@
    1.25            coroutine.yield(true)
    1.26          end
    1.27        end
    1.28 -    end
    1.29 +    end)
    1.30  
    1.31 -    for v in filter(filterfunc, {"a", "b", 3, "c"}) do
    1.32 +    for v in number_to_trues{"a", "b", 3, "c"} do
    1.33        print(v)
    1.34      end
    1.35      -- prints:
    1.36 @@ -100,7 +102,7 @@
    1.37      --  true
    1.38      --  c
    1.39  
    1.40 -    print((","):concat(filter(filterfunc, {"a", "b", 3, "c"})))
    1.41 +    print((","):concat(number_to_trues{"a", "b", 3, "c"}))
    1.42      -- prints: a,b,true,true,true,c
    1.43  
    1.44  

Impressum / About Us