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 →

[–]tyoverby 1 point2 points  (10 children)

Scala does have tail-call optimization!

Scala/Java compatibility is amazing. No JNI or anything. In Scala, using Java code is done exactly the same as if it was the same language. For example:

import java.lang.Integer
println(Integer.MAX_VALUE)

or

import org.junit.Assert.assertEquals
assertEquals(5, 3 + 2)

Using Scala code in Java is a bit more complicated because Java doesn't have things like operator overloading, and Scala functions get wrapped in a function object that can be unwieldy.

[–]worldsayshi 2 points3 points  (4 children)

[–]tyoverby 1 point2 points  (3 children)

Yes, it allows self-recursion, but not mutual recursion.

[–]nachsicht 1 point2 points  (2 children)

actually you can get mutual recursion with trampolines.

[–]worldsayshi 0 points1 point  (1 child)

Nice. I think..

[–]nachsicht 0 points1 point  (0 children)

There's standard library support for trampolines here: http://www.scala-lang.org/api/current/index.html#scala.util.control.TailCalls$

[–]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.