seqlua

view seqlua_c_example_test.lua @ 57:da4b9d6a5b7e

Fixed bug due to problem combining luaL_buffinit with seqlua_iterinit
author jbe
date Tue Jan 05 04:26:49 2016 +0100 (2016-01-05)
parents 332216604f83
children
line source
1 require "seqlua_c_example" -- defines function "printcsv"
3 function alphabet()
4 local letter = nil
5 return function()
6 if letter == nil then
7 letter = "a"
8 elseif letter == "z" then
9 return nil
10 else
11 letter = string.char(string.byte(letter) + 1)
12 end
13 return letter
14 end
15 end
17 t = {"a", "b", "c"}
19 printcsv(t)
20 -- prints:
21 -- a,b,c
23 printcsv(alphabet())
24 -- prints:
25 -- 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
27 call_proxy = setmetatable({}, {__ipairs = function() return "call", alphabet() end})
29 printcsv(call_proxy)
30 -- prints:
31 -- 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
33 random_access_alphabet = setmetatable({}, {
34 __index = function(self, i)
35 if type(i) == "number" and i >= 1 and i <= 26 then
36 return string.char(string.byte("a") + i - 1)
37 end
38 end
39 })
41 printcsv(random_access_alphabet)
42 -- prints:
43 -- 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
45 index_proxy = setmetatable({}, {
46 __ipairs = function() return "index", random_access_alphabet end
47 })
49 printcsv(index_proxy)
50 -- prints:
51 -- 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
53 raw_proxy = setmetatable({"dummy"}, {
54 __ipairs = function() return "raw", t end
55 })
57 printcsv(raw_proxy)
58 -- prints:
59 -- a,b,c
61 require "seqlua"
63 do
64 local function ipairsaux(s, i)
65 i = i + 1
66 if i <= s.count then
67 return i, s.entry
68 end
69 end
70 custom_proxy = setmetatable({entry = "zebra", count=5}, {
71 __ipairs = function(self)
72 return ipairsaux, self, 0
73 end
74 })
75 end
77 printcsv(custom_proxy)
78 -- prints: zebra,zebra,zebra,zebra,zebra

Impressum / About Us