CSC/ECE 517 Fall 2014/ch1a 9 kn

From Expertiza_Wiki
Jump to navigation Jump to search

Functional Programming Languages i.e Scala over Object Oriented Languages i.e Java

What is Functional Programming<ref>http://en.wikipedia.org/wiki/Functional_programming</ref>

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional programming, programs are executed by evaluating expressions, in contrast with imperative programming where programs are composed of statements which change global state when executed. Functional programming typically avoids using mutable state. Examples of functional Programming languages are:

What is Object-oriented Programming<ref>http://searchsoa.techtarget.com/definition/object-oriented-programming</ref>

Object-oriented programming (OOP) is a programming language model which is organized around objects rather than "actions" and data rather than logic. A program has always been thought as a logical function that takes input data, processes it and produces the output. Object oriented programming cares about the objects we want to manipulate rather than the logic required to manipulate them. Objects here are usually instances of classes which are used to design applications and computer programs. Object-oriented programming languages have a come a long way starting from Simula all the way to languages like JAVA, Ruby etc. Some of the key features of object oriented programming languages are:

Introduction to Scala

Scala is an acronym for “Scalable Language”. It is an object- functional programming and scripting language which fuses functional and object-oriented programming in a practical package. Scala is now being used in a rapidly increasing number of open source projects and companies. It provides the core infrastructure for sites such as Twitter, LinkedIn, Foursquare, Tumblr, and Klout.

Aspects of Scala <ref>http://www.scala-lang.org/what-is-scala.html</ref>

Some of the major aspects of Scala are:

  • Object oriented

Scala is a pure-bred object-oriented language. Conceptually, every value is an object and every operation is a method-call. The language supports advanced component architectures through classes and traits. Many traditional design patterns in other languages are already natively supported in SCALA.

  • Functional

Even though its syntax is fairly conventional, Scala is also a full-blown functional language. Many of Scala's design decisions were inspired by criticism over the shortcomings of Java. Scala has full support for functional programming concepts such as pattern matching, currying, algebraic data types, lazy evaluation, tail recursion, immutability, etc.

  • Seamless Interoperability with Java

Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine. Java libraries may be used directly in Scala code, and vice versa.

A simple "Hello World" example in Scala:


 object HelloWorld extends App {
   println("Hello, World!")
 }

Introduction to Java

Java is one of the more advanced and popular object-oriented based computer programming language. Java is a computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible. Java applications are typically compiled to byte-code(class file) that can run on any Java virtual machine (JVM) regardless of computer architecture. The presence of Java Byte Code makes the language portable. There were five primary goals in the creation of the Java language<ref>http://en.wikipedia.org/wiki/Java_(programming_language)</ref>:

  • It should be "simple, object-oriented and familiar"
  • It should be "robust and secure"
  • It should be "architecture-neutral and portable"
  • It should execute with "high performance"
  • It should be "interpreted, threaded, and dynamic"

Syntactically Java is similar to C++ and a “Hello World” program in Java would look as follows:

class HelloWorldApp {
    public static void main(String[] args) {
         System.out.println("Hello World!"); 
    }
}

Features of Scala

Support for Higher-Order Functions

Scala allows to write definition of higher-order functions. Higher Order functions can be defined as functions that take other functions as parameters, or whose result is a function. Consider an example which is a function "apply". It takes another function f and a value v and applies function f to v:

def apply(f: Int => String, v: Int) = f(v)

Higher-order functions are very useful for refactoring code and reduce the amount of repetition. For example, typically most for loops can be expressed using maps. Custom iteration schemes, such as parallel loops, can be easily expressed using HOFs.

For a more detailed example, refer here.

Immutable Collections

Scala collections systematically distinguish between mutable and immutable collections. Immutable collections never change, in contrast to mutable collections. It can have still operations that simulate additions, removals, or updates, but those operations will in each case return a new collection and leave the old collection unchanged. Similarly, all of the collection objects (container types) in Scala, e.g. linked lists, arrays, sets and hash tables, are available in mutable and immutable variants, with the immutable variant considered the more basic and default implementation.This allows for very easy concurrency — no locks are needed as no shared objects are ever modified.

Currying

Methods may define multiple parameter lists. When a method is called with a fewer number of parameter lists, then this will yield a function taking the missing parameter lists as its arguments.

Pattern Matching

Scala has built-in support for pattern matching, which can be thought of as a more sophisticated, extensible version of a switch statement, where arbitrary data types can be matched (rather than just simple types like integers, booleans and strings), including arbitrary nesting. A special type of class known as a case class is provided, which includes automatic support for pattern matching and can be used to model the algebraic data types used in many functional programming languages. Scala has a built-in general pattern matching mechanism. It allows to match on any sort of data with a first-match policy. Here is a small example which shows how to match against an integer value:

object MatchTest1 extends App {
  def matchTest(x: Int): String = x match {
    case 1 => "one"
    case 2 => "two"
    case _ => "many"
  }
  println(matchTest(3))
}

The block with the case statements defines a function which maps integers to strings. The match keyword provides a convenient way of applying a function (like the pattern matching function above) to an object.

Lazy evaluation

Since pure computations are referentially transparent they can be performed at any time and still yield the same result. This makes it possible to defer the computation of values until they are needed, that is, to compute them lazily. Lazy evaluation avoids unnecessary computations and allows, for example, infinite data structures to be defined and used.

Tail Recursion

A function can be called tail recursive when the last thing that happened was a call to itself, and no other recursive calls were made earlier. A clever compiler can then avoid adding a new frame to the stack, thus saving memory. Unlike Java, Scala has this capability. For example, simple reverse function can easily be converted into using tail recursion by adding a variable to track the result. We will also take advantage of the @tailrec annotation which tells the compiler to expect tail recursion and throw an error if that is not the case.

def reverse_tail2(s: String): String = {
 @scala.annotation.tailrec
 def impl(ss: String, r: String): String = {
   if (ss === null) return null
   if (ss.tail.isEmpty) return ss.head + r
   impl(ss.tail, ss.head + r)
 }
 impl(s, “”);
}

For a more detailed example, refer here

Advantages of Scala over Java

Scala adds a large number of features compared with Java, and has some fundamental differences in its underlying model of expressions and types, which make the language theoretically cleaner and eliminate a number of "corner cases" in Java<ref>http://en.wikipedia.org/wiki/Scala_(programming_language)#Features_.28with_reference_to_Java.29</ref>. Here are a few factors which make Scala beneficial over java:

Conciseness

One of the major advantage of Scala over Java is the succinct and concise code. Scala drastically reduce number of lines from a Java application by making clever use of type inference, treating everything as object, function passing and several other features. Scala programs tend to be short. Fewer lines of code mean also leads to less effort at reading and understanding programs and fewer possibilities of defects. In Java, a class with a constructor often looks like this:

class MyClass {
   private int index;
   private String name;
   public MyClass(int index, String name) {
      this.index = index;
      this.name = name;
   }
}

In Scala, class will look like this instead:

class MyClass(index: Int, name: String)

Interoperability<ref>http://java.dzone.com/articles/moving-java-scala-one-year</ref>

Interoperability between Scala and other programming languages, including Java/C/C++, is very good. The reason is that Scala programs are compiled and run on Java Virtual Machine. Their run-time performance is usually on par with Java programs. Scala’s powerful features such as Implicit Conversions enhances its interoperability furthermore.

Better Modularity

Since functions are pure in Scala, you can organize your program in a better way in terms of modules. This is due to two characteristic of functional programming: higher-order functions and lazy evaluation. As a result, with Scala you can compose pure functions and build bigger abstractions out of smaller ones in a purely referentially transparent way.

Concurrency

It is a lot easier to achieve concurrency with Scala because the compiler takes care of most of the operations which normally require manual setting up state variables (like the iterator in a loop). In modern applications involving highly concurrent computing on multicore machines, state is a big problem<ref>http://www.javaworld.com/article/2078610/java-concurrency/functional-programming--a-step-backward.html</ref>. Many imperative languages, including object-oriented languages, involve multiple threads changing the shared state of objects. This is where deadlocks, stack traces, and low-level processor cache misses all take place. Hence For pure mathematical calculation, Scala and other functional programming languages make a great fit and probably a better approach.

Hybrid

Scala's hybrid ability to take advantage of FP and/or OOP as required for the task in hand is a great benefit. Scala code can call Java methods, access Java fields, inherit from Java classes, and implement Java interfaces. None of this requires special syntax, explicit interface descriptions, or glue code. In fact, almost all Scala code makes heavy use of Java libraries, often without programmers being aware of this fact.

Limitations of Scala compared to Java

Scala although a new generation JVM language built on taking advantage of the functional programming paradigm does have its disadvantages over Java. Some of the key disadvantages are:

Run-Time Performance

Performance differences usually arise from the features of Scala that are not natively supported by the JVM. This can be a problem for performance sensitive programs. A lean code at a high level of abstraction written in Scala can be compiled to a large amount of byte-code resulting in degrading runtime performance

Compilation Performance

Some Scala features, such as the search for implicit conversions take a long time to build, and the compiler would still need to check both the Scala type system and the type system of the underlying platform (JVM, CLR or other). All this makes compilation slower. The IDE or build tool can use incremental compilation to alleviate these problems, but for complex compilations or continuous integration, compiler performance can be an issue.

Hard To Learn<ref>http://www.infoq.com/articles/scala-java-myths-facts</ref>

