<let name="stock" value="{ apples: 3 }" />
<let name="more" value="$stock->set('pears', 5)" />
<echo value="$stock->count()" /> <!-- 1 because ->set() gives a new map and leaves this one alone -->
<echo value="$more->count()" /> <!-- 2 because the new map has apples and pears -->
$stock = { apples: 3 }
$more = $stock->set('pears', 5)
echo $stock->count() // 1 because ->set() gives a new map and leaves this one alone
echo $more->count() // 2 because the new map has apples and pears
<let name="stock" value="{ apples: 3, pears: 0, plums: 2 }" />
<echo value="$stock->all(fn(count) => $count >= 0)" /> <!-- true because no count is below zero -->
<echo value="$stock->all(fn(count) => $count > 0)" /> <!-- false because there are no pears left -->
<echo value="{}->all(fn(count) => $count > 0)" /> <!-- true because there is no entry to fail -->
$stock = { apples: 3, pears: 0, plums: 2 }
echo $stock->all(fn(count) => $count >= 0) // true because no count is below zero
echo $stock->all(fn(count) => $count > 0) // false because there are no pears left
echo {}->all(fn(count) => $count > 0) // true because there is no entry to fail
<let name="stock" value="{ apples: 3, pears: 0, plums: 2 }" />
<echo value="$stock->any(fn(count) => $count == 0)" /> <!-- true because pears have run out -->
<echo value="$stock->any(fn(count, fruit) => $fruit == 'kiwis')" /> <!-- false because there is no kiwis key -->
$stock = { apples: 3, pears: 0, plums: 2 }
echo $stock->any(fn(count) => $count == 0) // true because pears have run out
echo $stock->any(fn(count, fruit) => $fruit == 'kiwis') // false because there is no kiwis key
<echo value="{ apples: 3, pears: 0 }->count()" /> <!-- 2 because there are two keys -->
<echo value="{ name: 'Ada', nickname: null }->count()" /> <!-- 2 because nickname is a key, even with no value -->
<echo value="{}->count()" /> <!-- 0 -->
echo { apples: 3, pears: 0 }->count() // 2 because there are two keys
echo { name: 'Ada', nickname: null }->count() // 2 because nickname is a key, even with no value
echo {}->count() // 0
<let name="person" value="{ name: 'Ada', age: 36 }" />
<echo value="$person->get('name')" /> <!-- 'Ada' -->
<echo value="$person->get('town', 'Leeds')" /> <!-- 'Leeds' because there is no town key -->
<echo value="$person->get('town')" /> <!-- null because there is no town key and no default -->
$person = { name: 'Ada', age: 36 }
echo $person->get('name') // 'Ada'
echo $person->get('town', 'Leeds') // 'Leeds' because there is no town key
echo $person->get('town') // null because there is no town key and no default
<let name="person" value="{ name: 'Ada', nickname: null }" />
<echo value="$person->has('name')" /> <!-- true -->
<echo value="$person->has('nickname')" /> <!-- true because the key is there, even with no value -->
<echo value="$person->has('Name')" /> <!-- false because keys match exactly, and this one is name -->
$person = { name: 'Ada', nickname: null }
echo $person->has('name') // true
echo $person->has('nickname') // true because the key is there, even with no value
echo $person->has('Name') // false because keys match exactly, and this one is name
<echo value="{}->is_empty()" /> <!-- true -->
<echo value="{ cart: [] }->is_empty()" /> <!-- false because the map has a key, even though its list is empty -->
echo {}->is_empty() // true
echo { cart: [] }->is_empty() // false because the map has a key, even though its list is empty
<let name="user" value="{ name: 'Ada', email: 'ada@example.com', password: 'secret' }" />
<echo value="$user->omit([password])" /> <!-- { name: 'Ada', email: 'ada@example.com' } because the password is left out -->
<echo value="$user->omit([password, phone])" /> <!-- { name: 'Ada', email: 'ada@example.com' } because there was no phone to leave out -->
$user = { name: 'Ada', email: 'ada@example.com', password: 'secret' }
echo $user->omit([password]) // { name: 'Ada', email: 'ada@example.com' } because the password is left out
echo $user->omit([password, phone]) // { name: 'Ada', email: 'ada@example.com' } because there was no phone to leave out
<let name="user" value="{ name: 'Ada', email: 'ada@example.com', password: 'secret' }" />
<echo value="$user->pick([email, name])" /> <!-- { name: 'Ada', email: 'ada@example.com' } because the map's own order is kept -->
<echo value="$user->pick([name, phone])" /> <!-- { name: 'Ada' } because there is no phone to pick -->
$user = { name: 'Ada', email: 'ada@example.com', password: 'secret' }
echo $user->pick([email, name]) // { name: 'Ada', email: 'ada@example.com' } because the map's own order is kept
echo $user->pick([name, phone]) // { name: 'Ada' } because there is no phone to pick
<let name="person" value="{ name: 'Ada', age: 36 }" />
<echo value="$person->set('age', 37)" /> <!-- { name: 'Ada', age: 37 } because age keeps its place -->
<echo value="$person->set('town', 'Leeds')" /> <!-- { name: 'Ada', age: 36, town: 'Leeds' } because town is new, so it goes last -->
$person = { name: 'Ada', age: 36 }
echo $person->set('age', 37) // { name: 'Ada', age: 37 } because age keeps its place
echo $person->set('town', 'Leeds') // { name: 'Ada', age: 36, town: 'Leeds' } because town is new, so it goes last
<let name="votes" value="{ tea: 5, coffee: 9, juice: 2 }" />
<echo value="$votes->sort(by: fn(count) => $count)" /> <!-- { juice: 2, tea: 5, coffee: 9 } because 2 is the fewest votes -->
<echo value="$votes->sort(by: fn(count) => $count, order: desc)->keys()->first()" /> <!-- 'coffee' because it has the most votes -->
<echo value="$votes->sort(by: fn(count, drink) => $drink)->keys()" /> <!-- ['coffee', 'juice', 'tea'] because the keys are in alphabetical order -->
$votes = { tea: 5, coffee: 9, juice: 2 }
echo $votes->sort(by: fn(count) => $count) // { juice: 2, tea: 5, coffee: 9 } because 2 is the fewest votes
echo $votes->sort(by: fn(count) => $count, order: desc)->keys()->first() // 'coffee' because it has the most votes
echo $votes->sort(by: fn(count, drink) => $drink)->keys() // ['coffee', 'juice', 'tea'] because the keys are in alphabetical order