Git Product home page Git Product logo

firststeps's Introduction

First steps in Scala

This is a small Scala tutorial exercise to start getting used to the syntax of Scala programming.

It is typically taken before introducing functional programming concepts, so students should feel free to just use imperative programming to complete this tutorial.

The project is organised as an sbt project: src/main/scala contains the main code and src/test/scala contains tests that will be run against it if you run sbt test (or test from an interactive sbt prompt).

Useful to know before you start

A few language conventions are useful to remember before you begin the tutorial:

Companion objects instead of static methods

Scala compiles (by default) to run on the JVM. Just as Java requires a little overhead to define a program, so does Scala. So, this in Java

public class MyApp {

    public static void main(String[] args) {
        // program
    }
}

might be replaced by this in Scala:

object MyApp {

    def main(args:Array[String]) = {
        // program
    }

}

Notice that in Java, main is a static method on a class. In Scala, each class can have a "companion object" - a single object with the same name as the class. Methods defined on a companion object are (roughly) similar to static methods in Java.

The test harness supplies its own main

If you open src/main/scala/cosc250/firststeps/StepOne.scala you'll notice there is an object containing the code, and a 'main' method.

The first exercises ask you to run the object. To run the object, from the sbt interactive prompt, run the command run.

The next exercises ask you to run the tests. To run the tests, from the sbt interactive prompt, run the command test.

In this case, the test framework is the program that is run, and it runs the tests that are defined in src/test/scala/cosc250/firststeps/FirststepsSpec.scala. So, in this case, the testing framework is providing the main method.

Remember, though, that if there is a compile error in your code, even if it is in a different part of the code than you want to test right now, the code won't compile and the tests can't run.

The exercise

Open the project in your favourite IDE, and start an sbt shell. The exercises are in StepOne.scala and the comments provide additional instructions. Your task is to edit the code to make the tests pass.

firststeps's People

Contributors

wbillingsley avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.