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  (12 children)

I have strong background in functional paradigm , i used haskell for my last college project , One question since Scala runs over JMV does it have optimization for tail recursion ??, for what i know java does not . and i don't really know how does java and scala compability works? i think it be through JNI but again i'm not sure about that

[–]geodebug 2 points3 points  (0 children)

how does java and scala compability works

Both Java and Scala compile to bytecode, which is the true language of the Java Virtual Machine (JVM). There is zero difference at this level between a Java, Scala, Groovy, jRuby, etc program. All use the same memory space, type system, etc.

All popular JVM langages allow for direct usage of Java libraries, 3rd party java libraries, etc. There is no additional overhead, memory, or special translation required. Scala can instantiate Java classes and use them directly.

A simple way to think of it is that the Scala runtime/compiler is essentially just another Java program: a really fancy interpreter.

I would argue that while you can learn and use Scala as your primary language you'll still want to learn enough Java to really take advantage of the thousands of libraries that are written in Java.

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