Since the code of Scala is succinct it might look confusing and repulsive to read. It presents a completely different programming paradigm—requiring a higher level, more sophisticated developer/engineer skill set to understand it. Whereas Java code is much more easier to read because of its nested structure.

Code Example: A program that prints the thousandth element of the Fibonacci sequence

Code in Java:

import java.math.BigInteger;

public class FiboJava {
  private static BigInteger fibo(int x) {
    BigInteger a = BigInteger.ZERO;
    BigInteger b = BigInteger.ONE;
    BigInteger c = BigInteger.ZERO;
    for (int i = 0; i < x; i++) {
      c = a.add(b);
      a = b;
      b = c;
    }
    return a;
  }

  public static void main(String args[]) {
    System.out.println(fibo(1000));
  }
}

Code in Scala:

Code in Scala is definitely concise but at the same time hard to learn and understand.
object FiboFunctional extends App {
  val fibs:Stream[BigInt] =
    0 #:: 
    1 #:: 
    (fibs zip fibs.tail).map{ case (a,b) => a+b }
  println(fibs(1000))
}

Limited Community Presence<ref>http://javarevisited.blogspot.com/2013/11/scala-vs-java-differences-similarities-books.html</ref>

A lot of work has already been done by the Java community over Java tools and libraries which might be difficult to replicate. On the other side ScalaDocs, the official documentation of the class library, is incomplete in many aspects. If your developers do not have the time to resolve issues themselves, Scala may not be a viable option for you. If your developers do not have the time to resolve issues themselves, Scala may not be a viable option for you.

Limited Backward Compatibility<ref>http://www.celerity.com/blog/2012/10/23/pros-cons-scala/</ref>

Each major new release of Scala is incompatible with the previous version. This leads to a lot of wheel-reinventing, headaches and product delays. If your development schedule is tight, you should avoid using Scala, for now, to stay on track.

Comparison in a Nutshell

Let us compare both the programming paradigms with respect to several disctinction attributes.

Point of Comparison Functional Languages i.e Scala Object-Oriented Languages i.e Java
  • Primary Focus
Scala combines features of both Object oriented and Functional programming paradigm. The functional programming techniques lead to better scalability and flexibility than java. Java is strictly an Object oriented programming language.
  • Conciseness of code
Scala syntax is very concise and compact. The code is less error prone because conciseness of code leads to better readability and understanding of the code. Java syntax tends to be is lengthier as compared to scala which makes the debugging difficult. Since the code is lengthier it is also more error prone.
  • Interoperability
Scala code can completely inter-operate with Java code and library and other languages like C++. This is because scala runs on Java Virtual Machine. Java code is converted into byte code and then compiled.
  • Concurrency
It is easy to write concurrent programs in Scala using Actor based model implementation API's. Furthermore, the use of functional programming facilitates internal parallelism, automated by the compiler or libraries. It is difficult and complex to write concurrent programs using Java threads. Involve multiple threads to share changing state lead to deadlocks, stack traces, and low-level processor cache misses.
  • Frameworks and libraries
Scala has access to Java's existing libraries and frameworks. Apart from that, Scala also provides some native extensions such as Lift web framework and Akka actors platform. Java also has wide variety of libraries and frameworks to use from.
  • Performance
Scala is a light weight language and consumes less CPU resources. Both scala and java show similar runtime performances as both run on JVM. Java is a heavy weight language and consumes more CPU resources. Compile time performance of java is much better than scala as some features of scala such as implicit conversions take longer time to compile.
  • Ease of learning
Scala being a new language with complex syntax is difficult to learn especially for beginners. Java code is simpler hence easier to learn.
  • Community support
Scala is a young language with limited community support. The documentation of Scala framework and libraries is incomplete making it difficult for developers to resolve issues. Java is a much mature language with world-wide community support. Unlimited Java based framework and libraries are there to integrate almost everything.
  • Backward compatibility
Scala doesn't have backward compatibility with its previous versions. Java is back-ward compatible with previous versions.

Conclusion

Scala, a new generation JVM language, is gaining popularity day by day due to its usage of both functional and object-oriented paradigms. It comprises of an impressive set of features which leads to more concise, extendable and flexible code. Scala particularly shines when it comes to scalable server software that makes use of concurrent and synchronous processing, parallel utilization of multiple cores, and distributed processing in the cloud which makes it a language for the future . It still doesn't enjoy enough community presence and support that Java does. But as more and more Java developers are learning Scala and inspired by Twitter, more and more companies are using Scala.

References

<references/>

Further Readings

http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees
http://www.infoq.com/articles/java-8-vs-scala
http://www.javaworld.com/article/2078610/java-concurrency/functional-programming--a-step-backward.html
http://www.scala-lang.org/docu/files/ScalaTutorial.pdf