Skip to content

Commit

Permalink
molly: propagate luafun iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
ligurio committed Jul 3, 2024
1 parent 503fd71 commit 2950810
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Allow chaining `luafun` iterators with iterators defined in Molly and vice versa.
- Using of SQL prepared statements in test examples.

### Removed
Expand Down
27 changes: 4 additions & 23 deletions molly/gen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,32 +40,13 @@ local fun = require('fun')
local clock = require('molly.clock')
local log = require('molly.log')

local methods = {}
local exports = {}

local iterator_mt = {
__call = function(self, param, state)
return self.gen(param, state)
end,
__index = methods,
__tostring = function(self)
return '<generator>'
end
}

local wrap = function(gen, param, state)
return setmetatable({
gen = gen,
param = param,
state = state
}, iterator_mt), param, state
end
exports.wrap = wrap
local fun_mt = debug.getmetatable(fun.range(10))
local methods = fun_mt.__index
local exports = table.copy(fun)

local unwrap = function(self)
return self.gen, self.param, self.state
end
methods.unwrap = unwrap

-- Helpers

Expand Down Expand Up @@ -652,7 +633,7 @@ end)(function(timeout, gen, param, state)
local get_time = clock.monotonic
local start_time = get_time()
local time_is_exceed = false
return wrap(function(ctx, state_x)
return fun.wrap(function(ctx, state_x)
local gen_x, param_x, duration, cnt = ctx[1], ctx[2], ctx[3], ctx[4] + 1
ctx[4] = cnt
if time_is_exceed == false then
Expand Down

0 comments on commit 2950810

Please sign in to comment.