Re: [scala-functional] Re: lazy val memoization

2018-04-10 Thread Steven Parkes
And, FWIW, one thing we just noticed is that the lazy implementation for
this case takes the lock on the object that contains this val even though
the scope of this particular value is note the object scope, at least in
2.11.something.

In our case, we had a lot of lock contention that we weren't
expecting/aware of.

Our conclusion was that lazy locals are of _very_ limited and mostly to be
avoided.

On Tue, Apr 10, 2018 at 8:41 AM, Adam Mackler  wrote:

> The value cached is defined inside the definition of the method func(),
> so everytime you invoke func() it defines a new local Int named cached.
> Change your code to this:
>
> def func(i: => Int): Unit = {
>   lazy val cached = {
> println("Calculating")
> i * i
>   }
>   println(cached)
>   println(cached)
>   println(cached)
> }
>
>
> and you will see that it prints "Calculating" only once each time you
> invoke func(), even though func() is now accessing cached three times.
>
> --
> You received this message because you are subscribed to the Google Groups
> "scala-functional" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scala-functional+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to scala-functional+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[scala-functional] Re: lazy val memoization

2018-04-10 Thread Adam Mackler
The value cached is defined inside the definition of the method func(), so 
everytime you invoke func() it defines a new local Int named cached.  
Change your code to this:

def func(i: => Int): Unit = {
  lazy val cached = {
println("Calculating")
i * i
  }
  println(cached)
  println(cached)
  println(cached)
}


and you will see that it prints "Calculating" only once each time you 
invoke func(), even though func() is now accessing cached three times.

-- 
You received this message because you are subscribed to the Google Groups 
"scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to scala-functional+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


[scala-functional] Re: lazy val memoization

2018-04-10 Thread des . magner
Ah OK. So I am guessing I misunderstood section 5.2.1. The "cons" function 
is only ever called once in this scenario?

On Tuesday, 10 April 2018 17:19:24 UTC+2, des.m...@gmail.com wrote:
>
> Hi
>
> Based on my understanding of the explanation of lazy val memoization in 
> section 5.2.1, I would have thought that if I define the following function:
>
> def func(i: => Int): Unit = {
> lazy val cached = {
> println("Calculating")
> i * i
> }
> cached
> }
>
> And then made the following calls:
>
> func(3)
> func(3)
>
> that my lazy evaluation should only happen once? - as the function is 
> getting called with the same parameter the second time. But this is not the 
> case when I test. Could someone explain what I am missing here?
>
> Thanks
> Des
>

-- 
You received this message because you are subscribed to the Google Groups 
"scala-functional" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to scala-functional+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.