When I call the method stream that I made, with any table as argument, I get the error "attempt to call table".
As far as I know this error only occurs when I use a for wrong, but I don't have a for in the code that is executed...
function stream(input)
...
local function _stream(input)
local result = {
_stream = true,
_data = input._data or input,
-- Without the Operation-wrapping no function could access the input
-- Intermediate Operations
concat = function(str) return _concat(input,str) end,
distinct = function(func) return _distinct(input,func) end,
filter = function(func) return _filter(input,func) end,
limit = function(n) return _limit(input,n) end,
map = function(func) return _map(input,func) end,
skip = function(n) return _skip(input,n) end,
sort = function(func) return _sort(input,func) end,
split = function(func) return _split(input,func) end,
-- Terminal Operations
allmatch = function(func) return _allmatch(input,func) end,
anymatch = function(func) return _anymatch(input,func) end,
collect = function(coll) return _collect(input,coll) end,
count = function() return _count(input) end,
foreach = function(func) return _foreach(input,func) end,
max = function(func) return _max(input,func) end,
min = function(func) return _min(input,func) end,
nonematch = function(func) return _nonematch(input,func) end,
reduce = function(init,op) return _reduce(input,init,op) end,
toArray = function() return _toArray(input) end
}
return result
end
if input == nil then
error("input must be of type table, but was nil")
elseif type(input) ~= "table" then
error("input must be of type table, but was a "..type(input)..": "..input)
end
return _stream(input)
end
table.stream = stream
Full Source - Older Version (where the argument gets deleted for some reason)
I want to use the method for a more stream-based programming style. Very similar to this project, but more not only for numbers and with named keys.
In your code
function stream(input)
-- ...
local function _count()
local count = 0
for k, v in pairs(input._data) do
count = count + 1
end
return count
end
-- ...
local function _stream(input)
local result = {
_stream = true,
_data = input._data or input,
-- ...
count = _count,
-- ...
}
return result
end
return _stream(input)
end
print(stream({1,2,3}).count())
the created object _stream(input)
does contain _data
field, but input
upvalue still refers to your argument {1,2,3}
which doesn't have _data
field.
It could be fixed by working with the object instead of input argument:
function stream(input)
-- ...
local obj
local function _count()
local count = 0
for k, v in pairs(obj._data) do
count = count + 1
end
return count
end
-- ...
local function _stream(input)
local result = {
_stream = true,
_data = input._data or input,
-- ...
count = _count,
-- ...
}
return result
end
obj = _stream(input)
return obj
end
print(stream({1,2,3}).count())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.