Class Hash
The Hash
class provides a mapping between a key and a value. Hash
values
can be created through [key1 => value1, key2 => value2, ...]
. When writing
a Hash
, the key is the first type, and the value is the second.
[1 => "a", 2 => "b", 3 => "c"]
would therefore be written as
Hash[Integer, String]
.
Currently, only Integer
and String
can be used as keys.
public define clear
Removes all pairs currently present within self
.
RuntimeError
ifself
is currently being iterated over.
public define delete(key: A)
Attempt to remove key
from self
. If key
is not present within
self
, then nothing happens.
RuntimeError
ifself
is currently being iterated over.
public define each_pair(fn: Function(A, B))
Iterate through each pair that is present within self
. For each of the
pairs, call fn
with the key and value of each pair.
public define each_value(fn: Function(B))
Similar to Hash.each_pair
, except for values only.
public define get(key: A): Option[B]
Attempt to find key
within self
.
If key
is present, then a Some
containing the associated value is
returned.
Otherwise, this returns None.
public define has_key(key: A): Boolean
Return true
if key
is present within self
, false
otherwise.
public define keys: List[A]
Construct a List
containing all values that are present within self
.
There is no guarantee of the ordering of the resulting List
.
public define map_values(fn: Function(B => C)): Hash[A, C]
This iterates through self
and calls fn
for each element present.
The result of this function is a newly-made Hash
where each value is
the result of the call to fn
.
public define merge(others: List[Hash[A, B]]): Hash[A, B]
Create a new Hash
that holds the result of self
and each Hash
present within others
.
When duplicate elements are found, the value of the right-most Hash
wins.
public define reject(fn: Function(A, B => Boolean)): Hash[A, B]
This calls fn
for each element present within self
. The result of
this function is a newly-made Hash
containing all values for which
fn
returns false
.
public define select(fn: Function(A, B => Boolean)): Hash[A, B]
This calls fn
for each element present within self
. The result of
this function is a newly-made Hash
containing all values for which
fn
returns true
.
public define size: Integer
Returns the number of key+value pairs present within self
.