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
If you peek at the source, you'll find that
Task<A>is actually just() => Promise<A>which is more than interoperable.Additionally, If your team has no prior experience, then might I suggest book clubs (or similar) for group training: this might be a good start https://github.com/enricopolanski/functional-programming. Also frontend masters has a pretty decent set of courses that build up to the ideas in the book I shared.
Overthinking is totally fine! FP at a glance seems very complicated with how heavy the literature is, it's very easy to overthink/over-complicate things. One thing that has helped me in my journey was joining and actively asking questions in communities like the fp-ts discord, the FP slack, and the FP discord