arrow-fx-coroutines / arrow.fx.coroutines.stream / Stream / bracket

bracket

fun <R> bracket(acquire: suspend () -> R, release: suspend (R) -> Unit): Stream<R>

Creates a stream that emits a resource allocated by an effect, ensuring the resource is eventually released regardless of how the stream is used.

A typical use case for bracket is working with files or network sockets. The resource effect opens a file and returns a reference to it. One can then flatMap on the returned Stream to access the file, e.g to read bytes and transform them in to some stream of elements (e.g., bytes, strings, lines, etc.). The release action then closes the file once the result Stream terminates, even in case of interruption or errors.

Parameters

acquire - resource to acquire at start of stream

release - function which returns an effect that releases the resource

Do you like Arrow?

Arrow Org
<