get_or_compute(cache, x, y, compute_fn), where cache is a dict whose keys are (x, y) coordinate tuples. If (x, y) is already a key in cache, return the cached value without calling compute_fn. Otherwise call compute_fn(x, y), store the result under the (x, y) tuple key, and return it. This relies on tuples being hashable — unlike lists, they can be used directly as dict keys.cache = {}; get_or_compute(cache, 2, 3, lambda x, y: x * y)6
Computed once and stored under cache[(2, 3)] = 6.
get_or_compute(cache, 2, 3, some_fn) # called again with the same coords
6
Returned straight from the cache; some_fn is never invoked this time.
Build key = (x, y) and check `if key in cache` first.
Only call compute_fn(x, y) on a cache miss, then store and return it.