-
Hello. In Haskell, I can traverse (or sequence) when a bracket and an array of its input is given. How can I do the same with IDisposable objects in language-ext? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For the exact example with the IO monad: public static IO<C> bracket<A, B, C>(
IO<A> acquire,
Func<A, IO<B>> release,
Func<A, IO<C>> action) =>
IO.lift(env =>
{
var x = acquire.Run(env);
try
{
return action(x).Run(env);
}
finally
{
release(x).Run(env);
}
}); If the acquired resource is public static IO<B> bracket<A, B>(
IO<A> acquire,
Func<A, IO<B>> action)
where A : IDisposable =>
IO.lift(env =>
{
var x = acquire.Run(env);
try
{
return action(x).Run(env);
}
finally
{
x.Dispose();
}
}); This is using the IO monad from Support for a NOTE: None of this is tested yet. And if there's one thing I expect to not be perfect in the alpha it's the resource-tracking, so be aware that it's likely to have some bugs. In Resource tracking is one of the 'big pushes' of the You didn't give a huge amount of context, but hopefully that answers the question! |
Beta Was this translation helpful? Give feedback.
For the exact example with the IO monad:
If the acquired resource is
IDisposable
then you can write abracket
function that doesn't need arelease
lambda argument: