Skip to content
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
3 changes: 3 additions & 0 deletions cirru/index.cirru
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,6 @@ html
.case (.name "Protocol") $ .pair
.card (.lang Kotlin) $ pre.code $ code (@insert ../code/protocol.kt)
.card (.lang TypeScript) $ pre.code $ code (@insert ../code/protocol.ts)
.case (.name "ADT (discriminated union)") $ .pair
.card (.lang Kotlin) $ pre.code $ code (@insert ../code/adt-sealed-du.kt)
.card (.lang TypeScript) $ pre.code $ code (@insert ../code/adt-sealed-du.ts)
13 changes: 13 additions & 0 deletions code/adt-sealed-du.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sealed class Operation {
class Add(val value: Int) : Operation()
class Substract(val value: Int) : Operation()
class Multiply(val value: Int) : Operation()
class Divide(val value: Int) : Operation()
}

fun execute(x: Int, op: Operation) = when (op) {
is Operation.Add -> x + op.value
is Operation.Substract -> x - op.value
is Operation.Multiply -> x * op.value
is Operation.Divide -> x / op.value
}
19 changes: 19 additions & 0 deletions code/adt-sealed-du.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Add { constructor(public value: number) { } }
class Substract { constructor(public value: number) { } }
class Multiply { constructor(public value: number) { } }
class Divide { constructor(public value: number) { } }

type Operation =
| Add
| Substract
| Multiply
| Divide

function execute(x: number, op: Operation) {
if (op instanceof Add) return x + op.value;
if (op instanceof Substract) return x - op.value;
if (op instanceof Multiply) return x * op.value;
if (op instanceof Divide) return x / op.value;

return 0; // needs this for stricted inferenced number type
}
32 changes: 32 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -271,4 +271,36 @@
function f(x: Nameable) {
console.log("Name is " + x.name());
}
</code></pre></div></div></div><div class="case"><div class="name">ADT (discriminated union)</div><div class="pair"><div class="card"><div class="lang">Kotlin</div><pre class="code"><code>sealed class Operation {
class Add(val value: Int) : Operation()
class Substract(val value: Int) : Operation()
class Multiply(val value: Int) : Operation()
class Divide(val value: Int) : Operation()
}

fun execute(x: Int, op: Operation) = when (op) {
is Operation.Add -> x + op.value
is Operation.Substract -> x - op.value
is Operation.Multiply -> x * op.value
is Operation.Divide -> x / op.value
}
</code></pre></div><div class="card"><div class="lang">TypeScript</div><pre class="code"><code>class Add { constructor(public value: number) { } }
class Substract { constructor(public value: number) { } }
class Multiply { constructor(public value: number) { } }
class Divide { constructor(public value: number) { } }

type Operation =
| Add
| Substract
| Multiply
| Divide

function execute(x: number, op: Operation) {
if (op instanceof Add) return x + op.value;
if (op instanceof Substract) return x - op.value;
if (op instanceof Multiply) return x * op.value;
if (op instanceof Divide) return x / op.value;

return 0; // needs this for stricted inferenced number type
}
</code></pre></div></div></div></div></body></html>