Data and Math
vcache
A least recently used cache, and memoizing for any function.
Released v1.0.0 · 2026-09-28use "stdlib/cache.ven"
A least recently used cache with a fixed size and real eviction, plus a memoize helper that wraps a one argument function and remembers every result it returns. The memoize cache has no limit; use the cache directly when the bound and the eviction matter. Neither structure locks itself across concurrent tasks.
print LRUCache(2, new_map(), [])
API reference
lru_touch(cache, key)
Internal helper: move key to the most-recently-used end of cache.order (the end of the List), leaving cache.store untouched.
c = lru_new(2)
lru_put(c, "a", 1)
lru_put(c, "b", 2)
lru_touch(c, "a")
lru_put(c, "c", 3)
print lru_has(c, "a")
print lru_has(c, "b")
:: prints: yes
:: nolru_has(cache, key)
Tests whether key is currently present in cache.
c = lru_new(2)
lru_put(c, "a", 1)
print lru_has(c, "a")
print lru_has(c, "z")
:: prints: yes
:: nolru_get(cache, key)
Gives back the value stored for key in cache, or nothing when absent.
c = lru_new(2)
lru_put(c, "a", 1)
print lru_get(c, "a")
:: prints: 1lru_put(cache, key, value)
Stores value under key in cache, evicting the least-recently-used entry if full.
c = lru_new(2)
lru_put(c, "a", 1)
lru_put(c, "b", 2)
lru_put(c, "c", 3)
print lru_has(c, "a")
print lru_has(c, "c")
:: prints: no
:: yeslru_size(cache)
Gives back how many entries are currently stored in cache.
c = lru_new(2)
lru_put(c, "a", 1)
print lru_size(c)
:: prints: 1memoized(f)
Wraps a one-argument function f so repeat calls with the same argument are cached.
make square(n)
give n * n
cached = memoized(square)
print cached(5)
:: prints: 25