Guide
Put it together
One small program - a type with cases, a type with fields, a function that can fail and a pipeline - that uses what the guide taught.
This page adds nothing new. It puts the pieces of the guide into one program you can run and change.
Goal#
At the end of this page you have run a program that models tasks, checks its input and reports on a list.
The program#
type Priority {
case Low
case Medium
case High
}
type Task {
title: String
priority: Priority
done: Bool = false
fn completed(): Task {
copy done: true
}
}
type TaskError {
case EmptyTitle
}
fn newTask(title: String, priority: Priority): Result<Task, TaskError> {
if title.isEmpty() {
return Fail TaskError.EmptyTitle
}
Task title, priority
}
fn label(priority: Priority): String {
match priority {
.Low => "low"
.Medium => "medium"
.High => "high"
}
}
const attempts = [
newTask("Write the guide", Priority.High),
newTask("", Priority.Low),
newTask("Review the change", Priority.Medium),
]
var tasks = attempts.filterMap({ _.ok() }).toList()
tasks[0] = tasks[0].completed()
for task in tasks {
const mark = if task.done { "x" } else { " " }
print "[{mark}] {task.title} ({label(task.priority)})"
}
const open = tasks.filter({ !_.done }).count()
print "{open} open"
// prints [x] Write the guide (high)
// prints [ ] Review the change (medium)
// prints 1 openWhat each part does#
Priorityis a type with three cases and no fields,Taska type with fields, one of them with a default.completedis a participle: it returns a changed copy, made withcopy, and leaves the task alone.newTaskcan fail, so it returns aResult. The empty title becomes aFailthe caller has to handle.labelis amatchthat handles every case ofPriority.filterMap({ _.ok() })keeps theOkvalues and drops everyFail, so the empty title never reaches the loop.tasks[0] = tasks[0].completed()changes the list where the value lives, which is whytasksis avar.
Next#
- Idiomatic TorbScript - the habits that make code read like the standard library.
- Task recipes - one page per task, once you know the language.
- The language reference - the exact rule behind anything on the way here.