Command Loop: Summary
Part of the Introduction To Luau section of Coddy's Lua journey. Lesson 54 of 80.
Challenge
MediumFinish the task manager with two report commands.
- On
"pending", print each title infilterTasks(tasks, false). - On
"summary", callprintSummary(tasks).
Press Run Code: with the input pending, summary, quit, the output shows Learn Luau types, Build a task manager and 1/3 tasks done.
Try it yourself
-- The shape every task in the manager follows
type Task = {
id: number,
title: string,
done: boolean,
}
local tasks: {Task} = {
{id = 1, title = "Learn Luau types", done = false},
{id = 2, title = "Build a task manager", done = false},
{id = 3, title = "Write tests", done = true},
}
-- Build a task with the next free id and add it to the list
local function addTask(list: {Task}, title: string): Task
local newTask: Task = {id = #list + 1, title = title, done = false}
table.insert(list, newTask)
return newTask
end
-- Mark the task with this id as done; say whether one was found
local function completeTask(list: {Task}, id: number): boolean
for _, t in ipairs(list) do
if t.id == id then
t.done = true
return true
end
end
return false
end
-- A new list of the tasks whose done flag matches
local function filterTasks(list: {Task}, done: boolean): {Task}
local result: {Task} = {}
for _, t in ipairs(list) do
if t.done == done then
table.insert(result, t)
end
end
return result
end
-- One line: how many tasks are done
local function printSummary(list: {Task}): ()
local doneCount = 0
for _, t in ipairs(list) do
if t.done then
doneCount += 1
end
end
print(`{doneCount}/{#list} tasks done`)
end
-- Try it
while true do
local command = io.read()
if command == nil or command == "quit" then
break
elseif command == "add" then
local title = io.read()
local added: Task = addTask(tasks, title)
print(`Added task {added.id}: {added.title}`)
elseif command == "list" then
for _, t in ipairs(tasks) do
if t.done then
print(`[x] {t.title}`)
else
print(`[ ] {t.title}`)
end
end
elseif command == "done" then
local id: number = tonumber(io.read()) or 0
if completeTask(tasks, id) then
print(`Task {id} completed`)
else
print(`No task with id {id}`)
end
end
end
All lessons in Introduction To Luau
1Getting Started with Luau
What Is Luau?Why Use Luau?Your First Luau CodeType Checking & Error ModesRecap: Introduction to Luau4Working with Functions
Typing Params & Return ValuesTyping Anonymous FunctionsFunctions Returning NothingOptional ParametersDefault Parameter ValuesDefaults for BooleansVariadic FunctionsDefining Function TypesRecap: Typed Functions7Project: Typed Task Manager
Project: The Task ShapeFunction to Add a Task2Core Types
Number, String and BooleanThe 'any' Type: Escape HatchThe 'unknown' TypeNil & Optional TypesType Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Aliases, Unions, Intersections
Type Aliases for PrimitivesUnion TypesWorking with Union TypesLiteral TypesIntersection TypesCombining Type AliasesRecap: Advanced Type Combos3Typed Tables: Arrays & Maps
Typed ArraysAdding and Reading ElementsWhat is a Map Type?Declaring and Accessing MapsIterating TablesMixed-Shape TablesReading a GridBuilding a Gridtable.unpackRecap: Arrays and MapsPractice on your own: Online Luau compiler