Conditional Fill/Return of Dictionary

Hi,

in many programming languages I can quicky return

an entry in an Hashmap or fill (=calculate) it and return the result if

its not in the map yet;

For example in Coffeescript:

func:(x)->Cache or=calculate x

Currently my best effort in k is this:

func:{$[^r:Cache;Cache:calculate x;r]}

is there another way without having to repeat “Cache”?

Markus

Nothing wrong with having to repeat cache! But if you insist, something like this would suit:

calculate:{100+x};

cache:(1 2)!(101 102);

q){@[`cache;x;{$[null y;:calculate x;:y]}x]x}1

101

q){@[`cache;x;{$[null y;:calculate x;:y]}x]x}4

104

q)cache

1| 101

2| 102

4| 104

Assumes global calculate function and global cache.

Terry