Skip to content

Instantly share code, notes, and snippets.

@kvverti
Last active April 16, 2022 02:56
Show Gist options
  • Save kvverti/df050de2a43e58b9fcada4e9c5620948 to your computer and use it in GitHub Desktop.
Save kvverti/df050de2a43e58b9fcada4e9c5620948 to your computer and use it in GitHub Desktop.
Korou Syntax Quick-Stop
-- functions are defined like so
fn add(a: Int, b: Int) -> Int {
a + b
}
-- effects can be declared like so
effect fail[E] {
fn fail(ctx: E)
}
-- and used like so
fn divide(a: Int, b: Int) -> Int/fail[Str] {
if b == 0 {
"Cannot divide by zero" -> fail;
}
a / b
}
-- and handled like so
fn divideOrDefault(a: Int, b: Int, d: Int) -> Int {
with handle fail[Str] {
fn fail(ctx: Str) { d }
} do {
divide(a, b)
}
}
-- functions are sugar for continuations
fn add2(a: Int, b: Int, cc: Int ->) {
a + b -> cc;
}
-- because of this, functions can have multiple return values
fn adjacent(x: Int) -> (Int, Int) {
x, x
}
-- effects on the return type are sugar as well
fn divide2(a: Int, b: Int, cc: Int/fail[Str] ->)/fail[Str] {
if b == 0 {
"Cannot divide by zero" -> fail;
}
a / b -> cc;
}
-- closures are defined with { curly braces }
fn addCurried(a: Int) -> (Int -> Int) {
|b: Int| -> Int { a + b }
}
-- nullary closures are written as blocks
fn orElse(a: Int) -> Int {
-- type may be written {Int}, () -> Int, or (Int ->) ->
let incremented: {Int} = {
a + 1
};
if a == 0 {
a
} else {
incremented()
}
}
-- nullary closures have special sugar when passed as the last argument to a function
fn fix[A](f: ({A}) -> A) -> A {
f { fix(f) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment