Skip to content

Kotlin Translations for Event-Compose, Monad, and Prototype Examples #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Kotlin/event-compose.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.*

class Point(x: Int, y: Int) {
private var x: Int = x
private var y: Int = y
val emitter = EventEmitter()

init {
emitter.on("move") { event ->
val map = event as Map<*, *>
this.x += (map["x"] as Int)
this.y += (map["y"] as Int)
}

emitter.on("clone") { callback ->
val point = Point(this.x, this.y)
(callback as (Point) -> Unit)(point)
}

emitter.on("toString") { callback ->
(callback as (String) -> Unit)("(${this.x}, ${this.y})")
}
}
}

class EventEmitter {
private val listeners = mutableMapOf<String, MutableList<(Any) -> Unit>>()

fun on(event: String, listener: (Any) -> Unit) {
listeners.computeIfAbsent(event) { mutableListOf() }.add(listener)
}

fun emit(event: String, arg: Any) {
listeners[event]?.forEach { it(arg) }
}
}

// Usage
fun main() {
val p1 = Point(10, 20)
p1.emitter.emit("toString", { message: String -> println(message) })
p1.emitter.emit("clone") { c1: Point ->
c1.emitter.emit("toString", { message: String -> println(message) })
c1.emitter.emit("move", mapOf("x" to -5, "y" to 10))
c1.emitter.emit("toString", { message: String -> println(message) })
46 changes: 46 additions & 0 deletions Kotlin/monad.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
data class Point(val x: Int, val y: Int)

class Monad<T>(private val value: T) {

companion object {
fun <T> of(value: T): Monad<T> {
return Monad(value)
}
}

fun <R> map(fn: (T) -> R): Monad<R> {
val valueCopy = deepCopy(value)
return of(fn(valueCopy))
}

fun <R> chain(fn: (T) -> Monad<R>): Monad<R> {
val valueCopy = deepCopy(value)
return fn(valueCopy)
}

fun ap(container: Monad<Point>): Monad<Point> {
val fn = value as (Point) -> Point
return container.map(fn)
}

private fun <T> deepCopy(obj: T): T {
return when (obj) {
is Point -> Point(obj.x, obj.y) as T
else -> obj
}
}
}

val move: (Map<String, Int>) -> (Point) -> Point = { d -> { p -> Point(p.x + d["x"]!!, p.y + d["y"]!!) } }
val clone: (Point) -> Point = { p -> Point(p.x, p.y) }
val toString: (Point) -> Monad<String> = { p -> Monad.of("(${p.x}, ${p.y})") }

// Usage
fun main() {
val p1 = Monad.of(Point(10, 20))
p1.chain(toString).map { println(it) }
val c0 = p1.map(clone)
val t1 = Monad.of(move(mapOf("x" to -5, "y" to 10)))
val c1 = t1.ap(c0)
c1.chain(toString).map { println(it) }
}
24 changes: 24 additions & 0 deletions Kotlin/prototype.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class Point(var x: Int, var y: Int) {

fun clone(): Point {
return Point(x, y)
}

fun move(x: Int, y: Int) {
this.x += x
this.y += y
}

override fun toString(): String {
return "(${x}, ${y})"
}
}

// Usage
fun main() {
val p1 = Point(10, 20)
println(p1.toString())
val c1 = p1.clone()
c1.move(-5, 10)
println(c1.toString())
}