Map
The map
data type in Filtrera is used to represent sequences of keyed values. This reference guide provides a detailed specification of the map
data type, its syntax, and usage for users already familiar with the concept.
Type Notation for Maps
The type notation for maps is {<key type> -> <value type>}
.
Example
let map: { text -> number } = { 'key1' -> 1 'key2' -> 2 'key3' -> 3}
Defining Maps
Built-in Map (Dictionary)
The built-in Dictioanry is a Map that uses a hash mechanism to quickly lookup values based on provided keys.
Example
let map: { text -> number } = { 'key1' -> 1 'key2' -> 2 'key3' -> 3}from map->'key2'// Returns 2
See Also
Records also implement the Map interface.
Runtime-provided Maps
Other Map implementations can be provided by the runtime.
Maps are Not Ordered
The Map interface doesn’t enforce any order of the keys, values or pair entries. Do not rely on values being returned in any certain order.
Maps as Iterators
All maps are also Iterators. When iterating a map, the values are returned.
Practical Usage
Example: Accessing an Item by Key
let map: { text -> number } = { 'key1' -> 1 'key2' -> 2 'key3' -> 3}from map->'key2'// Returns 2
Example: Getting the Keys of a Map
let map: { text -> number } = { 'key1' -> 1 'key2' -> 2 'key3' -> 3}from map keys// Returns ['key2', 'key1', 'key3']
Note that the order of the keys are undeterministic.
Example: Get Entries as an Iterator of Tuples
let map: { text -> number } = { 'key1' -> 1 'key2' -> 2 'key3' -> 3}from map entries// Returns [('key2', 2), ('key1', 1), ('key3', 3)]
Example: Counting Items in an Map
let map: { text -> number } = { 'key1' -> 1 'key2' -> 2 'key3' -> 3}from map count
Summary
The map
data type in Filtrera represents a map between keys and values. Built-in maps, referred to as dictioanries, can be defined using curly brackets, while other maps can be provided by the runtime. Maps can be used as iterators of it’s values. By understanding its syntax and usage, including defining maps, accessing values, and using filters and functions such as keys
and entries
, you can leverage maps to create expressive and functional programs. The ability to handle runtime-provided maps further enhances the flexibility and robustness of your Filtrera programs.