Hi all! recently I have started learning Scala and :). Since I am coming from Java world, it was difficult to catch up functional style and use the language effectively. When I have started the Hello world in Scala, I asked myself what is so special about functions in Scala? How do they differ from methods in Java program and why are we going back to functional programming which was introduced before OOP. Before answer these questions, there is a big questions which should be answered first is “What is functional programming?”..
Functional programming is about treating computation as the evaluation of mathematical functions and avoids state and mutable data [Wikipeadia]. For example y = log(x) is a function. Here output of the function depends only on input . In other words, if we call this function with x=10 for thousands times, it will return same value. It does not depends on global state or mutable variable. We can call this function anywhere without worrying about the context which is bound to. This property simplifies debugging, analyzing a function and support concurrent execution also.
Oh this is also can be achieved in a Java method, isn't it? What is special about functions in Scala? Functions are first class construct in functional programming like object/primitive types in Java. Functions can be stored in a variable, it can be passed to another function , moreover functions can return a function. So in Scala we can do above operations with functions. So “How do they differ from Java methods?”. Java methods can act like functions if it does not have a reference to a shared state but they do not have other properties of pure functions.
“why are we going back to functional programming ….?”. This is a trickier question than other ones. By using Scala, we are not going to loose any OOP features. Since it is a hybrid object oriented functional language , it is going to make things easier.. Here is an example of power of functions and this would require more lines of code in Java .Following Scala program reads a text file line by line and ignore the commented lines start with #.
Functional programming is about treating computation as the evaluation of mathematical functions and avoids state and mutable data [Wikipeadia]. For example y = log(x) is a function. Here output of the function depends only on input . In other words, if we call this function with x=10 for thousands times, it will return same value. It does not depends on global state or mutable variable. We can call this function anywhere without worrying about the context which is bound to. This property simplifies debugging, analyzing a function and support concurrent execution also.
Oh this is also can be achieved in a Java method, isn't it? What is special about functions in Scala? Functions are first class construct in functional programming like object/primitive types in Java. Functions can be stored in a variable, it can be passed to another function , moreover functions can return a function. So in Scala we can do above operations with functions. So “How do they differ from Java methods?”. Java methods can act like functions if it does not have a reference to a shared state but they do not have other properties of pure functions.
“why are we going back to functional programming ….?”. This is a trickier question than other ones. By using Scala, we are not going to loose any OOP features. Since it is a hybrid object oriented functional language , it is going to make things easier.. Here is an example of power of functions and this would require more lines of code in Java .Following Scala program reads a text file line by line and ignore the commented lines start with #.
import io.Source
object Runner {
def main(arg: Array[String]) {
val path = "data.txt"
val lns = Source.fromFile(path).getLines.map(_.stripLineEnd).filter(l => !l.isEmpty && !(l.trim startsWith "#"))
for (line <- lns) {
println(line)
}
}
}
Here .getLines returns an Iterator with Strings. After that map function is invoked in that Iterator . Map function is a higher order function (Higher order function is a function which accept another function as a input) which applies stripLineEnd on all strings in the Iterator. Likewise filter function applies validation logic on all strings in the newly return Iterator. Finally, we get an Iterator with strings..
What are the other advantages Scala can give us ;).Functional programming languages provides concurrency by message passing and make easier to write concurrent programs with out worrying about shared states and locks. Scala provides actor based message passing to write concurrent applications. Nowadays we have processors with multiple cores in a single chip. Most of the mainstream languages concurrency models are working with locks. Lock based concurrency will require additional complex synchronization logic to work with multi core processors where Scala like languages can make it easier. Other than these advantages we get things done with less lines of code ;).
What are the other advantages Scala can give us ;).Functional programming languages provides concurrency by message passing and make easier to write concurrent programs with out worrying about shared states and locks. Scala provides actor based message passing to write concurrent applications. Nowadays we have processors with multiple cores in a single chip. Most of the mainstream languages concurrency models are working with locks. Lock based concurrency will require additional complex synchronization logic to work with multi core processors where Scala like languages can make it easier. Other than these advantages we get things done with less lines of code ;).
No comments:
Post a Comment