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
2
u/OlaoluwaM Jun 14 '23 edited Jun 14 '23
Hmm, so first, if your function returns a monadic type, it's no longer pure.
Task<void>is the type of your lambda handler, correct? Then, you can simply compose your startingTask<void>with otherTask<void>s that represent all the other capabilities you want your lambda to have: https://gcanti.github.io/fp-ts/modules/Task.ts.htmlAs for the generalization, do you have an example to look at?