Last active
March 6, 2019 17:12
-
-
Save agumonkey/cfc5443f7581c3fe3bdfb50efaa7ac86 to your computer and use it in GitHub Desktop.
fantom type-ish in kotlin
This file contains 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
// fantom type-ish in kotlin | |
// class res | |
// class open:res | |
// class close:res | |
// class file<res> | |
// fun open file<close> -> file<open> | |
// fun close file<open> -> file<close> | |
// fun read file<open> -> file<open> list<string> | |
import java.io.* | |
import java.nio.* | |
import java.nio.file.* | |
open class res { } | |
class open:res { constructor():super() {} } | |
class close:res { constructor():super() {} } | |
data class Fyle<Res>(val f:Path) {} | |
fun op(input:Path):Fyle<close> { return Fyle<close>(input) } | |
fun open(input:Fyle<close>):Fyle<open> { return Fyle<open>(input.f) } | |
fun close(input:Fyle<open>):Fyle<close> { return Fyle<close>(input.f) } | |
fun read(input:Fyle<open>):List<String> { | |
return input.f.let { | |
it -> Files.newBufferedReader(it).readLines() | |
} | |
} | |
fun t(fn:String):List<String> { | |
val p = Paths.get(fn) | |
return read(open(op(p))) | |
} | |
fun t0() { | |
t("/home/agumonkey/.bashrc").forEach { it -> println(it) } | |
} | |
fun t1() { | |
"/home/agumonkey/.bashrc".let { it -> t(it).forEach { at -> println(at) } } | |
} | |
fun main(args:Array<String>) { t1() } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment