Ink by Example — Maps

Ink by Example: Maps

Like lists, maps use the composite value — Ink's single built-in data structure. At runtime, maps are hashmaps with string keys.

std := load('../vendor/std')
clone := std.clone
each := std.each
log := std.log


observation := {
    weather: 'Sunny'
    'observedAt': {
        time: time()
    }
}
log(observation)

Like lists, we use the dot syntax to access and mutate.

observation.('weather') := 'Raining'

If we know the key before runtime, we can use a shortcut and just write it out.

observation.weather := 'Raining'
log(observation)

std.clone can be used to create a copy of a map.

cloned := clone(observation)

We can iterate through the keys by calling keys.

each(keys(observation), (key) => (
    log(observation.(key))
))

$ ink maps.ink
{weather: 'Sunny', observedAt: {time: 1626466495.22846460}}
{weather: 'Raining', observedAt: {time: 1626466495.22846460}}
Raining
{time: 1626466495.22846460}

Next example: Functions.