REF make ylabels generator functional

This commit is contained in:
Nathan Dwarshuis 2022-07-24 11:56:57 -04:00
parent 696d6b16d7
commit 2faabd0925
3 changed files with 13 additions and 12 deletions

View File

@ -43,7 +43,6 @@ return function(update_freq, config, common, width, point)
mod_state[key] mod_state[key]
) )
return common.mk_acc( return common.mk_acc(
-- TODO construct this more sanely without referring to hardcoded vars
width, width,
PLOT_HEIGHT + PLOT_SEC_BREAK, PLOT_HEIGHT + PLOT_SEC_BREAK,
function() common.update_rate_timeseries(obj, mod_state[key]) end, function() common.update_rate_timeseries(obj, mod_state[key]) end,

View File

@ -219,6 +219,10 @@ M.iter_to_tableN = function(iter)
return r return r
end end
M.curry_table = function(f)
return function(tbl) return f(table.unpack(tbl)) end
end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
-- functional functions -- functional functions

View File

@ -1,6 +1,7 @@
local M = {} local M = {}
local geom = require 'geom' local geom = require 'geom'
local pure = require 'pure'
local text = require 'text' local text = require 'text'
local ti = require 'text_internal' local ti = require 'text_internal'
@ -17,25 +18,22 @@ local make_y_label_text = function(point, chars, font)
) )
end end
-- TODO this function smells funny
M.make = function(point, h, n, font, y_format, scale_factor) M.make = function(point, h, n, font, y_format, scale_factor)
local y_labels = {width = 0}
local f = y_format(scale_factor) local f = y_format(scale_factor)
for i = 1, n do local to_label = function(i)
local z = (i - 1) / (n - 1) local z = (i - 1) / (n - 1)
local l = make_y_label_text( return make_y_label_text(
geom.make_point(point.x, point.y + z * h), geom.make_point(point.x, point.y + z * h),
f((1 - z) * scale_factor), f((1 - z) * scale_factor),
font font
) )
local w = ti.get_width(l.chars, font)
if w > y_labels.width then
y_labels.width = w
end end
y_labels[i] = l local labels = pure.map_n(to_label, n)
end local max_width = pure.compose(
y_labels.width = y_labels.width + Y_LABEL_PAD pure.curry_table(math.max),
return y_labels pure.partial(pure.map, function(l) return ti.get_width(l.chars, font) end)
)
return { width = max_width(labels) + Y_LABEL_PAD, table.unpack(labels) }
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------