User defined effects for Kotlin multiplatform
class IOConsole(
val printString: (String) -> Effect<Unit>,
val readString: Effect<String>
): Handler
val actions = mutableListOf<String>()
handle<Unit, IOConsole> { console ->
val name = console.readString.bind()
console.printString("Hello $name").bind()
} with IOConsole(
printString = { text ->
{ k ->
actions += "printString($text)"
k.resume(Unit)
}
},
readString = { k ->
actions += "readStream(World)"
k.resume("World!")
}
)
class Log(val log: (String) -> Effect<Unit>) : Handler
The corresponding state is a naive specification. State monad should be provided for this purpose.
class State<V>(
val set: (V) -> Effect<Unit>,
val get: Effect<V>
) : Handler
val log: AtomicRef<String> = atomic("")
val state = atomic(10)
handle<Unit, And<State<Int>, Log>> {
val value1 = it.left.get.bind()
it.left.set(value1 + 32).bind()
val value2 = it.left.get.bind()
it.right.log("Done with $value2").bind()
} with {
State<Int>(
set = { value ->
{ k ->
state.value = value
k.resume(Unit)
}
},
get = { k ->
k.resume(state.value)
}
) and Log { value ->
{ k ->
log.getAndSet(log.value + value)
k.resume(Unit)
}
}
}