Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function stream(input)
- -- Local functions
- -- local function _fn()
- local function _concat(stream)
- local data = input._data
- for k, v in pairs(stream) do
- input._data[k] = v
- end
- return _stream(data)
- end
- local function _distinct(func)
- func = func or (function(v) return v end)
- local data = {}
- for k1, v1 in pairs(input._data) do
- local notExist = true
- for k2, v2 in pairs(data) do
- if k1 ~= k2 and func(v1) == func(v2) then
- exist = false
- break
- end
- end
- if notExist then
- data[k1] = v1
- end
- end
- return _stream(data)
- end
- local function _filter(func)
- local data = {}
- for k, v in pairs(input._data) do
- if func(v) then
- data[k] = v
- end
- end
- return _stream(data)
- end
- local function _limit(n)
- local counter = 0
- local data = {}
- for k, v in pairs(input._data) do
- if counter < n then
- data[k] = v
- counter = counter + 1
- if counter == n then
- break
- end
- end
- end
- return _stream(data)
- end
- local function _map(func)
- local data = {}
- for k, v in pairs(input._data) do
- data[k] = func(v, k)
- end
- return _stream(data)
- end
- local function _skip(n)
- local counter = 0
- local data = {}
- for k, v in pairs(input._data) do
- if counter % n == 0 then
- data[k] = v
- end
- counter = counter + 1
- end
- return _stream(data)
- end
- local function _sort(comp)
- local data = input._data
- table.sort(data, comp)
- return _stream(data)
- end
- local function _split(func)
- local data1, data2 = {}
- for k, v in pairs(input._data) do
- if func(v, k) then
- data1[k] = v
- else
- data2[k] = v
- end
- end
- return _stream(data1), _stream(data2)
- end
- local function _allmatch(func)
- for k, v in pairs(input._data) do
- if func(v, k) then
- return false
- end
- end
- return true
- end
- local function _anymatch(func)
- for k, v in pairs(input._data) do
- if func(v, k) then
- return true
- end
- end
- return false
- end
- local function _collect(coll)
- return coll(input._data)
- end
- local function _count()
- local count = 0
- for k, v in pairs(input._data) do
- count = count + 1
- end
- return count
- end
- local function _foreach(func)
- for k, v in pairs(input._data) do
- func(v, k)
- end
- end
- local function _max(func)
- local maxk = nil
- for k, v in pairs(input._data) do
- if maxk == nil then
- maxk = k
- else
- maxk = func(v, input._data[maxk], k, maxk)
- end
- end
- return maxk, input._data[maxk]
- end
- local function _min(func)
- local mink = nil
- for k, v in pairs(input._data) do
- if mink == nil then
- mink = k
- else
- mink = func(v, input._data[mink], k, mink)
- end
- return mink, input._data[mink]
- end
- end
- local function _reduce(init, op)
- local result = init
- for k, v in pairs(input._data) do
- result = op(result, v, k)
- end
- return result
- end
- local function _toArray()
- local result = input._data
- return result
- end
- local function _stream(input)
- local result = {
- _stream = true,
- _data = input._data or input,
- -- Intermediate Operations
- concat = _concat,
- distinct = _distinct,
- filter = _filter,
- limit = _limit,
- map = _map,
- skip = _skip,
- sort = _sort,
- split = _split,
- -- Terminal Operations
- allmatch = _allmatch,
- anymatch = _anymatch,
- collect = _collect,
- count = _count,
- foreach = _foreach,
- max = _max,
- min = _min,
- nonematch = _nonematch,
- reduce = _reduce,
- toArray = _toArray
- }
- 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
- input = _stream(input)
- return input
- end
Add Comment
Please, Sign In to add comment