r/functionalprogramming • u/jacobissimus • 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
View all comments
Show parent comment
3
u/jacobissimus Jun 14 '23
Ok, yeah I'm thinking about this all wrong. You're write, we already a statefull program because our lambdas are always asynchronous. All I really want is for the person who writes that Task<void> to not have to think about the fact that it's going to run in a lambda, but I already take Tasks an execute them within different contexts all the time.
Our team is trying to lean heavier into FP and I'm falling into the trap of over-thinking everything.