wood burning stoves*
The moose likes Scala and the fly likes what is an example of how Scala maintains Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "what is an example of how Scala maintains "referential transparency"" Watch "what is an example of how Scala maintains "referential transparency"" New topic
Author

what is an example of how Scala maintains "referential transparency"

Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Is my understanding correct that Scala, as a functional programming language, makes it easier to write functions that do not have "side effects" ?

I could use an example of how this is done in Scala but isn't as easy in Java.
Venkat Subramaniam
Author
Ranch Hand

Joined: Jan 28, 2008
Posts: 79
Hi Tim

Scala is not a pure functional language. It allows you the freedom
to write imperative code or functional code.

You can declare a reference as val or var. If you declare it val,
you can't reassign the reference. This is like making a reference
final in Java.

In Scala, it is a lot more concise and obvious that an instance
of a class is immutable.

//Java
public class StockPrice
{
private final String symbol;
private final int price;

public StockPrice(String theSymbol, int thePrice) {...}

//... getters
}


//Scala
class StockPrice(val symbol:String, val price:Int)

Less code for your eyes to parse. Also, if you do not mark the
field val or var, you will get a compilation error. So, there
are no forgotten finals. You are forced to make a conscious
decision on immutability.

Functional programming treats functions as first class citizens.
This means you can pass functions to functions (among other things).
This also makes it easier to write side effect free code.

Suppose I want to find the stock with highest price. Assume the
instances of the StockPrice are in a list named stockPrices.
Here is the code to find the maximum:

val stockWithHighPrice = (stockPrices(0) /: stockPrices) { (high, element)
if (element.price > high.price) element else high
}

Scala List (in this case stockPrices) is immutable.
You can't change the reference StockWithHighPrice or the object
that refers to. As long as the input to foldLeft is the same,
it's output is guaranteed to the same. It does not affect anything
outside and is not affected by anything outside. Also, once it
returns you a result, you can't change it. No side effects. The code
is concise as well (like any language you learn new, the syntax
takes some getting used to).
Tim McGuire
Ranch Hand

Joined: Apr 30, 2003
Posts: 820

Thank you, Venkat, that clarified things a lot. I just read up on fold left ( the /: in your example ) at http://dibblego.wordpress.com/2008/01/15/scalalistfoldleft-for-java-programmers/ and that is indeed a powerful construct.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is an example of how Scala maintains "referential transparency"