Skip to content

Instantly share code, notes, and snippets.

@wemrysi
Created May 31, 2015 18:32
Show Gist options
  • Save wemrysi/b61d30f7d2b466c09577 to your computer and use it in GitHub Desktop.
Save wemrysi/b61d30f7d2b466c09577 to your computer and use it in GitHub Desktop.
def bracket[F[_], R, A](acquire: NF[F, R])(use: R => NF[F, Option[A]], release: R => NF[F, Unit]): NF[F, A] = {
def go(cln: NF[F, A], p: NF[F, Option[A]]): NF[F, A] =
await(p) flatMap { step =>
step.head.cata(a => emit(a) ++ go(cln, step.tail), cln ++ go(empty, step.tail))
}
flatMap(acquire) { r =>
val cleanup = drain[F, Unit, A](mask(release(r)))
onError(go(cleanup, use(r))) { e => append(cleanup, fail(e)) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment