맵은 이름이라고 부르는 키에 값을 담아요: { name: 'Ada', age: 36 }. 점으로 값을 읽거나($person.name), 키가 변수에 있거나 공백을 포함할 때는 대괄호로 읽어요($person['home town']). 키는 텍스트이며, 적은 순서대로 유지돼요.
맵 메서드는 호출한 맵을 절대 바꾸지 않아요. ->set()이나 ->remove() 같은 메서드는 새 맵을 반환하므로, 결과를 저장하거나 다음 메서드를 이어서 호출하세요:
<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
함수를 받는 메서드는 각 항목마다 한 번씩 함수를 호출해요. 함수는 먼저 값을, 필요하면 두 번째로 키를 받아요: fn(count) => ... 또는 fn(count, fruit) => .... ->map_keys()만 예외인데, 키를 다루므로 키를 먼저 넘겨요. 각 값의 필드나 메서드만 필요하면 전체 함수 대신 점으로 쓸 수 있어요: ->filter(.is_active)는 ->filter(fn(user) => $user.is_active)와 같아요.
필드는 값에 관한 단순한 정보로, 대괄호 없이 점으로 읽어요: $value.name. 메서드는 인수를 받거나 작업을 수행하며, ->로 호출하거나 $value->name(arguments)처럼 값을 파이프로 넘겨서 호출해요: $value |> name(arguments).
->all(fn(value, key?))은 함수가 모든 항목에 대해 참이면 참이에요. 실패하는 첫 항목에서 멈춰요. 빈 맵은 실패하는 항목이 없으므로 true를 반환해요:
<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
->any(fn(value, key?))은 함수가 적어도 한 항목에 대해 참이면 참이에요. 통과하는 첫 항목에서 멈춰요:
<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
->count()은 맵에 있는 키의 개수예요. 값이 null인 키도 키가 있으므로 셈에 포함돼요:
<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
맵을 재귀적으로 병합합니다. 오른쪽 값이 리스트, null, 그 밖의 말단 값을 대체합니다.
->deep_merge(other)은 ->merge()처럼 다른 맵의 키를 추가하지만, 양쪽 맵에 같은 키가 있으면 그 안쪽 맵까지 병합해요. 양쪽 모두 맵이 아닌 경우(예: 리스트, 숫자, null)에는 다른 맵의 값으로 대체해요. 작은 재정의로 깊이 있는 설정 하나만 바꾸고 나머지는 유지하고 싶을 때 좋아요:
<let name="defaults" value="{ theme: { colour: 'blue', size: 12 }, tags: ['news'] }" />
<let name="mine" value="{ theme: { size: 14 }, tags: ['sport'] }" />
<let name="settings" value="$defaults->deep_merge($mine)" />
<echo value="$settings.theme" /> <!-- { colour: 'blue', size: 14 } because both themes are maps, so only size changes -->
<echo value="$settings.tags" /> <!-- ['sport'] because lists aren't merged: the new list replaces the old one -->
<echo value="$defaults->merge($mine).theme" /> <!-- { size: 14 } because ->merge() replaces the whole theme -->
$defaults = { theme: { colour: 'blue', size: 12 }, tags: ['news'] }
$mine = { theme: { size: 14 }, tags: ['sport'] }
$settings = $defaults->deep_merge($mine)
echo $settings.theme // { colour: 'blue', size: 14 } because both themes are maps, so only size changes
echo $settings.tags // ['sport'] because lists aren't merged: the new list replaces the old one
echo $defaults->merge($mine).theme // { size: 14 } because ->merge() replaces the whole theme
->get(key, default?)은 키의 값을 읽거나, 키가 없으면 기본값을 반환해요(기본값을 주지 않으면 null). 변수에 있는 키를 쓸 때나 키가 없는 것이 정상인 경우에 사용하세요:
<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
->has(key)은 맵에 키가 있으면 참이에요. 값이 null여도 마찬가지예요. 키는 정확히 일치해야 하므로 대문자도 구별해요:
<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
->omit(keys)은 나열한 키를 뺀 복사본을 반환해요. 맵에 없는 키는 무시돼요. ->pick()는 반대로 동작해요. 편집기는 대괄호 안에 맵 자신의 키를 제안해요:
<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
->pick(keys)은 나열한 키만 가진 복사본을 주는데, 나열한 순서가 아니라 맵 자체의 순서를 따릅니다. 맵에 없는 키는 무시됩니다:
<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
#map->reduce(fn(total, value, key?), initial?)→ any
항목들을 하나의 값으로 합칩니다: initial에서 시작해 fn(total, value, key)이 다음 total을 돌려줍니다. initial이 없으면, 리스트의 ->reduce()와 같이 첫 번째 값이 첫 total이 되고, 빈 맵이면 null이 됩니다.
->reduce(fn(total, value, key?), initial?)은 항목들을 순서대로 훑으며 하나의 값을 쌓아 올립니다. 함수는 지금까지의 total과 다음 값을 받아 새로운 total을 돌려줍니다. initial가 없으면 첫 번째 값이 첫 total이 되고, 빈 맵이면 null가 됩니다:
<let name="basket" value="{ apples: 3, pears: 5, plums: 2 }" />
<echo value="$basket->reduce(fn(total, count) => $total + $count)" /> <!-- 10 because 3 + 5 is 8, then 8 + 2 is 10 -->
<echo value="$basket->reduce(fn(total, count) => $total + $count, 100)" /> <!-- 110 because the total starts at 100 -->
<echo value="$basket->reduce(fn(list, count, fruit) => "{ $list } { $fruit }", 'Bought:')" /> <!-- 'Bought: apples pears plums' -->
$basket = { apples: 3, pears: 5, plums: 2 }
echo $basket->reduce(fn(total, count) => $total + $count) // 10 because 3 + 5 is 8, then 8 + 2 is 10
echo $basket->reduce(fn(total, count) => $total + $count, 100) // 110 because the total starts at 100
echo $basket->reduce(fn(list, count, fruit) => "{ $list } { $fruit }", 'Bought:') // 'Bought: apples pears plums'
->set(key, value)은 키를 값으로 설정한 복사본을 줍니다. 기존 키는 자리를 유지하고, 새 키는 끝에 붙습니다:
<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
by: fn(value, key)로 정렬한 항목들의 복사본을 돌려줍니다. 리스트의 ->sort(by:)와 같습니다. order: desc는 가장 큰 값을 맨 앞에 둡니다.
->sort(by: fn(value, key?), order?: asc|desc)은 각 항목에 대해 by가 준 값의 오름차순으로 항목들을 새 순서로 배치한 복사본을 줍니다. order: desc는 가장 큰 값을 맨 앞에 둡니다. 키로 정렬하려면 by가 키를 반환하도록 하세요:
<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