Lazy initializing default values

One of the tasks you will come across often is to look up a value in a map, while providing a default value for the looked up value. For e.g.

Properties prop = ...
Object value = prop.get("key", "defaultValue")

The issue with this pattern is that, what if arriving at default value is a costly operation. Even worse, there may be no need to compute the default value at all, may be because the get operation will always find a value in the properties.

During such times "lazy evaluation" comes to the rescue. This is not a new pattern, it has been around for long time. Just that some APIs are written with lazy evaluation in mind, and some not. In the example above, instead of taking the value itself, if the get method takes an argument that is callable or a lambda function, then it can use that function only under the situation that the value is not present.

The example below is from Fantom language when you attempt to get a value from a map.
    class Main {
        Void main() {
            myMap := [Str:Str]["k1":"v1", "k2":"v2"]
            echo(myMap.getOrAdd("k3", |Str k->Str| {return "v3"}))
        }
    }

What the piece of code above does is to compute the value of the key only when it is not present in the map. In the example above when you look up "k3" which is not found in the map, the lambda function is invoked to compute the value once.

Comments

Popular posts from this blog

Gotchas with DBCP

A note on Java's Calendar set() method

The mysterious ORA-03111 error