Langbahn Team – Weltmeisterschaft

Scala (programming language): Difference between revisions

Content deleted Content added
m rm nested cats
82.181.80.97 (talk)
Line 37: Line 37:


==Functional programming==
==Functional programming==
Scala is also a [[functional language]] in the sense that every function is a value. Scala provides a lightweight syntax for defining [[anonymous function]]s, it supports [[higher-order function]]s, it allows functions to be nested, and supports [[currying]]. Scala's case classes and its built-in support for [[pattern matching]] model algebraic types used in many functional programming languages.
Scala is also a [[functional language]] in the sense that every function is a value. Scala provides a lightweight syntax for defining [[anonymous function]]s, it supports [[higher-order function]]s, it allows functions to be nested, and supports [[currying]]. Scala's case classes and its built-in support for [[pattern matching]] model algebraic types used in many functional programming languages.


Furthermore, Scala's notion of pattern matching naturally extends to the processing of [[XML]] data with the help of [[regular expression]] patterns. In this context, sequence [[List comprehension|comprehensions]] are useful for formulating queries. These features make Scala ideal for developing applications like web services.
Furthermore, Scala's notion of pattern matching naturally extends to the processing of [[XML]] data with the help of [[regular expression]] patterns. In this context, sequence [[List comprehension|comprehensions]] are useful for formulating queries. These features make Scala ideal for developing applications like web services.

However, the underlying JVM makes some methods of functional programming very difficult to implement. For instance, some tail call optimizations for recursive functions are not supported in Scala, because the JVM lacks the bytecodes for this.


An implementation of a [[Quicksort]] algorithm in functional style, for comparison with the [[Erlang_(programming_language)|Erlang Quicksort example]]:
An implementation of a [[Quicksort]] algorithm in functional style, for comparison with the [[Erlang_(programming_language)|Erlang Quicksort example]]:

Revision as of 16:21, 25 March 2008

This article is about the programming language. For other uses, see Scala.
Scala
Paradigmmulti-paradigm: functional, object-oriented
Designed byMartin Odersky
DeveloperProgramming Methods Laboratory of EPFL
First appeared2003
Stable release
2.6.1 / December 19, 2007
Typing disciplinestatic, strong, inferred
Websitehttp://www.scala-lang.org
Major implementations
Scala
Influenced by
Java, Haskell, Standard ML, Objective Caml, Smalltalk

Scala (Scalable Language) is a multi-paradigm programming language designed to integrate features of object-oriented programming and functional programming.[1]

Platform

Scala runs on the Java Platform (Java Virtual Machine) and is compatible with existing Java programs. An alternative implementation exists for the .NET platform, but the main platform remains the Java Platform[2]. It also runs on Java Platform, Micro Edition Connected Limited Device Configuration.

History

Scala was created at the École Polytechnique Fédérale de Lausanne (EPFL) in 2001 by Martin Odersky. It was released publicly on the Java platform in January 2004, and on the .NET platform in June the same year. A second version of the language was released in March 2006.[1]

Object-oriented features

Scala is a pure object-oriented language in the sense that every value is an object. Types and behavior of objects are described by classes and traits. Class abstractions are extended by subclassing and a flexible mixin-based composition mechanism as a clean replacement for multiple inheritance.

Functional programming

Scala is also a functional language in the sense that every function is a value. Scala provides a lightweight syntax for defining anonymous functions, it supports higher-order functions, it allows functions to be nested, and supports currying. Scala's case classes and its built-in support for pattern matching model algebraic types used in many functional programming languages.

Furthermore, Scala's notion of pattern matching naturally extends to the processing of XML data with the help of regular expression patterns. In this context, sequence comprehensions are useful for formulating queries. These features make Scala ideal for developing applications like web services.

However, the underlying JVM makes some methods of functional programming very difficult to implement. For instance, some tail call optimizations for recursive functions are not supported in Scala, because the JVM lacks the bytecodes for this.

An implementation of a Quicksort algorithm in functional style, for comparison with the Erlang Quicksort example:

def qsort(l: List[Int]): List[Int] = {
    l match {
        case List() => l
        case _ =>  qsort(for(x <- l.tail if x < l.head) yield x) ::: List(l.head) ::: qsort(for(x <- l.tail if x >= l.head) yield x)
    }
}

Static typing

Scala is equipped with an expressive type system that enforces statically that abstractions are used in a safe and coherent manner. In particular, the type system supports:

Extensibility

The design of Scala acknowledges the fact that in practice, the development of domain-specific applications often requires domain-specific language extensions. Scala provides a unique combination of language mechanisms that make it easy to smoothly add new language constructs in the form of libraries:

  • any method may be used as an infix or postfix operator, and
  • closures are constructed automatically depending on the expected type (target typing).

A joint use of both features facilitates the definition of new statements without extending the syntax and without using macro-like meta-programming facilities.

Platform independence

Scala is designed to interoperate well with popular programming environments like the Java 2 Runtime Environment (JRE) and the .NET CLR[2]. In particular, the interaction with mainstream object-oriented languages like Java and C# is as smooth as possible.

In other words, Scala can effortlessly make use of all libraries available for Java/C#, addressing the common drawback of using advanced functional languages which is that the small community, much of it centered around academia, often does not get to implementing quality libraries for common real-world tasks such as relational database access, XML processing, regular expressions, and so on. Scala can accomplish those tasks in a manner very similarly to how one would in Java or C#.

Scala has the same compilation model (separate compilation, dynamic class loading) as Java and C#, and allows thousands of high-quality libraries to be accessed.

Hello World example

Here is the canonical Hello world program written in Scala:

object HelloWorld extends Application {
  println("Hello, world!")
}

or

object HelloWorld {
  def main(args: Array[String]) =
    println("Hello, world!")
}

Notice how similar it is to the stand-alone Hello World application for Java. The notable difference is that we do not declare anything to be static or void; the object keyword gives us a singleton object, freeing us from having to invoke any such constructs.

One would then compile this from the command line roughly as follows, assuming it was saved in a filename called HelloWorld.scala:

> scalac HelloWorld.scala

One would then run it like so:

> scala -classpath . HelloWorld

This is analogous to how one compiles and runs a Java "hello world" program. Indeed, Scala's compilation and execution model is identical to that of Java, making it compatible with Java build tools such as Ant.

Testing

There are some ways to test code in Scala:

  • The scala-library itself provides SUnit, a xUnit like framework using Assertions.
  • The Rehersal project provides a more extensive framework using Expectations and natural language for test names. It also has integration with Apache Ant.
  • ScalaCheck
  • JUnit

References

  1. ^ a b Martin Odersky et al, An Overview of the Scala Programming Language, 2nd Edition
  2. ^ a b "Scala on .NET". Programming Methods Laboratory of EPFL. 2008-01-07. Retrieved 2008-01-15. Scala is primarily developed for the JVM and embodies some of its features. Nevretheless, its .NET support is designed to make it as portable across the two platforms as possible.

See also