Skip to content

Instantly share code, notes, and snippets.

@sarveshseri
Last active August 26, 2016 13:19
Show Gist options
  • Save sarveshseri/787960aa1e557c6228de1372842bae8f to your computer and use it in GitHub Desktop.
Save sarveshseri/787960aa1e557c6228de1372842bae8f to your computer and use it in GitHub Desktop.
Hey sbt! Who are you?

Our new project

lets say we want to start a new scala project.

cd ~/Works
mkdir ScalaProject

So... the base directory for our project is ~/Work/ScalaProject.

The sbt convention for sources

Now, sbt will follow convention to try to find the following automatically,

  • Sources in the base directory
  • Sources in src/main/scala or src/main/java
  • Tests in src/test/scala or src/test/java
  • Data files in src/main/resources or src/test/resources
  • jars in lib

Build Definition

build.sbt located in base directory

Note :: The blank line after each statement in build.sbt is important.

name := "ScalaProject"

version := "1.0"

scalaVersion := "2.11.8"

Set sbt version

We can fix sbt for our project in ScalaProject/project/build.properties file.

sbt.version=0.13.12

Lets add some code

Create src/main/scala/Demo.scala

object Demo {
  def main(args: Array[String]) {
    println("")
    println("Hi,")
    println("I am nothing but a Scala+Sbt Demo")
    println("")
  }
}

Lets try running this

cd ~/Work/ScalaProject
# invoke sbt compiler; let it download things; will take some time
sbt compile
# after compiling run the code
sbt run

this will print something like this,

[info] Set current project to ScalaProject (in build file:/Users/xxxx/Works/ScalaProject/)
[info] Running Demo 

Hi,
I am nothing but a Scala+Sbt Demo

[success] Total time: 1 s, completed 26 Aug, 2016 6:25:28 PM

Now, lets try something real

Create a file src/main/scala/Utils.scala

Add few functions to our Utils

object Utils {

  // A naive function for fibonacci numbers
  def fibonacciSimple(n: Int): Int = {
    n match {
      case 0 => 1
      case 1 => 1
      case i => fibonacciSimple(n-1) + fibonacciSimple(n-2)
    }
  }

  // tail recursion for better performence
  def fibonacciTail(n: Int): Int = {
    def _fibonacci(n: Int, m: Int, m1: Int, m2: Int): Int = {
      if (n == m) m1 + m2
      else _fibonacci(n, m + 1, m1 + m2, m1)
    }

    n match {
      case 0 => 1
      case 1 => 1
      case i => _fibonacci(i, 2, 1, 1)
    }
  }
  
}

The above Scala code is equivalent to following JavaScript code,

var Utils = {

  // A naive function for fibonacci numbers
  fibonacciSimple = function(n) {
    if (n == 0) return 1;
    else if (n == 1) return 1;
    else return fibonacciSimple(n-1) + fibonacciSimple(n-2);
  },

  // tail recursion for better performence
  fibonacciTail: function(n) {
    function _fibonacci(n, m, m1, m2) {
      if (n == m) return m1 + m2
      else return _fibonacci(n, m + 1, m1 + m2, m1)
    }

    if (n == 0) return 1;
    else if (n == 1) return 1;
    else return _fibonacci(i, 2, 1, 1);
  }
  
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment