Created
January 28, 2019 02:59
-
-
Save bjonnh/ec7ea27df8d758d6f694697f00a63517 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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