shortTitle | shortDescription | expandText | anchorTarget | icon |
---|---|---|---|---|
Ideal for teaching | Scala is ideal for teaching programming to beginners as well as for teaching advanced software engineering courses. | Why teach Scala? | why-teach-scala | icon7.svg |
Most of the concepts involved in software design directly map into Scala constructs. The concise syntax of Scala allows the teachers and the learners to focus on those interesting concepts without dealing with tedious low-level implementation issues.
The example in file HelloWorld.scala
below shows how a “hello world” program looks like in Scala. In Modeling.scala
, we show an example of structuring the information of a problem domain in Scala. In Modules.scala
, we show how straightforward it is to implement software modules with Scala classes. Last, in Algorithms.scala
, we show how the standard Scala collections can be leveraged to implement algorithms with few lines of code.
Learn more in the dedicated page about Teaching.
@main def run() = println("Hello, World!")
// Average number of contacts a person has according to age def contactsByAge(people: Seq[Person]): Map[Int, Double] = people .groupMap( person => person.age )( person => person.contacts.size ) .map((age, contactCounts) => val averageContactCount = contactCounts.sum.toDouble / contactCounts.size (age, averageContactCount) )
/** A Player can either be a Bot, or a Human. * In case it is a Human, it has a name. */ enum Player: case Bot case Human(name: String)
<div class="code-element dark"> <div class="bar-code"><span>Modules.scala</span></div> <pre><code>// A module that can access the data stored in a database
class DatabaseAccess(connection: Connection): def readData(): Seq[Data] = ???
// An HTTP server, which uses the DatabaseAccess
module class HttpServer(databaseAccess: DatabaseAccess): // The HTTP server can call readData
, but it cannot // access the underlying database connection, which is // an implementation detail databaseAccess.readData()