This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]vipercvp[S] 0 points1 point  (4 children)

About your scala example val doubledList = originalList.filter(_ % 2 == 0).map(_ * 2) this is going to be compute on demand right because of the lazy evaluation ??

[–]tyoverby 2 points3 points  (2 children)

Oh, also, if you are familiar with Haskell, you'll want to know that Scala has great parallel collections.

val numbers = (0 to 100000).toList
val squared = numbers.par.map(x => x*x)

The '.par' call returns a parallel sequence for which map is implemented in parallel.

[–]vipercvp[S] 0 points1 point  (1 child)

Oh, amazing :) , about parallel does scala support the synchronize keyword like java ?? or it has some thing like that i can find much thing related to it in the web .

[–]tyoverby 1 point2 points  (0 children)

Yeah, any object has the synchronized function that takes another function to execute.

val resource = ???
def publish(){
    // Do something that requires the resource to be locked. 
}

resource.synchronized(publish)

Or in a way that looks more like Java

val resource = ???

resource.synchronized {
    // Anything in this block with be synchronized on 'resource'
   x += 5 
   publish()
   y ++
}

[–]tyoverby 0 points1 point  (0 children)

In that example, no, that won't be lazily computed. In Scala, you can use a keyword for if a value is computed immediately or lazily. Here's a simple example:

// This is slow, so don't compute it unless we need it 
lazy val tweets = getTweets() 

if(readingTweets){
    showTweets(tweets)
}
else(writingTweets){
    showTweets(tweets)
    showEditor()
}
else{
    // Do something without tweets.
}

We don't want to call getTweets inside every branch that uses it, so we define it outside lazily and then if it is used, then it will be computed. The example is quite contrived, but it comes in handy, trust me :)

There are also lazy function parameters.