I've been learning scala and I gotta say that it's a really cool language. I especially like its pattern matching capabilities and function literals but I come from a javascript, ruby background and one of my favorite patterns in those languages is the lazy function and method definition pattern. An example in javascript is
var foo = function() {
var t = new Date();
foo = function() {
return t;
};
return foo();
};
The same code with minor tweaks works in ruby where you just use the singleton object to redefine the method after the computation is performed. This kind of thing comes in really handy when expensive computation are involved and you don't know ahead of time if you are going to need the result. I know that in scala I can use a cache to simulate the same kind of result but I'm trying to avoid conditional checks and so far my experiments have returned negative results. Does anyone know if there is a lazy function or method definition pattern in scala?
Note: The javascript code is from Peter Michaux's site.
I think what you mean "lazy function" is function literal or anonymous function.
In Scala you could do things like this, very similar to the javascript code you posted.
The main difference is that:
I think some of the responders were a little confused by the way you phrased the question. The Scala construct you want here is a simple lazy definition:
The construction of the Date object will occur at most once and be deferred until the first reference to foo.
You can define a lazy val which is a function :
foo()
will now return the same Date object each time, object which will be initialized the first time foo is called.To explain the code a little, the first time foo() is called
{ val d = new Date; () => { d } }
is executed, d is assigned to a new date value then it evaluate the last expression() => { d }
and assign it to the foo value. Then foo is a function with no parameters which return d.I known nothing about Ruby, but scala has singleton object pattern also:
If you want to get function ,you can make it subtype of a function type:
All that complicated code in JavaScript appears to just try to cache the value of the date. In Scala, you can achieve the same thing trivially:
And, if don't even want to make a val, but want to call a function that will only execute the expensive code if it needs it, you can
where the pattern
expensive: => String
is called a by-name parameter, which you can think of as, "Give me something that will generate a string on request." Note that if you use it twice, it will regenerate it each time, which is where Randall Schultz' handy pattern comes in:Now you generate only if you need it (via the by-name parameter) and store it and re-use it if you need it again (via the lazy val).
So do it this way, not the JavaScript way, even though you could make Scala look a lot like the JavaScript.
Scala has
lazy val
s, whose initializers are not evaluated unless and until the val is used. Lazy vals may be used as method local variables.Scala also has by-name method parameters, whose actual parameter expressions are wrapped in a thunk and that thunk is evaluated every time the formal parameter is referenced in the method body.
Together these can be used to achieve lazy evaluation semantics such as are the default in Haskell (at least in my very limited understanding of Haskell).
In this method, the expression used as the actual parameter will be evaluated either zero times (if the dynamic execution path of the method body never uses
ii
) or once (if it usesii
one or more times).