Scala

From WhyNotWiki
Jump to: navigation, search

Floyd Marinescu (Jun 09, 2006). Scala: combining the best of Ruby and Java? (http://www.infoq.com/news/Scala--combing-the-best-of-Ruby-). Retrieved on 2007-05-11 11:18.


While the Ruby vs. Java / dynamic vs. static debate continues, a small community has been forming around the Scala programming language, which has has some of the best features of both languages but is also a functional programming language and runs on the JVM. Like Ruby, Scala has a very terse syntax and its extensibility makes it suitable for writing DSLs, like Java, Scala is statically typed and can call Java code seamlessly without any declarations or glue code. An older version of Scala also compiles to .NET. Scala founder Martin Odersky (who co-designed Java Generics and implemented javac) has started blog on Artima today with his first entry on the history which led up to Scala: Scala was designed to be both object-oriented and functional. It is a pure object-oriented language in the sense that every value is an object. Objects are defined by classes, which can be composed using mixin composition. Scala is also a functional language in the sense that every function is a value. Functions can be nested, and they can operate on data using pattern matching. Earlier this year, Ted Neward also picked up on the "Ruby-esque" features of Scala and made three detailed blog entries introducing Scala, demonstrating it's brevity, and explaining it's object oriented design. Key features, from the Scala homepage:

  • Scala is object-oriented. 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.
  • Scala is functional. 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 right-ignoring sequence patterns. In this context, sequence comprehensions are useful for formulating queries. These features make Scala ideal for developing applications like web services (external link).
  • Scala is statically typed. 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, generic classes, variance annotations, upper and lower type bounds, inner classes and abstract types as object members, compound types, explicitly typed self references, views, and polymorphic methods. A local type inference mechanism takes care that the user is not required to annotate the program with redundant type information. In combination, these features provide a powerful basis for the safe reuse of programming abstractions and for the type-safe extension of software.
  • Scala is extensible. 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 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.

[Currying (category)]

A Tour of Scala: Currying (http://www.scala-lang.org/intro/currying.html). Retrieved on 2007-05-11 11:18.


object CurryTest extends Application {

  def filter(xs: List[Int], p: Int => Boolean): List[Int] =
    if (xs.isEmpty) xs
    else if (p(xs.head)) xs.head :: filter(xs.tail, p)
    else filter(xs.tail, p)

  def modN(n: Int)(x: Int) = ((x % n) == 0)

  val nums = List(1, 2, 3, 4, 5, 6, 7, 8)
  println(filter(nums, modN(2)))
  println(filter(nums, modN(3)))
}
Retrieved from "http://whynotwiki.com/Scala"
Ads
Personal tools