jvm fun <A : Closeable> Resource.Companion.fromCloseable(f: suspend () -> A): Resource<A>

Creates a Resource from an Closeable, which uses Closeable.close for releasing.

import arrow.fx.coroutines.*
import java.io.FileInputStream

suspend fun copyFile(src: String, dest: String): Unit =
  Resource.fromCloseable { FileInputStream(src) }
    .zip(Resource.fromCloseable { FileInputStream(dest) })
    .use { (a: FileInputStream, b: FileInputStream) ->
       /** read from [a] and write to [b]. **/
       // Both resources will be closed accordingly to their #close methods

Do you like Arrow?

Arrow Org