Scala (programming language): Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Tom Morris
No edit summary
imported>Tom Morris
No edit summary
Line 1: Line 1:
{{subpages}}
{{subpages}}


'''Scala''' is a high-level, compiled, type-checked programming language designed by [[Martin Odersky]]. It is based on the [[Java]] Virtual Machine (JVM)<ref>A .NET implementation exists, but the Java implementation is far more mature.</ref> and has full access to classes, libraries and modules used in the host language. Scala combines the functional programming style of Haskell, Smalltalk, Lisp and Ruby with strong object-oriented programming capabilities. The powerful ''traits'' system allows the object/type system to be significantly more flexible, in much the same way that Ruby's "mixins" do. The current Scala implementation allows you to compile code using <kbd>scalac</kbd>, and to run Scala code interactively in a Scala shell.
'''Scala''' is a high-level, compiled, type-checked programming language designed by [[Martin Odersky]]. It is based on the [[Java]] Virtual Machine (JVM)<ref>A .NET implementation exists, but the Java implementation is far more mature.</ref> and has full access to classes, libraries and modules used in the host language. Scala combines the functional programming style of Haskell, Smalltalk, Lisp and Ruby with strong object-oriented programming capabilities. The powerful ''traits'' system allows the object/type system to be significantly more flexible, in much the same way that Ruby's "mixins" do. The current Scala implementation allows you to compile code using scalac, and to run Scala code interactively in a Scala shell.
 
== Prime Number Example ==
<code><pre>/** Print prime numbers less than 100, very inefficiently */
object primes extends Application {
  def isPrime(n: Int) = (2 until n) forall (n % _ != 0)
  for (i <- 1 to 100 if isPrime(i)) println(i)
}</pre></code><ref>Taken from the [http://www.scala-lang.org/node/230 Scala website examples section]</ref>
 
This example code shows a number of features of the language, including the cross-over between the object-orientated and functional styles. An object is being defined rather than a class, following the [[Singleton pattern]]. The object uses the Application trait to turn it into an executable script. It defines a method on the primes object called isPrime which takes one Int object (Scala wraps Java's built-in int type in an object with an upper-case first character) and iterates on all numbers from two through n (the "(2 until n)" constructs a list of all those numbers), and then calculates if it is prime by dividing n by that number and seeing if it returns zero. forall is being called as a method on the array constructed by the first bracketed segment in the method definition, and the second bracketed segment contains the loop - an anonymous or lambda function. The underscore character stands in for the current iteration. The for loop shows how iteration is conducted in Scala. println at the end of the line is a call to the System.out.println method in the Java library.
 


== Notes ==
== Notes ==
<references />
<references />

Revision as of 09:39, 24 September 2009

This article is a stub and thus not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
 
This editable Main Article is under development and subject to a disclaimer.

Scala is a high-level, compiled, type-checked programming language designed by Martin Odersky. It is based on the Java Virtual Machine (JVM)[1] and has full access to classes, libraries and modules used in the host language. Scala combines the functional programming style of Haskell, Smalltalk, Lisp and Ruby with strong object-oriented programming capabilities. The powerful traits system allows the object/type system to be significantly more flexible, in much the same way that Ruby's "mixins" do. The current Scala implementation allows you to compile code using scalac, and to run Scala code interactively in a Scala shell.

Prime Number Example

/** Print prime numbers less than 100, very inefficiently */
object primes extends Application {
  def isPrime(n: Int) = (2 until n) forall (n % _ != 0)
  for (i <- 1 to 100 if isPrime(i)) println(i)
}

[2]

This example code shows a number of features of the language, including the cross-over between the object-orientated and functional styles. An object is being defined rather than a class, following the Singleton pattern. The object uses the Application trait to turn it into an executable script. It defines a method on the primes object called isPrime which takes one Int object (Scala wraps Java's built-in int type in an object with an upper-case first character) and iterates on all numbers from two through n (the "(2 until n)" constructs a list of all those numbers), and then calculates if it is prime by dividing n by that number and seeing if it returns zero. forall is being called as a method on the array constructed by the first bracketed segment in the method definition, and the second bracketed segment contains the loop - an anonymous or lambda function. The underscore character stands in for the current iteration. The for loop shows how iteration is conducted in Scala. println at the end of the line is a call to the System.out.println method in the Java library.


Notes

  1. A .NET implementation exists, but the Java implementation is far more mature.
  2. Taken from the Scala website examples section