Class Hash

(builtin) class Hash[A, B] {  }

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.

Methods

define clear

Removes all pairs currently present within self.

  • RuntimeError if self is currently being iterated over.

define delete(key: A)

Attempt to remove key from self. If key is not present within self, then nothing happens.

  • RuntimeError if self is currently being iterated over.

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.

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.

define has_key(key: A): Boolean

Return true if key is present within self, false otherwise.

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.

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.

define merge(others: 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.

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.

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.

define size: Integer

Returns the number of key+value pairs present within self.