Dictionary
Dictionaries are a type of data structure that can be used to store key-value pairs.
local dictionary = {
cats = 2,
dogs = 1
}
print(dictionary.cats) -- 2
Functions
copy
Dictionary.
copy
(
dictionary:
T
--
The dictionary to copy.
) →
T
--
The copied dictionary.
Copies a dictionary.
local dictionary = { hello = "world" }
local new = Copy(dictionary) -- { hello = "world" }
print(new == dictionary) -- false
print(new.hello == dictionary.hello) -- true
update
Dictionary.
update
(
dictionary:
{
[
K
]
:
V?
}
,
--
The dictionary to update.
key:
K
,
--
The key to update.
updater?:
(
value:
V
,
key:
K
)
→
U
,
--
The updater function.
callback?:
(
key:
K
)
→
C
--
The callback function.
) →
{
[
K
]
:
V
|
U
|
C
}
--
The updated dictionary.
Updates a value in a dictionary at the given key. If the value at the given key does not exist, callback
will be called, and its return value will be used as the value at the given key.
local dictionary = { cats = 2 }
local new = Update(dictionary, "cats", function(value)
return value + 1
end) -- { cats = 3 }
local new = Update(dictionary, "dogs", function(value)
return value + 1
end, function(value)
return 1
end) -- { cats = 3, dogs = 1 }
map
Dictionary.
map
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to map.
mapper:
(
value:
V
,
key:
K
,
dictionary:
{
[
K
]
:
V
}
)
→
(
Y?
,
X?
)
--
The mapper function.
) →
{
[
X
]
:
Y
}
--
The mapped dictionary.
Maps the dictionary using the mapper function. The mapper function can return a value and a key. If the mapper function does not return a key, the original key will be used.
local dictionary = { hello = 10, goodbye = 20 }
local new = Map(dictionary, function(value, key)
return value * 2, key .. "!"
end) -- { ["hello!"] = 20, ["goodbye!"] = 40 }
local new = Map(dictionary, function(value, key)
return value * 10
end) -- { hello = 100, goodbye = 200 }
flatten
Dictionary.
flatten
(
dictionary:
T
,
--
The dictionary to flatten.
depth?:
number
--
The depth to flatten the dictionary to.
) →
T
--
The flattened dictionary.
Flattens a dictionary. If depth is not specified, it will flatten the dictionary as far as it can go.
local dictionary = {
hello = "world",
goodbye = {
world = "hello",
roblox = {
yes = "no",
no = "yes",
}
}
}
local new = Flatten(dictionary) -- { hello = "world", world = "hello", yes = "no", no = "yes" }
local new = Flatten(dictionary, 1) -- { hello = "world", world = "hello", roblox = { yes = "no", no = "yes" } }
includes
Dictionary.
includes
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to check.
value:
V
--
The value to check for.
) →
boolean
--
Whether or not the dictionary includes the given value.
Checks whether or not the given dictionary includes the given value.
local dictionary = { hello = "roblox", goodbye = "world" }
local includesRoblox = Includes(dictionary, "roblox") -- true
local includesCat = Includes(dictionary, "cat") -- false
removeValues
Dictionary.
removeValues
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to remove the values from.
values:
...V
--
The values to remove.
) →
{
[
K
]
:
V
}
--
The dictionary without the given values.
Removes the given values from the given dictionary.
local dictionary = { hello = "world", cat = "meow", unicorn = "rainbow", goodbye = "world" }
local withoutWorld = RemoveValues(dictionary, "world") -- { cat = "meow", unicorn = "rainbow" }
local onlyWorld = RemoveValues(dictionary, "meow", "rainbow") -- { hello = "world", goodbye = "world" }
mergeDeep
Dictionary.
mergeDeep
(
dictionaries?:
...any
--
The dictionaries to merge.
) →
T
--
The merged dictionary.
Merges the given dictionaries into a single dictionary. If the
value is None
, it will be removed from the result. This is
recursive. The parameters may be any number of dictionaries or
nil
. Non-dictonaries will be ignored.
Aliases: joinDeep
local dictionary1 = { hello = "roblox", goodbye = { world = "goodbye" } }
local dictionary2 = { goodbye = { world = "world" } }
local merged = MergeDeep(dictionary1, dictionary2) -- { hello = "roblox", goodbye = { world = "world" } }
removeValue
Dictionary.
removeValue
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to remove the value from.
value:
V
--
The value to remove.
) →
{
[
K
]
:
V
}
--
The dictionary without the given value.
Removes the given value from the given dictionary.
local dictionary = { hello = "roblox", goodbye = "world" }
local withoutHello = RemoveValue(dictionary, "roblox") -- { goodbye = "world" }
local withoutGoodbye = RemoveValue(dictionary, "world") -- { hello = "roblox" }
merge
Dictionary.
merge
(
dictionaries?:
...any
--
The dictionaries to merge.
) →
T
--
The merged dictionary.
Merges the given dictionaries into a single dictionary. If the
value is None
, it will be removed from the result. The
parameters may be any number of dictionaries or nil
.
Non-dictonaries will be ignored.
Aliases: join
local dictionary1 = { hello = "roblox", goodbye = "world" }
local dictionary2 = { goodbye = "goodbye" }
local merged = Merge(dictionary1, dictionary2) -- { hello = "roblox", goodbye = "goodbye" }
filter
Dictionary.
filter
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to filter.
predicate?:
(
value:
V
,
key:
K
,
dictionary:
{
[
K
]
:
V
}
)
→
any
--
The predicate to use to filter the dictionary.
) →
{
[
K
]
:
V
}
--
The filtered dictionary.
Filters a dictionary using a predicate. Any items that do not pass the predicate will be removed from the dictionary.
local dictionary = { hello = "world", goodbye = "goodbye" }
local result = Filter(dictionary, function(value, key)
return value == "world"
end) -- { hello = "world" }
every
Dictionary.
every
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to check.
predicate:
(
value:
V
,
key:
K
,
dictionary:
{
[
K
]
:
V
}
)
→
any
--
The predicate to use to check the dictionary.
) →
boolean
--
Whether every item in the dictionary passes the predicate.
Checks whether every item in the dictionary passes the predicate.
local dictionary = { hello = "world", goodbye = "world" }
local value = Every(dictionary, function(value, key)
return value == "world"
end) -- true
local value = Every(dictionary, function(value, key)
return value == "hello"
end) -- false
values
Dictionary.
values
(
dictionary:
{
[
K
]
:
V
}
--
The dictionary to get the values from.
) →
{
V
}
--
The values in the dictionary.
Gets the values in the given dictionary.
local dictionary = { hello = "roblox", goodbye = "world" }
local values = Values(dictionary) -- { "roblox", "world" }
copyDeep
Dictionary.
copyDeep
(
dictionary:
T
--
The dictionary to copy.
) →
T
--
The copied dictionary.
Copies a dictionary recursively.
local dictionary = { hello = { world = "goodbye" } }
local new = CopyDeep(dictionary) -- { hello = { world = "goodbye" } }
print(new == dictionary) -- false
print(new.hello == dictionary.hello) -- false
fromEntries
Dictionary.
fromEntries
(
entries:
{
{
K
,
V
}
}
--
An array of key-value pairs.
) →
{
[
K
]
:
V
}
--
A dictionary composed of the given key-value pairs.
Creates a dictionary from the given key-value pairs.
local entries = { { "hello", "roblox" }, { "goodbye", "world" } }
local dictionary = FromEntries(entries) -- { hello = "roblox", goodbye = "world" }
removeKey
Dictionary.
removeKey
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to remove the key from.
key:
K
--
The key to remove.
) →
{
[
K
]
:
V
}
--
The dictionary without the given key.
Removes the given key from the given dictionary.
local dictionary = { hello = "roblox", goodbye = "world" }
local withoutHello = RemoveKey(dictionary, "hello") -- { goodbye = "world" }
local withoutGoodbye = RemoveKey(dictionary, "goodbye") -- { hello = "roblox" }
equalsDeep
Dictionary.
equalsDeep
(
...:
...
{
[
any
]
:
any
}
--
The dictionaries to compare.
) →
boolean
--
Whether the dictionaries are equal.
Compares two dictionaries for equality using deep comparison.
local dictionary = { hello = "world", goodbye = { world = "hello" } }
local other1 = { hello = "world", goodbye = { world = "hello" } }
local other2 = { hello = "hello", world = "goodbye" }
local value = EqualsDeep(dictionary, other1) -- true
local value = EqualsDeep(dictionary, other1, other2) -- false
freeze
Dictionary.
freeze
(
dictionary:
T
--
The dictionary to freeze.
) →
T
--
The frozen dictionary.
Freezes the given dictionary at the top level, making it read-only.
local dictionary = { hello = "roblox", goodbye = { world = "world" } }
local new = Freeze(dictionary)
new.hello = "world" -- error!
new.goodbye.world = "hello" -- still works!
fromArrays
Dictionary.
fromArrays
(
keys:
{
K
}
,
--
An array containing values to be used as keys.
values:
{
V
}
--
An array containing values to be used as values.
) →
{
[
K
]
:
V
}
--
A dictionary composed of the given keys and values.
Creates a dictionary from the given arrays, where the first array is used as keys and the second array is used as values.
local keys = { "hello", "goodbye" }
local values = { "roblox", "world" }
local dictionary = FromArrays(keys, values) -- { hello = "roblox", goodbye = "world" }
set
Dictionary.
set
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to set the value in.
key:
K
,
--
The key to set the value in.
value:
V
--
The value to set.
) →
{
[
K
]
:
V
}
--
The dictionary with the given value set.
Sets the given value in the given dictionary.
local dictionary = { hello = "world", cat = "meow", unicorn = "rainbow" }
local setCat = Set(dictionary, "cat", "woof") -- { hello = "world", cat = "woof", unicorn = "rainbow" }
count
Dictionary.
count
(
dictionary:
T
,
--
The dictionary to count.
predicate?:
(
value:
T
,
key:
K
,
dictionary:
T
)
→
any
--
The predicate to use to filter the dictionary.
) →
number
--
The number of items in the dictionary.
Counts the number of items in a dictionary.
local dictionary = { hello = "world", goodbye = "world" }
local value = Count(dictionary) -- 2
local value = Count(dictionary, function(item, key)
return item == "world"
end) -- 1
keys
Dictionary.
keys
(
dictionary:
{
[
K
]
:
V
}
--
The dictionary to get the keys of.
) →
{
K
}
--
An array containing the keys of the given dictionary.
Gets the keys of the given dictionary as an array.
local dictionary = { hello = "roblox", goodbye = "world" }
local keys = Keys(dictionary) -- { "hello", "goodbye" }
flip
Dictionary.
flip
(
dictionary:
{
[
K
]
:
V
}
--
The dictionary to flip.
) →
{
[
V
]
:
K
}
--
The flipped dictionary.
Flips a dictionary. Keys become values and values become keys.
local dictionary = { hello = "roblox", goodbye = "world" }
local new = Flip(dictionary) -- { world = "goodbye", roblox = "hello" }
withKeys
Dictionary.
withKeys
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to select the keys from.
keys:
...K
--
The keys to keep.
) →
{
[
K
]
:
V
}
--
The dictionary with only the given keys.
Returns a dictionary with the given keys.
local dictionary = { hello = "world", cat = "meow", dog = "woof", unicorn = "rainbow" }
local withoutCatDog = WithKeys(dictionary, "cat", "dog") -- { cat = "meow", dog = "woof" }
equals
Dictionary.
equals
(
...:
...
{
[
any
]
:
any
}
--
The dictionaries to compare.
) →
boolean
--
Whether the dictionaries are equal.
Compares two dictionaries for equality.
local dictionary = { hello = "world", goodbye = "world" }
local other1 = { hello = "world", goodbye = "world" }
local other2 = { hello = "hello", world = "goodbye" }
local value = Equals(dictionary, other1) -- true
local value = Equals(dictionary, other1, other2) -- false
removeKeys
Dictionary.
removeKeys
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to remove the keys from.
keys:
...K
--
The keys to remove.
) →
{
[
K
]
:
V
}
--
The dictionary without the given keys.
Removes the given keys from the given dictionary.
local dictionary = { hello = "world", cat = "meow", dog = "woof", unicorn = "rainbow" }
local withoutCatDog = RemoveKeys(dictionary, "cat", "dog") -- { hello = "world", unicorn = "rainbow" }
has
Dictionary.
has
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to check.
key:
any
--
The key to check for.
) →
boolean
--
Whether or not the dictionary has the given key.
Checks whether or not the given dictionary has the given key.
local dictionary = { hello = "roblox", goodbye = "world" }
local hasHello = Has(dictionary, "hello") -- true
local hasCat = Has(dictionary, "cat") -- false
entries
Dictionary.
entries
(
dictionary:
{
[
K
]
:
V
}
--
The dictionary to get the entries from.
) →
{
{
K
,
V
}
}
--
The entries in the dictionary.
Returns the entries in the given dictionary as an array of key-value pairs.
local dictionary = { hello = "roblox", goodbye = "world" }
local entries = Entries(dictionary) -- { { "hello", "roblox" }, { "goodbye", "world" } }
some
Dictionary.
some
(
dictionary:
{
[
K
]
:
V
}
,
--
The dictionary to check.
predicate:
(
value:
V
,
key:
K
,
dictionary:
{
[
K
]
:
V
}
)
→
any
--
The predicate to check against.
) →
boolean
--
Whether or not the predicate returned true for any value.
Checks whether or not the predicate returned true for any value in the dictionary.
local dictionary = { hello = "world", cat = "meow", unicorn = "rainbow" }
local hasMeow = Some(dictionary, function(value)
return value == "meow"
end) -- true
local hasDog = Some(dictionary, function(_, key)
return key == "dog"
end) -- false
freezeDeep
Dictionary.
freezeDeep
(
dictionary:
T
--
The dictionary to freeze.
) →
T
--
The frozen dictionary.
Freezes the entire dictionary, making it read-only, including all nested dictionaries.
local dictionary = { hello = "roblox", goodbye = { world = "world" } }
local new = FreezeDeep(dictionary)
new.hello = "world" -- error!
new.goodbye.world = "hello" -- error!