//arrow-fx-coroutines/arrow.fx.coroutines/fromAutoCloseable

fromAutoCloseable

jvm fun <A : AutoCloseable> Resource.Companion.fromAutoCloseable(f: suspend () -> A): Resource<A>

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

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

suspend fun copyFile(src: String, dest: String): Unit =
  Resource.fromAutoCloseable { FileInputStream(src) }
    .zip(Resource.fromAutoCloseable { 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
<