Other languages are translated automatically from English.
set fields and methods
Sets such as ['tea', 'milk'] as set: each item once, with in, + and - that stay fast however big the set gets. Methods that change a set return a new one.
A set is a group of items where each item appears once. Adding an item the set already has does nothing, asking whether an item is in it is quick however big the set gets, and two sets can be joined, compared and taken away from each other. Use a set where you would otherwise call ->unique() again and again, or check in on a long list inside a loop.
You write a set as a list and say it is a set. as set turns a list into one, and a variable or field declared as set turns whatever list it is given into one:
<let name="colours" value="['red', 'green', 'red', 'blue'] as set" />
<echo value="$colours" /> <!-- ['red', 'green', 'blue'] because the second 'red' is already in the set -->
<echo value="$colours->count()" /> <!-- 3 -->
<let name="seen" type="set" value="[]" /> <!-- an empty set, ready to fill -->
<set name="seen" op="+=" value="'home'" />
<echo value="'home' in $seen" /> <!-- true -->
$colours = ['red', 'green', 'red', 'blue'] as set
echo $colours // ['red', 'green', 'blue'] because the second 'red' is already in the set
echo $colours->count() // 3
$seen: set = [] // an empty set, ready to fill
$seen += 'home'
echo 'home' in $seen // true
set(of: string) is a set whose items are all of one type. Each item is converted to that type as it goes in, just as a typed list does, so '2' and 2 end up as the same item in a set(of: int):
<let name="ids" type="set(of: int)" value="['1', '2', 2, 3]" />
<echo value="$ids" /> <!-- [1, 2, 3] because '2' becomes the int 2, which is already there -->
<echo value="$ids->type()" /> <!-- 'set(of: int)' -->
<type name="Post">
<field name="title" type="string" />
<field name="tags" type="set(of: string)" default="[]" />
</type>
$ids: set(of: int) = ['1', '2', 2, 3]
echo $ids // [1, 2, 3] because '2' becomes the int 2, which is already there
echo $ids->type() // 'set(of: int)'
type Post {
title: string
tags: set(of: string) = []
}
A set keeps its items in the order they were first added, so printing it, looping over it and ->join() always give the same order. echo and JSON write a set as a list. To get a list back, write $set as list, or ->sort() for a sorted one.
Items match with ===: the same type and the same value. 1 and '1' are two different items, and lists and maps match by what they hold, so [1, 2] is one item however many times it is added.
Operators.+ gives every item of both sides and - takes away the items on the right. The right side may be a set or a plain list, so $colours - ['red'] needs no as set. += adds one item, or every item of a list or set, and -= takes away one item. in asks whether an item is in the set. == is true when two sets hold the same items, in any order:
<let name="mine" value="['tea', 'milk'] as set" />
<let name="yours" value="['milk', 'juice']" />
<echo value="$mine + $yours" /> <!-- ['tea', 'milk', 'juice'] because milk is only added once -->
<echo value="$mine - $yours" /> <!-- ['tea'] because milk is in both -->
<echo value="$mine == ['milk', 'tea'] as set" /> <!-- true because order doesn't matter between sets -->
$mine = ['tea', 'milk'] as set
$yours = ['milk', 'juice']
echo $mine + $yours // ['tea', 'milk', 'juice'] because milk is only added once
echo $mine - $yours // ['tea'] because milk is in both
echo $mine == ['milk', 'tea'] as set // true because order doesn't matter between sets
+= with a list on the right adds each item of the list, as it does for a list. To add a list itself as one item, such as a [row, column] pair, use ->add([row, column]).
Set methods never change the set they are called on. A method such as ->add() or ->filter() gives a new set, so you keep the result or chain the next method onto it. The new set shares what it can with the old one, so $seen += $place in a loop stays quick.
A field is a plain fact about the value, read with a dot and no brackets: $value.name. A method takes arguments or does work, and is called with ->: $value->name(arguments), or with the value piped in: $value |> name(arguments).
A new set with the items added. An item the set has already is left where it is.
->add(items...) gives a new set with the items added at the end. An item the set has already stays where it was, so adding it again changes nothing:
<let name="basket" value="['apple', 'pear'] as set" />
<echo value="$basket->add('fig')" /> <!-- ['apple', 'pear', 'fig'] because fig is new -->
<echo value="$basket->add('pear', 'plum')" /> <!-- ['apple', 'pear', 'plum'] because pear is there already -->
<echo value="$basket" /> <!-- ['apple', 'pear'] because ->add() gives a new set and leaves this one alone -->
$basket = ['apple', 'pear'] as set
echo $basket->add('fig') // ['apple', 'pear', 'fig'] because fig is new
echo $basket->add('pear', 'plum') // ['apple', 'pear', 'plum'] because pear is there already
echo $basket // ['apple', 'pear'] because ->add() gives a new set and leaves this one alone
->add() adds each argument as one item, so a list is added whole. That is how to keep points such as [row, column] in a set:
<let name="visited" value="[] as set" />
<set name="visited" value="$visited->add([0, 1])->add([0, 1])" />
<echo value="$visited->count()" /> <!-- 1 because the second [0, 1] matches the first -->
$visited = [] as set
$visited = $visited->add([0, 1])->add([0, 1])
echo $visited->count() // 1 because the second [0, 1] matches the first
Example
<echo>{ (['apple', 'pear'] as set)->add('fig', 'pear') }</echo>
echo (['apple', 'pear'] as set)->add('fig', 'pear')
True when fn is true for every item: ->all(fn(name) => $name->count() > 2). An empty set gives true.
->all(fn(item)) is true when the function is true for every item. An empty set gives true, because no item fails:
<let name="names" value="['ada', 'grace', 'linus'] as set" />
<echo value="$names->all(fn(name) => $name->count() >= 3)" /> <!-- true because every name has at least 3 letters -->
<echo value="$names->all(fn(name) => $name->count() > 3)" /> <!-- false because ada has only 3 -->
$names = ['ada', 'grace', 'linus'] as set
echo $names->all(fn(name) => $name->count() >= 3) // true because every name has at least 3 letters
echo $names->all(fn(name) => $name->count() > 3) // false because ada has only 3
True when fn is true for any item, or without fn, when the set isn't empty.
->any(fn(item)?) is true when the function is true for at least one item. Without a function, it asks whether the set has any items at all:
<let name="names" value="['ada', 'grace'] as set" />
<echo value="$names->any(fn(name) => $name->starts_with('g'))" /> <!-- true because grace starts with g -->
<echo value="([] as set)->any()" /> <!-- false because the set is empty -->
$names = ['ada', 'grace'] as set
echo $names->any(fn(name) => $name->starts_with('g')) // true because grace starts with g
echo ([] as set)->any() // false because the set is empty
The mean of the items (or of fn for each item), or null when the set is empty.
->average(fn(item)?) adds the items up and divides by how many there are. Given a function, it averages what the function gives for each item. An empty set gives null:
<let name="scores" value="[70, 90, 70, 80] as set" />
<echo value="$scores->average()" /> <!-- 80 because the set holds 70, 90 and 80 once each, and 240 / 3 is 80 -->
$scores = [70, 90, 70, 80] as set
echo $scores->average() // 80 because the set holds 70, 90 and 80 once each, and 240 / 3 is 80
True when the item is in the set, matched with ===. It stays quick however big the set gets, and so does item in $set.
->contains(item) is true when the item is in the set. item in $set asks the same thing. Both stay quick however many items the set holds, which is the reason to use a set instead of a list for a long list of things already seen:
<let name="vowels" value="['a', 'e', 'i', 'o', 'u'] as set" />
<echo value="$vowels->contains('e')" /> <!-- true -->
<echo value="'y' in $vowels" /> <!-- false because y isn't one of the five -->
<echo value="$vowels->contains('E')" /> <!-- false because items match exactly, and 'E' isn't 'e' -->
$vowels = ['a', 'e', 'i', 'o', 'u'] as set
echo $vowels->contains('e') // true
echo 'y' in $vowels // false because y isn't one of the five
echo $vowels->contains('E') // false because items match exactly, and 'E' isn't 'e'
Example
<echo>{ (['a', 'e', 'i'] as set)->contains('e') }</echo>
->count() gives how many items the set has. Each item counts once, however many times it was added:
<let name="line" value="'rr?gb?r'" />
<echo value="($line->chars() as set)->count()" /> <!-- 4 because the characters are r, ?, g and b -->
<echo value="($line->chars() as set - ['?'])->count()" /> <!-- 3 because taking away ? leaves r, g and b -->
$line = 'rr?gb?r'
echo ($line->chars() as set)->count() // 4 because the characters are r, ?, g and b
echo ($line->chars() as set - ['?'])->count() // 3 because taking away ? leaves r, g and b
Example
<echo>{ (['red', 'green', 'red'] as set)->count() }</echo>
->count_by(fn(item)) groups the items by what the function gives and counts each group. The keys are the function's results as text:
<let name="words" value="['tea', 'toast', 'jam', 'tea'] as set" />
<echo value="$words->count_by(fn(word) => $word->first())" /> <!-- { t: 2, j: 1 } because tea and toast start with t, and tea is only in the set once -->
$words = ['tea', 'toast', 'jam', 'tea'] as set
echo $words->count_by(fn(word) => $word->first()) // { t: 2, j: 1 } because tea and toast start with t, and tea is only in the set once
<echo>{ (['tea', 'toast', 'jam'] as set)->count_by(fn(word) => $word->first()) }</echo>
echo (['tea', 'toast', 'jam'] as set)->count_by(fn(word) => $word->first())
#set->difference(other: set|list, symmetric?: bool)→ set
The items that aren't in other, as $set - $other gives. With symmetric, the items in exactly one of the two: this set's, then other's.
->difference(other, symmetric?) gives the items that aren't in other, in this set's order. It takes away what $set - $other takes away. With symmetric, it gives the items that are in exactly one of the two: this set's own, then the other's:
<let name="mine" value="['tea', 'milk', 'sugar'] as set" />
<let name="yours" value="['milk', 'juice']" />
<echo value="$mine->difference($yours)" /> <!-- ['tea', 'sugar'] because milk is in both -->
<echo value="$mine->difference($yours, symmetric)" /> <!-- ['tea', 'sugar', 'juice'] because juice is only in yours -->
$mine = ['tea', 'milk', 'sugar'] as set
$yours = ['milk', 'juice']
echo $mine->difference($yours) // ['tea', 'sugar'] because milk is in both
echo $mine->difference($yours, symmetric) // ['tea', 'sugar', 'juice'] because juice is only in yours
Parameter
Takes
otherrequired
set|list
symmetricoptional
a flag: symmetric or not symmetric
Arguments can be named: ->difference(…, symmetric: …).
Example
<echo>{ (['tea', 'milk'] as set)->difference(['milk', 'juice'], symmetric) }</echo>
echo (['tea', 'milk'] as set)->difference(['milk', 'juice'], symmetric)
Calls fn for each item, in the order they were added, and gives back the set.
->each(fn(item)) calls the function once for each item, in the order they were added, and gives back the set, so you can keep chaining:
<let name="guests" value="['ada', 'grace', 'ada'] as set" />
<do>$guests->each(fn(guest) {
echo "Welcome, { $guest }"
})</do>
<!-- prints Welcome, ada then Welcome, grace because ada is only in the set once -->
$guests = ['ada', 'grace', 'ada'] as set
$guests->each(fn(guest) {
echo "Welcome, { $guest }"
})
// prints Welcome, ada then Welcome, grace because ada is only in the set once
->filter(fn(item)) keeps the items for which the function is true, as a new set:
<let name="numbers" value="[3, 8, 3, 12, 5] as set" />
<echo value="$numbers->filter(fn(number) => $number > 4)" /> <!-- [8, 12, 5] because 3 is too small, and it was only in the set once -->
$numbers = [3, 8, 3, 12, 5] as set
echo $numbers->filter(fn(number) => $number > 4) // [8, 12, 5] because 3 is too small, and it was only in the set once
The first item (in the order they were added) for which fn(item) is true, or null.
->find(fn(item)) gives the first item, in the order they were added, for which the function is true. It gives null when no item fits:
<let name="names" value="['ada', 'grace', 'linus'] as set" />
<echo value="$names->find(fn(name) => $name->count() > 3)" /> <!-- 'grace' because ada has only 3 letters -->
<echo value="$names->find(fn(name) => $name->count() > 9)" /> <!-- null because no name is that long -->
$names = ['ada', 'grace', 'linus'] as set
echo $names->find(fn(name) => $name->count() > 3) // 'grace' because ada has only 3 letters
echo $names->find(fn(name) => $name->count() > 9) // null because no name is that long
A map from fn's result to a list of the items that gave it.
->group_by(fn(item)) puts the items into lists by what the function gives, as a map from each result to its items:
<let name="foods" value="['apple', 'avocado', 'banana', 'apple'] as set" />
<echo value="$foods->group_by(fn(food) => $food->first())" /> <!-- { a: ['apple', 'avocado'], b: ['banana'] } because apple is in the set once -->
$foods = ['apple', 'avocado', 'banana', 'apple'] as set
echo $foods->group_by(fn(food) => $food->first()) // { a: ['apple', 'avocado'], b: ['banana'] } because apple is in the set once
The items that are in other too, in this set's order.
->intersect(other) keeps the items that are in other too, in this set's order. other may be a set or a list:
<let name="needed" value="['flour', 'eggs', 'milk'] as set" />
<let name="cupboard" value="['eggs', 'rice', 'flour']" />
<echo value="$needed->intersect($cupboard)" /> <!-- ['flour', 'eggs'] because there is no milk in the cupboard -->
$needed = ['flour', 'eggs', 'milk'] as set
$cupboard = ['eggs', 'rice', 'flour']
echo $needed->intersect($cupboard) // ['flour', 'eggs'] because there is no milk in the cupboard
Parameter
Takes
otherrequired
set|list
Example
<echo>{ (['flour', 'eggs', 'milk'] as set)->intersect(['eggs', 'flour']) }</echo>
echo (['flour', 'eggs', 'milk'] as set)->intersect(['eggs', 'flour'])
<let name="seen" type="set" value="[]" />
<echo value="$seen->is_empty()" /> <!-- true -->
<echo value="$seen->add('home')->is_empty()" /> <!-- false because the new set holds home -->
$seen: set = []
echo $seen->is_empty() // true
echo $seen->add('home')->is_empty() // false because the new set holds home
True when every item is also in of. With strict, of must have at least one item more. Turn it round for a superset: $big->is_subset(of: $small) asks the other way.
->is_subset(of: other, strict?) is true when every item of this set is also in other. With strict, other must also have at least one item this set doesn't. To ask the other way round, whether this set holds every item of another, turn it round: $small->is_subset(of: $big):
<let name="needed" value="['flour', 'eggs'] as set" />
<let name="have" value="['flour', 'eggs', 'milk']" />
<echo value="$needed->is_subset(of: $have)" /> <!-- true because flour and eggs are both there -->
<echo value="$needed->is_subset(of: $have, strict)" /> <!-- true because have also holds milk -->
<echo value="$needed->is_subset(of: ['eggs'])" /> <!-- false because flour is missing -->
<echo value="$needed->is_subset(of: $needed, strict)" /> <!-- false because a set has nothing more than itself -->
$needed = ['flour', 'eggs'] as set
$have = ['flour', 'eggs', 'milk']
echo $needed->is_subset(of: $have) // true because flour and eggs are both there
echo $needed->is_subset(of: $have, strict) // true because have also holds milk
echo $needed->is_subset(of: ['eggs']) // false because flour is missing
echo $needed->is_subset(of: $needed, strict) // false because a set has nothing more than itself
Parameter
Takes
ofrequired
set|list
strictoptional
a flag: strict or not strict
Arguments can be named: ->is_subset(…, strict: …).
Example
<echo>{ (['flour', 'eggs'] as set)->is_subset(of: ['flour', 'eggs', 'milk']) }</echo>
echo (['flour', 'eggs'] as set)->is_subset(of: ['flour', 'eggs', 'milk'])
The items joined into text (', ' by default), in the order they were added.
->join(separator?) joins the items into text, in the order they were added, with ', ' between them unless you give another separator:
<let name="tags" value="['news', 'tech', 'news'] as set" />
<echo value="$tags->join()" /> <!-- 'news, tech' because news is only in the set once -->
<echo value="$tags->join(' / ')" /> <!-- 'news / tech' -->
$tags = ['news', 'tech', 'news'] as set
echo $tags->join() // 'news, tech' because news is only in the set once
echo $tags->join(' / ') // 'news / tech'
Example
<echo>{ (['news', 'tech', 'news'] as set)->join(' / ') }</echo>
echo (['news', 'tech', 'news'] as set)->join(' / ')
A set of fn(item) for each item. Items that map to the same value become one.
->map(fn(item)) gives a new set of what the function gives for each item. Items that map to the same value become one item:
<let name="words" value="['tea', 'jam', 'toast'] as set" />
<echo value="$words->map(fn(word) => $word->count())" /> <!-- [3, 5] because tea and jam both have 3 letters -->
$words = ['tea', 'jam', 'toast'] as set
echo $words->map(fn(word) => $word->count()) // [3, 5] because tea and jam both have 3 letters
True when the two share at least one item. not $a->overlaps($b) is true when they have nothing in common.
->overlaps(other) is true when the two share at least one item. not $a->overlaps($b) is true when they have nothing in common:
<let name="allergies" value="['nuts', 'milk'] as set" />
<echo value="$allergies->overlaps(['flour', 'milk'])" /> <!-- true because milk is in both -->
<echo value="not $allergies->overlaps(['flour', 'sugar'])" /> <!-- true because they share nothing -->
$allergies = ['nuts', 'milk'] as set
echo $allergies->overlaps(['flour', 'milk']) // true because milk is in both
echo not $allergies->overlaps(['flour', 'sugar']) // true because they share nothing
Parameter
Takes
otherrequired
set|list
Example
<echo>{ (['nuts', 'milk'] as set)->overlaps(['flour', 'milk']) }</echo>
echo (['nuts', 'milk'] as set)->overlaps(['flour', 'milk'])
Combines the items, in the order they were added, into one value: fn(total, item) gives the next total, starting from initial.
->reduce(fn(total, item), initial?) combines the items into one value, in the order they were added. The function gets the total so far and the next item, and gives the next total, starting from initial:
<let name="prices" value="[3, 5, 3] as set" />
<echo value="$prices->reduce(fn(total, price) => $total + $price, 10)" /> <!-- 18 because the set holds 3 and 5, and 10 + 3 + 5 is 18 -->
$prices = [3, 5, 3] as set
echo $prices->reduce(fn(total, price) => $total + $price, 10) // 18 because the set holds 3 and 5, and 10 + 3 + 5 is 18
->reject(fn(item)) keeps the items for which the function is false, as a new set. It is the opposite of ->filter():
<let name="numbers" value="[3, 8, 12, 5] as set" />
<echo value="$numbers->reject(fn(number) => $number > 4)" /> <!-- [3] because 8, 12 and 5 are all more than 4 -->
$numbers = [3, 8, 12, 5] as set
echo $numbers->reject(fn(number) => $number > 4) // [3] because 8, 12 and 5 are all more than 4
A new set without the items. An item the set doesn't have is ignored.
->remove(items...) gives a new set without the items. An item the set doesn't have is ignored:
<let name="basket" value="['apple', 'pear', 'fig'] as set" />
<echo value="$basket->remove('pear')" /> <!-- ['apple', 'fig'] -->
<echo value="$basket->remove('fig', 'kiwi')" /> <!-- ['apple', 'pear'] because there was no kiwi to take away -->
$basket = ['apple', 'pear', 'fig'] as set
echo $basket->remove('pear') // ['apple', 'fig']
echo $basket->remove('fig', 'kiwi') // ['apple', 'pear'] because there was no kiwi to take away
Example
<echo>{ (['apple', 'pear', 'fig'] as set)->remove('pear') }</echo>
echo (['apple', 'pear', 'fig'] as set)->remove('pear')
#set->sort(compare?: fn(a, b), by?: fn(item), order?: asc|desc)→ list
The items as a sorted list: by: fn(item) gives the key and order: desc puts the largest first.
->sort(compare?, by?, order?) gives the items as a sorted list, since a sorted order is a list's job. by: gives the value to sort on, and order: desc puts the largest first:
<let name="scores" value="[70, 90, 70, 80] as set" />
<echo value="$scores->sort()" /> <!-- [70, 80, 90] because each score is in the set once -->
<echo value="$scores->sort(order: desc)" /> <!-- [90, 80, 70] -->
<echo value="$scores->sort()->type()" /> <!-- 'number[]' because ->sort() gives a list -->
$scores = [70, 90, 70, 80] as set
echo $scores->sort() // [70, 80, 90] because each score is in the set once
echo $scores->sort(order: desc) // [90, 80, 70]
echo $scores->sort()->type() // 'number[]' because ->sort() gives a list
The items of both: this set's first, then the new ones from other. $set + $other gives this too.
->union(other) gives every item of both: this set's first, then the new ones from other. It gives what $set + $other gives:
<let name="mine" value="['tea', 'milk'] as set" />
<echo value="$mine->union(['milk', 'juice'])" /> <!-- ['tea', 'milk', 'juice'] because milk is only added once -->
$mine = ['tea', 'milk'] as set
echo $mine->union(['milk', 'juice']) // ['tea', 'milk', 'juice'] because milk is only added once
Parameter
Takes
otherrequired
set|list
Example
<echo>{ (['tea', 'milk'] as set)->union(['milk', 'juice']) }</echo>
echo (['tea', 'milk'] as set)->union(['milk', 'juice'])