• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 135
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 820
IntelliJ IDE VI Editor Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic