New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a .bracket operation for Iterant #479

Closed
alexandru opened this Issue Dec 14, 2017 · 2 comments

Comments

Projects
None yet
2 participants
@alexandru
Member

alexandru commented Dec 14, 2017

FS2 has something like this:

def bracket(fa: F[A])(use: A => Stream[F, B], release: A => F[Unit]): Stream[F, B]

@alexandru alexandru added this to the 3.0.0 milestone Jan 7, 2018

@alexandru alexandru self-assigned this Jan 7, 2018

@oleg-py

This comment has been minimized.

Collaborator

oleg-py commented Jan 18, 2018

The bracket discussed currently in cats-effect is the alternative of try ... finally, ensuring cleanup after use completes or throws. On the other hand,fs2 bracket keeps resources open and closes them upon the completion of the stream, so things like:

def getFile = bracket(openFile)(file => Iterant[Coeval].of(file))(file => Coeval(file.close()))
def totallyOk = getFile.mapEval(f => Coeval(f.readFirstLine()))

are possible (and useful).

Maybe something like this would suffice as the implementation of the latter:

Iterant.liftF(fa)
  .flatMap(a => Iterant.suspend(use(a)).doOnComplete(_ => F.suspend(release(a)))

And maybe it should go under a different name?

@alexandru alexandru removed their assignment Jan 20, 2018

@alexandru

This comment has been minimized.

Member

alexandru commented Jan 20, 2018

@oleg-py hey, I've seen you interested in effects and stuff 😀

Would you mind giving this issue a try?

A bracket operation for Iterant would be very useful. We just need to make sure that we get the signature right.

The discussion at typelevel/cats-effect#113 is probably relevant. What I'm thinking of is that we need to distinguish between normal termination (with error or not) and cancellation, in Iterant's case this being an "early stop".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment