Skip to content

Instantly share code, notes, and snippets.

@bjonnh
Created January 28, 2019 02:59
Show Gist options
  • Save bjonnh/ec7ea27df8d758d6f694697f00a63517 to your computer and use it in GitHub Desktop.
Save bjonnh/ec7ea27df8d758d6f694697f00a63517 to your computer and use it in GitHub Desktop.
import arrow.effects.IO
class File(url: String) {
fun open(): File = this
fun close(): Unit {}
override fun toString(): String = "This file contains some interesting content!"
}
fun openFile(uri: String): IO<File> = IO { throw RuntimeException() }
fun closeFile(file: File): IO<Unit> = IO { println("File is closed")
file.close() }
fun fileToString(file: File): IO<String> = IO { file.toString() }
fun main(args: Array<String>) {
//sampleStart
val safeComputation = openFile("data.json").bracket(
release = { file -> closeFile(file) },
use = { file -> fileToString(file) })
//sampleEnd
println(safeComputation.unsafeRunAsync { result ->
result.fold( {println("Error: $it")}, { println("Result: $it")})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment