Skip to content

Language Tour

const name = "Alice"
fn greet(name: string) -> string {
`Hello, ${name}!`
}
fn identity<T>(x: T) -> T { x }
const result = [1, 2, 3, 4, 5]
|> filter((n) => n > 2)
|> map((n) => n * 10)
|> sort
users |> filter(.active) |> map(.name) |> sort
5 |> add(3, _) // add(3, 5)
const addTen = add(10, _) // partial application
const label = match status {
200..299 -> "success",
404 -> "not found",
_ -> "unknown",
}
match route {
Home -> <HomePage />,
Profile(id) -> <ProfilePage id={id} />,
NotFound -> <NotFoundPage />,
}
type User { id: string, name: string, email: string }
type Shape {
| Circle { radius: number }
| Rectangle { width: number, height: number }
}
const u = User(name: "Alice", id: "1", email: "a@t.com")
const updated = User(..u, name: "Bob")
type UserId { string } // newtype
fn loadProfile(id: string) -> Result<Profile, Error> {
const user = fetchUser(id)? // ? returns Err early
const posts = fetchPosts(user.id)?
Ok(Profile(user, posts))
}
match user.nickname {
Some(nick) -> nick, // Option<T> replaces null
None -> user.name,
}
for Array<Todo> {
export fn remaining(self) -> number {
self |> filter(.done == false) |> length
}
}
trait Display { fn display(self) -> string }
for User: Display {
fn display(self) -> string { `${self.name} (${self.email})` }
}
export fn Counter() -> JSX.Element {
const [count, setCount] = useState(0)
<div>
<h1>{`Count: ${count}`}</h1>
<button onClick={() => setCount(count + 1)}>+</button>
</div>
}
import { Todo } from "./types"
import { parseYaml } from "yaml-lib"
const result = try parseYaml(input) // wraps in Result
import trusted { useState } from "react" // skip try for safe libs
import { for Array } from "./helpers" // import for-block extensions
async fn fetchUser(id: string) -> Result<User, Error> {
const response = await Http.get(`/api/users/${id}`)?
const user = await response |> Http.json?
Ok(user)
}
fn add(a: number, b: number) -> number { a + b }
test "addition" {
assert add(1, 2) == 3
assert add(-1, 1) == 0
}
fn processPayment(order: Order) -> Result<Receipt, Error> { todo }
match direction { "north" -> go(0, 1), _ -> unreachable }