Skip to content

Instantly share code, notes, and snippets.

@KosmX
Last active July 4, 2023 20:40
Show Gist options
  • Save KosmX/a48e4b9ff5217343c3c780b662f8dc12 to your computer and use it in GitHub Desktop.
Save KosmX/a48e4b9ff5217343c3c780b662f8dc12 to your computer and use it in GitHub Desktop.
A computer can never be help accountable therefore a computer must never make a *management* decision [meme]
import org.jetbrains.annotations.ApiStatus.Internal
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
// helper function
fun userCondition(msg: String): Boolean {
while (true) {
println("$msg [Y/N]")
val r = readln().firstOrNull()?.uppercaseChar()
if (r == 'Y') {
return true
} else if (r == 'N') {
return false
}
}
}
inline fun decide(msg: String, block: () -> Unit) {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
if (userCondition(msg)) block()
}
fun decide(msg: String): PreDecideResult = PreDecideResult(userCondition(msg))
inline infix fun <T> PreDecideResult.between(block: () -> T): BlockResult<T>? {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return if (condition) BlockResult(block()) else null
}
inline infix fun <T> BlockResult<T>?.and(block: () -> T): T {
contract {
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
}
return if (this == null) {
block()
} else {
t
}
}
inline fun <T> choose(msg: String, block: ChooseInternalBlock<T>.() -> Unit): T? {
val collector = ChooseInternalBlock<T>()
block(collector)
while (true) {
println("$msg, Choose one branch from ${collector.choices}")
val r = readln()
if (r in collector.choices) {
ChooseInternalBlock<T>(r).let {
block(it)
return@choose it.r
}
}
println("$r is an invalid choice.")
}
}
@Internal
class PreDecideResult(val condition: Boolean)
@Internal
class BlockResult<T>(val t: T)
class ChooseInternalBlock<T>(val choice: String? = null, val choices: MutableList<String> = mutableListOf(), var r: T? = null) {
inline infix fun String.to(block: () -> T) {
if (choice == null) choices += this
else if (choice == this) r = block()
}
inline fun default(block: () -> T) {
if (choice == null) choices += "default"
else if (choice == "default") r = block()
}
}
@KosmX
Copy link
Author

KosmX commented Jul 4, 2023

And a perfect example of abusing it:

fun main() {
    println("Please type a number")
    val num = readln().toInt()

    decide("$num > 5") between {
        println("$num is greater than five")
    } and {
        println("$num is smaller or equal than five")
    }

    decide("Stop running?") {
        return // I can return from main
    }

    val asString = choose("what is $num") {
        "0" to { "zero" }
        "1" to { "one" }
        "2" to { "two" }
        "3" to { "greater than two and less than four" }
        "4" to { "four" }
        "5" to { "five" }
        "6" to { "six" }
        default { "lot" }
    }!!

    println("$num is $asString")

    messingWithUser()
}

fun messingWithUser() {
    for (i in 0 until 20) {
        decide("Vent Gas?") between {} and {
            goBoom()
        }
    }

    decide("Detonate?") {
        println("You asked for this")
        //Runtime.getRuntime().exec("shutdown") // this will attempt to shutdown the PC
        goBoom()
    }
}

fun goBoom(): Nothing = TODO("Do something horrible")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment