Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lazy function definitions in scala

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.

like image 976
David K. Avatar asked Aug 25 '10 03:08

David K.


People also ask

What are lazy values in Scala?

Save Article. Vals and Lazy vals are present in Scala. lazy keyword changes the val to get lazily initialized. Lazy initialization means that whenever an object creation seems expensive, the lazy keyword can be stick before val.

What are lazy Vals in Scala?

Scala provides a nice language feature called lazy val that defers the initialization of a variable. The lazy initialization pattern is common in Java programs. Though it seems tempting, the concrete implementation of lazy val has some subtle issues.

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

Is Scala lazily evaluated?

Lazy Evaluation in ScalaHaskell is a functional programming language that uses lazy evaluation by default.


2 Answers

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:

lazy val foo = new Date

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

def maybeExpensive(doIt: Boolean, expensive: => String) {
  if (doIt) println(expensive)
}
maybeExpensive(false, (0 to 1000000).toString)  // (0 to 1000000).toString is never called!
maybeExpensive(true, (0 to 10).toString)        // It is called and used this time

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:

def maybeExpensiveTwice(doIt: Boolean, expensive: => String) {
  lazy val e = expensive
  if (doIt) {
    println(e)
    println("Wow, that was " + e.length + " characters long!")
  }
}

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.

like image 181
Rex Kerr Avatar answered Sep 29 '22 22:09

Rex Kerr


Scala has lazy vals, 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).

def meth(i: => Int): Something = {
  //        ^^^^^^ by-name parameter syntax
  lazy val ii = i
  // Rest of method uses ii, not i
}

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 uses ii one or more times).

like image 35
Randall Schulz Avatar answered Sep 29 '22 23:09

Randall Schulz