lazy vals - makingthematrix/gailibrary GitHub Wiki

Since we’re working on immutable data, all functions calls (well, not really) will result in the same outcomes. Therefore, we can simplify functions by moving common parts of them into lazy vals. Just as in Scala, a lazy val is a value which being computed only once, the first time it is used. Then the value is cached, so every subsequent call will simply return the value without the need to recompute it. The access is transparent: a function asks for a value with the id and does not care if it’s actually a value, or a lazy val. It also means that FunctionIds and CellIds are interchangeable.

Internally, a GAI Cell holds a map of FunctionIdOption<Value>. If the value is None, the function for the given FunctionId is called and the result is stored in the map. But GAI does not search through all the functions, but only those registered as "lazy val functions". This is because ideally a lazy val function should not have any side-effects, and it should not be able to call other functions, except for a limited access to other lazy val functions. In the first version of GAI these rules will not be enforced, but in the future I might want to implement them as requirements, so putting lazy val functions in a special basket may prevent some backward incompabilities in the future.

Functions and the Function Library <- | -> Variables and pseudo-random numbers generators

⚠️ **GitHub.com Fallback** ⚠️