[Haskell-cafe] New version of itself

martin martin.drautzburg at web.de
Sat Jun 4 13:24:39 UTC 2016

Hello all,

I find myself frequentlly writing types like this

data Logger a l = Lgr {
  runLogger :: a -> Log l -> (Log l, Logger a l)

The purpose is to give a logger a chance to carry an internal state. It could e.g. choose to log only every n
invocations. To do this it must keep track of how many times it has been called. I want to leave such things private to
the Logger.

(1) I know that this is not an unusal thing to do, but it has an OO feel to it. Is there a more functional alternative
to it. Should I just not worry?

(2) I can write a combinator which creates a Logger from a list of Loggers. Since each Logger potentially returns a new
version of itself, I must always re-assemble the combined logger from all the returned new versions. I am worried that
this is a costly operation, particularly when most Loggers just return themselves unaltered. I don't have any hard
numbers about the performance penalty though.

These Loggers are used in a discrete-event-simulation and they will get called many times (once for each event), but
only occastionally actually write to the Log.

More information about the Haskell-Cafe mailing list