Skip to content

Commit

Permalink
molly: add a log iterator
Browse files Browse the repository at this point in the history
The patch add a generator which, when asked for an operation, logs
a message and yields `nil`.
  • Loading branch information
ligurio committed Jul 7, 2024
1 parent 54188b4 commit 45bff1a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- A `log()` iterator.
- A `mix()` iterator.
- A CAS-register generator.

Expand Down
25 changes: 20 additions & 5 deletions molly/gen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

local fun = require('fun')
local clock = require('molly.clock')
local log = require('molly.log')
local tbl = require('molly.compat.tbl')

local fun_mt = debug.getmetatable(fun.range(10))
Expand Down Expand Up @@ -456,15 +457,29 @@ methods.flip_flop = flip_flop
--- Special generators
-- @section

--- (TODO) A generator which, when asked for an operation, logs a message and yields
-- nil. Occurs only once; use `repeat` to repeat.
--- A generator which, when asked for an operation, logs
-- a message and yields `nil`. Occurs only once; use `repeat` to
-- repeat.
-- @return an iterator
--
-- @usage
-- > molly.gen.iter({1, 2, 3}):log():totable()
-- 1
-- ---
-- - - 2
-- - 3
-- ...
-- --
-- @function log
local log = function()
-- TODO
local log_it = function(it)
assert(tostring(it) == '<generator>')
local gen, param, state = unwrap(it)
local state1, v = gen(param, state)
log.info(v)
return fun.wrap(gen, param, state1)
end
exports.log = log
exports.log = log_it
methods.log = log_it

--- (TODO) Operations from that generator are scheduled at uniformly random intervals
-- between `0` to `2 * (dt seconds)`.
Expand Down
9 changes: 8 additions & 1 deletion test/tests.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ local utils = molly.utils
local seed = os.time()
math.randomseed(seed)

test:plan(11)
test:plan(12)

test:test('clock', function(test)
test:plan(5)
Expand Down Expand Up @@ -199,6 +199,13 @@ test:test('gen.mix', function(test)
test:is(#tbl, 11)
end)

test:test('gen.log', function(test)
test:plan(1)

local tbl = molly.gen.iter({1, 2, 3}):log():totable()
test:is(#tbl, 2)
end)

test:test('runner', function(test)
test:plan(4)

Expand Down

0 comments on commit 45bff1a

Please sign in to comment.