CSC/ECE 517 Fall 2009/wiki1b 10 sf: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
Scala
Scala


It was started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky . It unifies ‘Functional Programming’ and ‘Object Oriented Programming’.  It is object-oriented in the sense that everything in Scala is an object. The functionality and behavior is grouped into classes and traits and subclassing can be done by extending the same. In Scala every function returns a value. Functions can be anonymous, nested or curried.
It was started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky . It unifies ‘Functional Programming’ and ‘Object Oriented Programming’.  It is object-oriented in the sense that everything in Scala is an object. The functionality and behavior is grouped into classes and traits and subclassing can be done by extending the same. In Scala every function returns a value. Functions can be anonymous, nested or curried.
Line 10: Line 12:


Ruby
Ruby


It was first released in December 1995 and was created by Yukihiro Matsumoto. It is an object oriented language in which data types and classes are objects.  Ruby supports only single inheritance. It does not support multiple inheritance but modules and mixins can be used to import functionality of other classes.
It was first released in December 1995 and was created by Yukihiro Matsumoto. It is an object oriented language in which data types and classes are objects.  Ruby supports only single inheritance. It does not support multiple inheritance but modules and mixins can be used to import functionality of other classes.


Ruby also has features like exception handling, garbage collection, OS independent threading and is highly portable. Ruby can be embedded in C language.
Ruby also has features like exception handling, garbage collection, OS independent threading and is highly portable. Ruby can be embedded in C language.
Ruby Vs Scala
Differences with coding perspective
1)      Scala is statically typed wherein each variable, method parameter, return type etc has one type assigned at compile time and that dose not change. Look at the piece of code shown below types on the command prompt -:
scala> val msg = "Hello, world!"
msg: java.lang.String = Hello, world!
When we assign “Hello,world!” to ‘msg’ the scala compiler automatically associates message with string data type as shown by the output in the next line. Hence when the code runs in the JVM, ‘msg’ is a string data type.
Ruby on the other hand is dynamically typed which means a variables type can changes at run time.
def say_goodnight(name)
  result = "Good Night, " + name
  result = 10
  return result
end
puts say_goodnight("xyz")
The output of this code spippet shown above will be 10 and not ‘Good Night, xyz’. As you can see the variable result can be assigned a value which can be a string or an integer.
2)      Every function is a method is Ruby whereas functions are objects in Scala.
3)      Although both Scala and Ruby are almost the same in terms of ease of use but there are some finer points of differences for example -:
Ruby allows you to leave off parenthesis when invoking a method. For example-:
>> s = "hello"
You can determine whether it contains a substring like this:
>> s.include?("el")
=> true
Or like this:
>> s.include? "el"
Whereas Scala does not allow you to leave off parenthesis.
scala> val s = "hello"                                           
s: java.lang.String = hello
scala> s.contains("el")
res5: Boolean = true
scala> s.contains "el"
:1: error: ';' expected but string literal found.
      s.contains "el"
                  ^
