k:map stores values under string keys. Like lists, a map is a plain string under the hood, so every "change" returns a NEW map — reassign the variable each time.
// Tutorial 28: Maps (Key-Value Pairs)
// k:map stores values under string keys.
//
// Like lists, a map is a plain string under the hood, so every "change"
// returns a NEW map — reassign the variable each time.
import "k:map"
just run {
let ages = mapNew()
ages = mapSet(ages, "alice", "30")
ages = mapSet(ages, "bob", "25")
ages = mapSet(ages, "carol", "41")
kp("alice = " + mapGet(ages, "alice"))
kp("hasKey bob? " + hasKey(ages, "bob"))
kp("hasKey dave? " + hasKey(ages, "dave"))
kp("keys = " + keys(ages))
kp("values = " + values(ages))
kp("count = " + length(keys(ages)))
// Setting an existing key overwrites its value.
ages = mapSet(ages, "alice", "31")
kp("alice = " + mapGet(ages, "alice"))
}
Click ▶ Run to execute. Lessons using k:fs/k:http/match/struct need the real runtime.
Tip: this code box is editable — tweak it, then hit Run, or copy into a .k file and run locally.
Run it locally: kcc -r tutorial/28_maps.k