I have seen quite a few topics dealing with the difference between val, lazy val and def but none of them seemed to answer exactly to my question.
I'm wondering what is the difference between a val (or lazy val) and a def when the val is a function. Indeed, most answers I read explained that a val is evaluated only once whereas a def is evaluated each time it is called. What I don't get is when a val is used as in the following code :
lazy val name : Int => Int = {
case n if(n < 20) => //terminal case...
case n if(n < 100) => //terminal case...
case n if(n < 1000) => name(n / 100) + //something unrelevant...
}
val result = (1 to 1000).map(name).sum
To evaluate result, it seems to me that name is going to be "evaluated" for each element of the sequence 1 to 1000. How is it different from using a def in that particular case ?