However, Scala supports an "operator notation," which allows you to leave off both the dot and the parentheses:
scala> s contains "el"
res6: Boolean = true
Scala supports an "operator notation" that allows to leave off both the dot and the parentheses. For example -:
scala> s contains "el"
res6: Boolean = true
On the other hand, Ruby does not support this. For example -:
>> s include? "el"
(irb):21: warning: parenthesize argument(s) for future version
NoMethodError: undefined method `include?' for main:Object
            from (irb):21
4)      Scala does not have regulat expressions at the language leel where as Ruby has it which makes it easier to parse text/strings in Ruby.
5)      Ruby is a dynamic language and hence supports unbounded polymorphism through duck typing whereas Scala cannot support duck typing.
6)      Ruby supports class extensions by having open classes. Scala on the other hand supports implicit type conversion.
In ruby classes are not enclosed. More methods can be added to pre defined c      lasses by simply opening up the class and adding the methods in there.
class String
  def print_self
    puts self
  end
end
"Daniel Spiewak".print_self    # prints my name
In this code print_self is being added to the String class and hence can subsequently be invoked on String objects. 
In Scala if an object of a certain data type, is passed to a method that does not take such a data type as an input, then the compiler looks around for a method that converts the unsupported data type to a data type that can be passed to the method. It implicitly uses the method to convert an object from an unsupported data type to the supported data type and then calls the function on this object.
implicit def str2int(str:String):Int = Integer.parseInt(str)
def addTwo(a:Int, b:Int) = a + b
addTwo("123", 456)
In the example shown above although the user passes a string to the addTwo method, the compiler looks around and finds the str2int method that converts string to an integer and passes the integer as the first parameter to the addTwo function.
7)      Scala is faster than Ruby because the Scala interpreter creates Java classes behind the scene, that run on the JVM.

Revision as of 23:05, 19 September 2009

Scala


It was started in 2001 at the École Polytechnique Fédérale de Lausanne (EPFL) by Martin Odersky . It unifies ‘Functional Programming’ and ‘Object Oriented Programming’. It is object-oriented in the sense that everything in Scala is an object. The functionality and behavior is grouped into classes and traits and subclassing can be done by extending the same. In Scala every function returns a value. Functions can be anonymous, nested or curried.

Scala is extensible because it allows to integrate new language constructs in form of libraries. It is capable if running both on the Java platform and .NET platform.

The interpreter behind Scala is called ‘scalac’. It generates java classes which can be run on the JVM.


Ruby


It was first released in December 1995 and was created by Yukihiro Matsumoto. It is an object oriented language in which data types and classes are objects. Ruby supports only single inheritance. It does not support multiple inheritance but modules and mixins can be used to import functionality of other classes.

Ruby also has features like exception handling, garbage collection, OS independent threading and is highly portable. Ruby can be embedded in C language.


Ruby Vs Scala


Differences with coding perspective


1) Scala is statically typed wherein each variable, method parameter, return type etc has one type assigned at compile time and that dose not change. Look at the piece of code shown below types on the command prompt -:


scala> val msg = "Hello, world!"

msg: java.lang.String = Hello, world!


When we assign “Hello,world!” to ‘msg’ the scala compiler automatically associates message with string data type as shown by the output in the next line. Hence when the code runs in the JVM, ‘msg’ is a string data type.


Ruby on the other hand is dynamically typed which means a variables type can changes at run time.


def say_goodnight(name)

 result = "Good Night, " + name
 result = 10
 return result

end

puts say_goodnight("xyz")


The output of this code spippet shown above will be 10 and not ‘Good Night, xyz’. As you can see the variable result can be assigned a value which can be a string or an integer.


2) Every function is a method is Ruby whereas functions are objects in Scala.

3) Although both Scala and Ruby are almost the same in terms of ease of use but there are some finer points of differences for example -:


Ruby allows you to leave off parenthesis when invoking a method. For example-:


>> s = "hello"


You can determine whether it contains a substring like this:


>> s.include?("el")

=> true


Or like this:


>> s.include? "el"


Whereas Scala does not allow you to leave off parenthesis.


scala> val s = "hello"

s: java.lang.String = hello


scala> s.contains("el")

res5: Boolean = true


scala> s.contains "el"

1: error: ';' expected but string literal found.
      s.contains "el"
                 ^


However, Scala supports an "operator notation," which allows you to leave off both the dot and the parentheses:


scala> s contains "el"

res6: Boolean = true


Scala supports an "operator notation" that allows to leave off both the dot and the parentheses. For example -:

scala> s contains "el" res6: Boolean = true



On the other hand, Ruby does not support this. For example -:


>> s include? "el"

(irb):21: warning: parenthesize argument(s) for future version

NoMethodError: undefined method `include?' for main:Object

           from (irb):21



4) Scala does not have regulat expressions at the language leel where as Ruby has it which makes it easier to parse text/strings in Ruby.

5) Ruby is a dynamic language and hence supports unbounded polymorphism through duck typing whereas Scala cannot support duck typing.

6) Ruby supports class extensions by having open classes. Scala on the other hand supports implicit type conversion.

In ruby classes are not enclosed. More methods can be added to pre defined c lasses by simply opening up the class and adding the methods in there.


class String

 def print_self
   puts self
 end

end


"Daniel Spiewak".print_self # prints my name


In this code print_self is being added to the String class and hence can subsequently be invoked on String objects.

In Scala if an object of a certain data type, is passed to a method that does not take such a data type as an input, then the compiler looks around for a method that converts the unsupported data type to a data type that can be passed to the method. It implicitly uses the method to convert an object from an unsupported data type to the supported data type and then calls the function on this object.


implicit def str2int(str:String):Int = Integer.parseInt(str)

def addTwo(a:Int, b:Int) = a + b


addTwo("123", 456)


In the example shown above although the user passes a string to the addTwo method, the compiler looks around and finds the str2int method that converts string to an integer and passes the integer as the first parameter to the addTwo function.


7) Scala is faster than Ruby because the Scala interpreter creates Java classes behind the scene, that run on the JVM.