Learn Lua · Tables · Lesson 5 of 5

Sets

A set holds each value once. set.new builds one from a list, dropping repeats, and set.tolist gives the members back as a list, in the order they were first added:

LUA
local colours = set.new({ "cyan", "black", "cyan" })
set.has(colours, "black")  --> true
set.tolist(colours)        --> {"cyan", "black"}

set.union(a, b) has everything in either set, and set.intersection(a, b) only what is in both.

What your code receives

Your code gets its data as input. The type Input line at the top of the code lists every field, and the editor uses it to suggest field names and spot typos. Here is an example. Each check sends different values, so work from the fields rather than these exact values.

LUA
input = {
  colours = { "cyan", "black", "cyan", "magenta" },
  front = { "cyan", "black" },
  back = { "black" },
}

Steps

  1. Return unique: the colours in input.colours without repeats, in the order first seen.
  2. Add hasBlack: whether "black" is one of them.
  3. Add both: the colours used on both input.front and input.back, as a list.
Show a hint

Make sets with set.new, then use set.has, set.intersection and set.tolist.