CSC/ECE 517 Fall 2010/ch1 1e AE: Difference between revisions
Line 278: | Line 278: | ||
} | } | ||
== Languages Supporting Functional and OO code == | == Languages Supporting Functional and OO code == |
Revision as of 20:30, 17 September 2010
Mixing Functional & O-O code
This chapter deals with the mixing of functional and object-oriented programming paradigms. We start with listing the different programming paradigms available today. We introduce functional and object-oriented approaches briefly and then discuss the advantages of one over the other. Following this, we discuss some aspects and features of mixing both the approaches. Finally, we talk about few languages that constitute such a mix.
Programming Paradigms Today
Several programmatic approaches to solve a particular task lead to distinct programming paradigms like,
- Imperative Programming
- Functional programming
- Object Oriented programming
- Logical programming, etc.
Lets focus on functional and object oriented programming approaches.
Functional Programming
Functional programming is the task of decomposing a problem into a number of functions. Selectively executing these functions results in the solution to the problem. The functions mostly behave as mathematical. For given set of inputs the program should always return the same output. It usually concentrates on what type of problem we are dealing with rather than on the details of how to solve the problem. For example lets say there is a function which calculates area of a square. If we use it six times then we end up with area of a cube. Thus functional programming helps to build complex systems.
Approach
Expressions are the collection of variables and operations resulting in a single value this also deals with the way of solving the problem as expressions. Hence also called Expression related programming. It is structured as expression where each expression can be encapsulated in another yielding the result. Making the change in data structures as the program runs is called side effects. Purely functional language has no side effects. Language like Haskell is a pure functional language . Some languages need many approaches for achieving the desired result. Such languages are multi-paradigm. Examples for such languages are C++, Python,Lisp. Its like evaluating an expression and use the resulting value for something.
Structure of functional programming :
(fn2 ( fn1 ()[input list] ) [])
Functional programming feature (Python) :
m = map(lambda i : i*i , numbers)
where numbers is an array of numbers and lambda is closure.
Python has some functions like map, filter, reduce to support functional programming style. In the example above, the 'map' is a high-order function that takes a function and an array of numbers as input. The lambda takes i as input and returns i*i as output. Every result returned is appended to a list and produced as output. In functional programming, the programmer is given a natural framework for developing smaller, simpler, and more general modules, which are then glued together.
Object Oriented Programming
Object oriented programming is a programming technique where everything is defined as an object. Objects are well-defined discrete bundles of data(attributes) and the associated functions(behavior). They are devised to resemble real-life objects and are uniquely identifiable by a name. The attributes could be of simple datatype(int, String, Boolean) or objects themselves. The behavior is defined by methods to use and manipulate the attributes. The values of attributes of an object at an instant is the state of the object. OO Programming allows objects to have their state retained throughout the code until manipulated otherwise. Objects are capable of housekeeping their own states, interacting and establishing relationships with other objects, inheriting, etc.
Fundamental concepts
A class is a user-defined datatype that provides implementation details to define the attributes and their properties. It is an abstract representation of an object(i.e.,) like a mold for objects. An instance is an executable copy of a class. They are the actual objects created using the ‘class mold’ on run time. Methods are a set of procedural statements or functions to define the behavior of an object, convey the current state, modify values of the attributes, etc.
Example :
An object in Java might look like this:
public class Person {
private String name; public Person() { } public void setName(String name) { this.name = name; } public String getName() { return this.name; }
}
Here, OO Programming allows the programmer to set the name attribute of the class Person and get the name whenever needed in the code. An object of this class Person will remain in memory forever as long as there is a reference to it. As, OO Programming allows objects to have their state retained, the value of the name attribute for a particular Person object remains the same across any part of the code until its set differently.
Features
Three primary characteristics of object oriented programming are Encapsulation, Polymorphism, and Inheritance. Encapsulation is the ability of objects to hide visibility of their attributes and behavior, thereby providing service independent of implementation, is called encapsulation. Polymorphism allows identical operations to behave differently in different contexts. The operations that can be performed on an object make up its interface. They enable addressing of operations with the same name in different objects. Inheritance is one where an existing type can be used to derive a new type. Derived types inherit the data and operations of the super-type. And also, they can overwrite existing operations or add new ones.
There are other important features of O-O programming like message passing, abstraction, delegation, etc.
Now, we have seen both the approaches and their features in brief. Lets discuss the advantages of one over the other.
Advantages
Functional Approach over OO Approach[8]
- Functional programming solves the problem with lesser number of lines of code than the OO programming style.
- There is no side effect as they use immutable data structures which will have no change in the state of variables. Hence program dont have to depend on history.
- There is no limit in the numeric types used as opposed to OO languages which mainly depend on primitive data types.
- A programmer is given the freedom to think in terms of what to solve in the problem rather than concentrating on how to solve and the flow of the program.
- Suitable for calculation intensive programs.
OO Approach over Functional Approach
- Everything can be represented in an object and it has the attributes associated with it and hence the state of the object is known at any time.
- A complex system can be subdivided into modules of objects where as in functional approach, many functions inter operate to form a complex system.
- Re-usability (i.e.) OO approach enables programmers to create new objects that inherits many of its features from existing objects. This makes object-oriented programs easier to modify than functional.
- Debugging in Object Oriented environment is easy compared with functional approach where a small error in one function causes the whole system (i.e. all the functions) to fail.
- Suitable for making application-oriented software.
Both the approaches have their advantages and disadvantages. What if we could get the best of both the worlds? The next section deals with the aspects and features of how well both the approaches are mixed together in some languages.
Mixing Functional & OO Code - Best of both the worlds
Languages that are a mix of functional and object oriented approaches(eg. Scala[1], Clojure[10], etc.) make use of best of both the worlds. They support various aspects of functional and object oriented approaches. They have a bunch of functional approach features[6], which object oriented programming languages(like Java) lack, including closures. Closures are first-class functions with free variables that are bound in the lexical environment. These languages also sport a very malleable syntax that makes them well suited for "domain specific languages" with the benefits of static typing. They are more expressive, extensible and allow reuse of programming abstractions.
Certain languages like Scala, Clojure, etc run on Java Virtual Machine(JVM) ensuring portability to all underlying platforms. They even support immutable objects. In Scala, new language constructs can be added in the form of libraries. They allow safe reuse[9] of programming abstractions and type-safe extension of software.
Lambda
A lambda[5] expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression types. Such kind of anonymous function can be defined and passed around as easily as other data types and can be used to contain functionality that need not be named. Some notable examples include closures[5] and currying.Lambda functions with no name can be called with the help of variable assigned to it.
g = lambda x: x*2 g(3)=6
Can have lambda function without assigning it to a variable which can be used as inline-function.
(lambda x: x*2)(3)
Clojure as a functional language, the creation of a chunk of behavior that can be assigned to a variable (known as a lambda) is trivial. Below are two ways to create a lambda that returns the square of its argument
(def square1 (fn [n] (* n n))) (def square2 #(* % %))
Currying
Currying transforms a function that takes multiple parameters into a chain of functions, each taking a single parameter.It reuses a partially applied function where the function is originally defined as accepting n parameters.
In Scala, currying takes advatage of Scala’s syntactic sugar. Below is a currying example in Scala. This function creates a multiplier function which takes two arguments and again partially define it by assigning a constant to one argument and assigning it to a variable.Later the function is called by assigning the second argument.
def multiplier(i: Int)(factor: Int) = i * factor val byFive = multiplier(5) _ val byTen = multiplier(10) _
scala> byFive(2) res4: Int = 10
scala> byTen(2) res5: Int = 20
Less Verbosity
In Java each variable should have its type defined beforehand. Whereas in languages like Scala, it is more concise and easier to read than the equivalent Java.
For example, a Map can be created with a simple syntax in Scala, and in Java:
Java Map<String, Integer> nMap = new HashMap<String, Integer>(); nMap.put("one", 1); nMap.put("two", 2); nMap.put("three", 3); |
Scala var nMap = Map("one" -> 1, "two" -> 2, "three" -> 3) |
Clojure (doto (new Java.util.HashMap)(.put “a” 1)(.put “b” 2)) |
Here, Scala compiler knows that nMap uses Strings as keys, and Integers as values. Unlike Java, this need not be specified beforehand. Scala could figure it out itself. Moreover, Scala will give an error if a different key-value pair is tried to insert. This is called "type inference". This prevents a whole class of bugs at run time just like Java but with less verbosity. In a similar way Clojure also involves only few lines of code compared with the object oriented Java language which takes many lines of code.A simple Clojure example and the associated Java code : Example
(defn blank? [s] (every? #(Character/isWhitespace %) s))
The innermost function Character/isWhiteSpace checks the first argument % for any whitespace and returns true if it is. every? invoke the above inner function for all elements in the collection s.It has no variables, no branches etc.
Java code isBlank() method checks to see whether a string is blank or contains only whitespace.
public class Stringcheck{ public static boolean isBlank(String str){ int len; if(str==null || (strlen =str.length())==0) return true; for(int i=0; i<strlen;i++) { if((Character.isWhitespace(str.charAt(i))==false)){ return false; } } return true; } }
Pattern Matching
To say in simply, a Pattern matching is a technique to search string for the occurrence of any patterns or regularity. Pattern matching is an elegant way to decompose objects into their constituent parts for processing.Many languages support the concept of pattern matching. Perl supports pattern matching using the pattern definition mini language called regular expressions, also called regexes or REs, which is borrowed from the regular expressions used in many Unix tools, such as grep(1) and sed(1).The risk that the parts of an object might be changed outside of the control of the enclosing object is avoided. For example, if there is a Person class that contains a list of addresses, it will not be an issue to expose that list to clients if the list is immutable. The users will not be able to change the list unexpectedly.Given below is a simple example for pattern matching used in Perl:
$mystring = "Hello world!"; // Perls way of assigning string to a variable. if($mystring =~ m/World/) { print "Yes"; } // Checks the string stored in variable mystring has the string "World". $mystring =~ s/world/mom/; // Replaces the string "world" stored in mystring to mom.
Another sample of regular expression in java :
String mystring="Hello World" ; Pattern str = Pattern.compile("\\w+"); Matcher fit = str.matcher(mystring); if(fit.matches())
As languages like Scala and Clojure support procedural code, pattern matching is done as equal or with less difficulty as in Java which needs to use regex. Scala can even be embedded into XML as its syntax is somewhat similar.Given an overview of how regular expression is achieved in Clojure. Data are manipulated in Clojure through the sequence called seq which is a logical list. Clojure can access java collections, its collections, Regular expression matches via the seq also called seq-able. Clojure's regular expression uses java.util.regex library at the lowest level. For achieving that the keyword re-seq is used.
Syntax: (re-seq regexp string) Example (re-seq #"\w+" "Hello World") => ("Hello" "World")
Java Interoperability
Considering Clojure, it gives clean, simple and direct access to java and can access Java API directly. It provides syntax for accessing Java elements like classes, instances, constructors, methods and fields and can also wrap Java API. The “.” dot special form is used for accessing Java. Some Clojure syntax which can access Java API:
Java | Clojure |
---|---|
new Widget(“red”) | (new Widget “red”) |
Math.PI | (.Math PI) or Math/PI |
System.currentTimeMillis() | (.System currentTimeMillis) or System/currentTimeMillis |
rnd.nextInt() | (.rnd nextInt) or (.nextInt rnd) |
Clojure pass functions as arguments to other functions. But Java does not support this. So Clojure provides a member function memfn macro to wrap methods or can use anonymous function to wrap a method call.
(map (memfn toUpperCase) [“a” ,”short”,”message”])
or
(map #(.toUpperCase %)[“a”,”short”,”message”])
For example,
(def the-digits (map #(Integer. (str %))(filter #(Character/isDigit %) (seq big-num-str))))
where
(def big-num-str (str "123785334434se9088af8309304293872adbcdfd”))
The above code extracts the integers from string and discards the non-numeric characters. Here
#(Character/isDigit) represents the Java method Characgter.isDigit() and #(Integer.(str %)) represents the new java.lang.Integer(str(%))
Many of the Clojure library functions have defined semantics for objects of Java types. contains? and .get work on Java’s Maps, arrays, Strings, count works on Java Strings, Collections and arrays. Clojure is built around these aspects. It avails the performance of JVM and the richness of both the core APIs and the numerous third-party libraries written in the Java language and restrained from reinventing them.
Multiple Inheritance
Multiple Inheritance[2] is the concept of using the methods and variables of more than one superclass to a sub class.Java was designed without multiple inheritance1. But Java supports the solution of problems commonly solved with multiple inheritance in other ways with help of interfaces where the methods defined in it are not implemented.In Scala, classes are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance in Java.Lets see how multiple inheritance achieved in Scala:
As interfaces in Java, Scala allows traits but has some methods partially implemented. The only difference to classes is the traits may not have constructor parameters.
trait Similarity { def isSimilar(x: Any): Boolean // not implemented def isNotSimilar(x: Any): Boolean = !isSimilar(x) //implemented method }
This trait defines two methods isSimilar and isNotSimilar where isSimilar does not provide a concrete method implementation (it is abstract in the terminology of Java), and the method isNotSimilar defines a concrete implementation. Consequently, classes that integrate this trait only have to provide a concrete implementation for isSimilar.
class Point(xc: Int, yc: Int) extends Similarity { var x: Int = xc var y: Int = yc def isSimilar(obj: Any) = //need to implement the method here obj.isInstanceOf[Point] && obj.asInstanceOf[Point].x == x }
object TraitsTest extends Application { val p1 = new Point(2, 3) val p2 = new Point(2, 4) val p3 = new Point(3, 3) println(p1.isNotSimilar(p2)) println(p1.isNotSimilar(p3)) //uses the already defined method println(p1.isNotSimilar(2)) }
Languages Supporting Functional and OO code
OCaml and F#[7] are some of the languages which support both functional and object oriented programming. OCaml is as fast as C and its featured as Haskell which is a pure functional language. F#[3] is a dynamically typed language which can run on .NET framework and has supported immutable types i.e. tuples, records, discriminated unions and lists to work in Functional programming. It has functions that can either be in curried or in uncurried form. As in functional language it can pass functions as arguments to other functions, resulting in higher order functions. It also supports lambda functions and closures. F#,behaves like other .NET languages as both in imperative and object oriented style.
Clojure is also a functional programming language and it is a dialect of Lisp. It is not a pure functional language and it is a dynamically typed language. Just like Java it runs on JVM platform. But its syntax differs from Java. It is represented as :
(functions arguments...)
The function followed by its arguments is enclosed in paranthesis. It has features like immutable data structures, high-order functions and recursion, easy and fast java interoperability.
Python and Ruby are also the programming languages which offer the mix of functional and OO languages. But these languages doesn't support the algebraic data types and pattern matching.
Conclusion
The above aspects of functional and object oriented programming techniques discuss some if its merits and demerits. Mixing them produces a new programming paradigm which lead to a new dimension in programming. A few fundamental concepts are explored in Clojure and Scala here. There are also other languages which support functional and object oriented techiniques. The ability of Clojure and Scala to run in JVM platform makes them more preferable than others in the market. Some of the merits like closure, less verbosity, high order functions, currying along with interoperation with previously written libraries in these languages make them more efficient and useful.
References
- Bagwell. Learning Scala. Available from http://www.scala-lang.org/node/1305 (2009); Accessed 24 April 2010.
- Bjarne Stroustrup. Multiple Inheritance for C++. 1999.
- Chris Smith. Programming F#. O'Reilly Media, Sebastopol, CA, 2009.
- Jean E. Sammet, David Hemmendinger. Encyclopedia of Computer Science. John Wiley and Sons Ltd., Chicester, UK, 2003.
- Jeremiah Willcock, Jaakko Jarvi, Doug Gregor, Bjarne Stroustrup, Andrew Lumsdaine. Lambda expressions and closures for C++. 2006.
- Lee Braine. An Object-Oriented Functional Approach to Information Systems Engineering, Department of Computer Science, University College London.
- Microsoft Corporation. Visual F#. Available from http://msdn.microsoft.com/en-us/library/dd233154.aspx (2010)
- Ph. Narbel. Functional Programming at Work in Object-Oriented Programming. ETH Zurich, Chair of Software Engineering, Journal of Object Technology, 2009. Vol. 8, No. 6.
- Shriram Krishnamurthi, Matthias Felleisen, Daniel P. Friedman. Synthesizing Object-Oriented and Functional Design to Promote Re-use. European Conference on Object-Oriented Programming, 1998.
- Stuart Hallway. Programming Clojure. Pragmatic Bookshelf, USA, 2009.