CSC/ECE 517 Fall 2014/ch1a 9 kn
Functional Programming Languages i.e Scala over Object Oriented Languages i.e Java
What is Functional Programming
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:
- Haskell
- Scala
- Erlang
What is Object-oriented Programming
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:
- Encapsulation
- Inheritance
- Polymorphism
- Reflection
- Abstraction
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
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 currying, pattern matching, 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:
- 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 the definition of higher-order functions. These are functions that take other functions as parameters, or whose result is a function. Here is a function apply which 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.
Immutable Collections
Scala collections systematically distinguish between mutable and immutable collections. Immutable collections, by contrast, never change. You 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 to this link: https://medium.com/binary-dreams/from-java-to-scala-tail-recursion-a6acdd71a94d
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. 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
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. 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 over 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
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
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
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.
Conclusion
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 |
---|---|---|
|
Scala is both Object oriented and Functional programming language. | Java is Object oriented programming language. |
|
Scala syntax is very concise, compact and readable. The code is less error prone due to the better readability and conciseness. | Java syntax is lengthier and less readable which makes the debugging difficult. Since the code is lengthier it is more error prone. |
|
Scala code can completely inter-operate with Java code and library and other languages like C++ | Java code is converted into byte code and then compiled. |
|
Scala being a new language with complex syntax is difficult to learn especially for beginners. | Java code is simpler hence easier to learn. |
|
Scala is a young language with limited community support. It is very difficult to find Scala framework and libraries | Java is a much mature language with world-wide community support.Unlimited Java based framework and libraries are there to integrate almost everything. |
|
Scala doesn't have backward compatibility with previous versions. | Java is back-ward compatible with previous versions. |
|
Scala is a light language and consumes less CPU resources. | Java is a heavy language and consumes more CPU resources. |
|
It is easy to write concurrent programs in Scala using Actor based model implementation API's | It is difficult and complex to write concurrent programs using Java threads. |
References
http://searchsoa.techtarget.com/definition/object-oriented-programming
http://en.wikipedia.org/wiki/Object-oriented_programming
http://javarevisited.blogspot.com/2013/11/scala-vs-java-differences-similarities-books.html
http://www.infoq.com/articles/scala-java-myths-facts
http://beust.com/weblog/2011/02/23/from-scala-back-to-java/
http://www.celerity.com/blog/2012/10/23/pros-cons-scala/
http://en.wikipedia.org/wiki/Java_(programming_language)
http://en.wikipedia.org/wiki/Scala_(programming_language)
http://www.haskell.org/haskellwiki/Functional_programming
http://en.wikipedia.org/wiki/Functional_programming
http://www.scala-lang.org/documentation
http://en.wikipedia.org/wiki/Scala_(programming_language)
http://java.dzone.com/articles/moving-java-scala-one-year
http://www.javaworld.com/article/2078610/java-concurrency/functional-programming--a-step-backward.html
Further Readings
http://www.codecommit.com/blog/scala/roundup-scala-for-java-refugees