asFlow

fun <A> Resource<A>.asFlow(): Flow<A>

Runs Resource.use and emits A of the resource

import arrow.fx.coroutines.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import java.nio.file.Path
import kotlin.io.path.*

@OptIn(ExperimentalCoroutinesApi::class)
fun Flow<ByteArray>.writeAll(path: Path): Flow<Unit> =
closeable { path.toFile().outputStream() }
.asFlow()
.flatMapConcat { writer -> map { writer.write(it) } }
.flowOn(Dispatchers.IO)

fun Path.readAll(): Flow<String> = flow {
useLines { lines -> emitAll(lines.asFlow()) }
}

suspend fun main() {
Path("example.kt")
.readAll()
.collect(::println)
}