Repository navigation
Expand file tree
/
Copy pathrange.lua
More file actions
84 lines (73 loc) · 1.63 KB
/
Copy pathrange.lua
File metadata and controls
84 lines (73 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
local channel=require'clp.channel'
local t=getmetatable(channel.new())
local function geniter(c)
local select=select
local unpack=unpack or require'table'.unpack
local pcall=pcall
return function()
local r={pcall(c.get,c)}
if r[1]==true then
return select(2,unpack(r))
end
return nil
end
end
t.range=function(self,b,step)
local is=channel.ischannel
assert(is(self) or type(self)=='number', "Channel or number expected")
if channel.ischannel(self) then
return geniter(self), self
else
local a=self
if (step == nil) then
step = 1
end
assert(type(step) == 'number', 'Step parameter must be a number.')
assert(step ~= 0, 'Step parameter cannot be zero.')
if step > 0 then
if a > b then
error("First parameter must be less than second parameter.")
end
else
if a < b then
error("First parameter must be greater than second parameter.")
end
end
local i = a
return function()
local ret = i
i = i + step
if step > 0 then
if ret <= b then
return ret
end
else
if ret >= b then
return ret
end
end
end
end
end
t.__wrap=function(c)
local id=c:__id()
return function()
require('clp.range')
return require'clp.channel'.get(id)
end
end
--test
if not (...) then
local clp=require'clp'
local c=clp.channel()
local range=t.range
print(c)
clp.spawn(function()
for i=1,10 do for j in range(1,2) do c:put(i,j) end end
c:close()
end)()
for i in c:range() do
print(i)
end
end
return t.range