This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Scala Snippets

Source Description
abstractTypes.scala An example of how abstract types are used in a program
bigint.scala User-defined integers, BigInts, are used seamlessly
complexOps.scala Operators can be defined on user-defined classes, here complex numbers
for-yield.scala An example of the for and yield constructs
extendBuiltins.scala Adding "!" as a new method on integers
implicits.scala Define a new method 'sort' on arrays without changing their definition
maps.scala Maps are easy to use in Scala
match.scala Using pattern matching to recognize command line arguments
primes.scala A simple, although inefficient, way to calculate prime numbers
sum.scala Calculates the sum of the arguments supplied on the command line
varargs.scala Java varargs can be easily used in Scala as well

 

To compile and run on Windows one of the above Scala programs, let's say sort.scala, we can simply proceed as follows:

> mkdir classes
> scalac -d classes %SCALA_HOME%\doc\scala-devel-docs\scala\examples\sort.scala
> scala -cp classes examples.sort
[6,2,8,5,1]
[1,2,5,6,8]

The name of the Scala executable is examples.sort where examples is the name of the package containing the sort object. Running the test on a Unix system is very much similar, except for the use of slashes instead of backslashes, and a different specification of the Scala home directory.

If you are ready to explore more complicated examples, please continue to our Advanced Examples page.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland