Lazy Macros

by Kaiochao
Easy macros for common lazy patterns.
ID:2130663
 
Easy macros for common lazy-initialization patterns.
These are all inline expressions, but they work best with the first argument being a variable.
(e.g. a, a.b, a.b.c, etc., not A())

Reference


LazyAssign(x, y)
X should be a variable that can be assigned to.
Initialize X to Y if it isn't already initialized.
Evaluates to the resulting X.

LazyListAdd(list, item)
LIST should be a variable that can be assigned to.
Initialize LIST if necessary, then add ITEM to it.
Evaluates to LIST.

LazyListRemove(list, item)
LIST should be a variable that can be assigned to.
If LIST exists, remove ITEM from it, and de-initialize LIST if it's empty.
Evaluates to LIST on successful removal.
Evaluates to null if LIST doesn't exist.
Evaluates to FALSE if ITEM is not in LIST.

LazyMap(list, key, value)
LIST should be a variable that can be assigned to.
Initialize MAP if necessary, then map a key to a value.
Evaluates to VALUE.

LazyIndex(list, key)
Gets the value associated to KEY in MAP.
Doesn't initialize MAP.
Evaluates to MAP if MAP does not exist.
Evaluates to MAP[KEY] otherwise.

LazyInclude(list, item)
LIST should be a variable that can be assigned to.
Initialize SET if necessary, then include ELEMENT in it.
Evaluates to LIST.