haskell - Getting the length of a Data.ByteString.Builder -
i have function tabulate
takes list of objects, , list of functions turn fields of these objects builders
. returns builder
representing nicely formatted table. e.g.:
tabulate :: [a -> builder] -> [a] -> builder tabulate = ... info assc = assc { key :: string, value :: string } > allow funcs = [string7 . key, const $ char7 '=', string7 . value ] > allow objs = ["short" `assc` "a", "longer" `assc` "b", "waylongername" `assc` "c"] > hputbuilder stdout $ tabulate funcs objs short = longer = b waylongername = c
to need determine maximum length each column. @ moment using tolazybytestring
on each element (which slow).
is possible length of builder
without first turning bytestring
?
alternatively, there way (efficiently) implement tabulate
(with or without using builder
)?
looking source of builder
, it's defined as
newtype builder = builder (forall r. buildstep r -> buildstep r)
so sequencing builder
composing functions, , there no way how length of output without evaluating stack of functions. create own helper module info type combine builder
s length calculations:
newtype builderl = builderl { bllenght :: !int, blbuilder :: builder } instance monoid builderl mempty = builderl 0 mempty mappend (builderl l1 t1) (builderl l2 t2) = builderl (l1 + l2) (t1 <> t2)
and create helper functions constructing builderl
s, like
bytestring :: bytestring -> builderl bytestring t = builderl (length t) (bytestring t)
etc. utilize module , builderl
tables , you'll have length @ hand.
haskell
No comments:
Post a Comment