5. A worked example
A tiny to-do list, built up piece by piece. It uses classes, a list of instances, methods, a loop, and string building.
The task item
class Task:
fn init(self, title):
self.title = title
self.done = false
fn complete(self):
self.done = true
fn line(self):
if self.done:
return "[x] " + self.title
else:
return "[ ] " + self.title
The list
class TaskList:
fn init(self):
self.tasks = []
fn add(self, title):
self.tasks.push(Task(title))
fn complete(self, index):
self.tasks[index].complete()
fn remaining(self):
count = 0
for task in self.tasks:
if not task.done:
count = count + 1
return count
fn show(self):
for task in self.tasks:
print(task.line())
print(str(self.remaining()) + " left")
Using it
class Task:
fn init(self, title):
self.title = title
self.done = false
fn complete(self):
self.done = true
fn line(self):
if self.done:
return "[x] " + self.title
else:
return "[ ] " + self.title
class TaskList:
fn init(self):
self.tasks = []
fn add(self, title):
self.tasks.push(Task(title))
fn complete(self, index):
self.tasks[index].complete()
fn remaining(self):
count = 0
for task in self.tasks:
if not task.done:
count = count + 1
return count
fn show(self):
for task in self.tasks:
print(task.line())
print(str(self.remaining()) + " left")
todo = TaskList()
todo.add("write the guide")
todo.add("run the tests")
todo.add("ship it")
todo.complete(0)
todo.complete(1)
todo.show()
# => [x] write the guide
# => [x] run the tests
# => [ ] ship it
# => 1 left
Where to go next
- The specification for the exact rules.
- The decision records for why Korrin is shaped this way.
crates/korrin/examples/in the repository for more sample programs.