Skip to content

Instantly share code, notes, and snippets.

@sentenza
Last active August 20, 2017 12:00
Show Gist options
  • Save sentenza/d1d1618ffd390b99a7e03c4988ab7a61 to your computer and use it in GitHub Desktop.
Save sentenza/d1d1618ffd390b99a7e03c4988ab7a61 to your computer and use it in GitHub Desktop.
A list of resources and tutorials to start working with Scala and do everything properly

scalalgorithms

A collection of algorithms written in Scala. The target is to make a pure excersize playground for testing data structures and algorithms.

Any external dependency should be possibly avoided.

Sorting

Algorithms to be implemented:

  • quickSort
  • countingSort
  • combSort
  • heapSort
  • mergeSort
  • shellSort
  • selectionSort
  • insertSort
  • gnomeSort
  • combinedBubbleSort
  • cocktailSort
  • bubbleSort
  • oddEvenSort
  • bubbleSortWithFlag

Each algorithm should be test with a range of element between 1 and INT_MAX.

Project scaffolding (doing it in the right way)

The best way to organize your codebase, using sbt, is to follow the same folder structure used by Maven:

src/
  main/
    resources/
       <files to include in main jar here>
    scala/
       <main Scala sources>
    java/
       <main Java sources>
  test/
    resources
       <files to include in test jar here>
    scala/
       <test Scala sources>
    java/
       <test Java sources>

What is sbt?

sbt (aka Simple Build Tool) is a general purpose build tool written in Scala for JVM developers. It took advantage of previous ideas developed by other similar tools like Maven, Gradle and Ant.

  1. Default project layouts
  2. Built-in tasks
  3. Plugin architecture
  4. Declarative Dependency management
  5. Code over Configuration: A DSL for build tool
  6. Interactive
  7. Scala REPL integration

In order to have a better understanding of the main tool you'll use programming with Scala you must read:

  1. SBT: The missing tutorial
  2. The sbt reference manual

Create a new project using sbt

At first, you've to create the new project folder (e.g. myproject). Then, put inside the new folder a file named build.sbt, which contains the build settings that sbt will use. to house the build script (how build.sbt defines settings). This file, at the very beginning, will look like the following:

name := "myproject"
version := "0.1.0"
scalaVersion := "2.12.2"

where := is a function defined in the sbt library. It is used to define a setting that overwrites any previous value without referring to other settings. In order to define the version of Scala that sbt will pick up it's possible to define a specific scalaVersion, but before doing so it's better to verify your environment configuration and the current version of your Scala compiler (scala -version).

If the Scala version is not specified, the version sbt was built against is used. It is recommended to explicitly specify the version of Scala.
Please note that because compile is a dependency of run, you don’t have to run compile before each run; just type sbt run. @see sbt documentation

What's more, in the interactive mode sbt has a list of useful commands that guide you in the day to day work like help and tasks. One of the most useful things of sbt is the automatic and continous execution of a task fired by the changes saved to a source file you're editing. To trigger this kind of behaviour a task must be prepended with ~. So, to execute all the tests that either failed during the previous execution or whose transitive dependencies changed the task is ~testQuick, to stop its execution just hit Enter.

Add ScalaTest

Starting from sbt 0.10 it's possible to add the ScalaTest framework to a sbt project adding the following line to the build.sbt:

libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test"

Start simple using the hello-world template

To start in the fastest way possible using sbt there is a hello-world template (the full tutorial) that can be imported just using sbt itself:

$ sbt new scala/hello-world.g8

Resources

IDE and its configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment