r/functionalprogramming • • Jun 14 '23

Question Question: Wrapping a function with side effects

I'm starting to get deeper into fp-ts and functional design in general, but I'm hoping to get some advice on how to approach this kind of problem:

Pretty often I'll need to have some kind of side effects around the execution of a function: logging its input / output, sending metrics to Datadog, etc. I'd like to be able to abstract these kinds of operations away from the core logic of the function itself. I'm comfortable using types like IO<A> or Task<A> to represent the statefull computations, and I know how to compose those types with themselves, but I'm not sure how to create a function that would trigger an IO around a pure function.

If I had some pure functions:

declare businessLogic: (a: A) => B;
declare sendMetric: (data: MetricData) => IO<void>;

I would want to be able to compose them somehow like:

declare newFunction: F<(a: A) => B>; // some type
newFunction = magic(
  sendMetric({ starting... }),
  businessLogic,
  sendMetric({ finished... }),
)

Is this the kind of thing that Traced is for? Would it make more sense to create an Applicative instance? Am I on the wrong track altogether?

edit:

Changed my desired type signature of newFunction

4 Upvotes

12 comments sorted by

View all comments

Show parent comment

2

u/KyleG Jun 27 '23

All this being said, I would caution against thinking you can just wrap your whole app in writer. You'll run out of memory!

but if you have a short sequence of actions and know many will be logging, then absolutely lift those actions into the writer monad and then once those short sequence of actions are all lifted and chained together, make sure you add your flush to a logfile or dmesg or whatever.