arrow-fx-coroutines / arrow.fx.coroutines / Resource / fromClosable

fromClosable

fun <A : Closeable> fromClosable(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.fromClosable { FileInputStream(src) }
    .zip(Resource.fromClosable { 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
<