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 a6da163 commit 90705a7
Show file tree
Hide file tree
Showing 3 changed files with 28 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
24 changes: 19 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,28 @@ 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()
-- [INFO 2024-07-07 11:38:47:119933]: 1
-- ---
-- - null
-- ...
-- --
-- @function log
local log = function()
-- TODO
local log_it = function(it)
assert(tostring(it) == '<generator>')
local gen, param, state = unwrap(it)
local _, v = gen(param, state)
log.info(v)
return nil_gen(nil, nil)
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(12)
test:plan(13)

test:test('clock', function(test)
test:plan(5)
Expand Down Expand Up @@ -226,6 +226,13 @@ test:test('gen.mix', function(test)
test:is(#tbl, 11, 'length of items generated by gen.mix')
end)

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

local res = molly.gen.iter({1, 2, 3}):log()
test:is(res, nil, 'gen.log returns nil')
end)

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

Expand Down

0 comments on commit 90705a7

Please sign in to comment.