Use vals not vars

Is Object mutable?

- if the effect of your methods on the object depend on the history of the object -- what happened to the object before - then the object is mutable.


    object HelloWorld {
        // Casimirs second scala program
        def main( args: Array[Strings] ) { // note: array definition is whitespace sensitive (no ws)
            println( "Hello, Booty!" )
        }
    }

Scala: OO, Functional, Static

    -> subclasses and mix-ins
    -> anon and higher-order functions
    -> function nesting and currying

    Compiled with scalac
    Executed with scala


Syntax
    * Case Sensitive
    * ClassNamesCamelCase
    * methodNamesLowCamelCase
    * filename same as object name
    * identifiers
    * keywords (including $) cannot be used as identifiers
    * Operator identifiers ++, Mixed identifiers _+m , Literal identifiers ``
    * multiline comment /** **/, single line comment //
    * statements terminated by \n or ; -char
    * package-declaration on first line: package com.liftcode.stuff
    * import scala.xml._
    * Java datatypes
    * literals

Variables and values

var myVar : String = "Foo"
val aVal : String = "Bar" // immutable
val bVal : Int;
var nVar = 10; // type inference
val uVal = "Waa"; // type inference

    * private, protected, public
    * access mod scope: private[packagename]

Operators 

    * precedence

Loops, Conditionals

Functions

    def functionName ([params]) : [return type] = {
        // function body
        return [expr]
    }

    * function that does not return anything has return-type Unit ( == void in java)
    * function call:
    
    functionName( param list )
    // or
    instance.functionName( param list )


Collections

    * Lists, Sets, Maps, Tuples, Options, Iterators


Classes

    class Point(xc: Int, yc: Int) {
        var x: Int = xc
        var y: Int = yc

        def move(dx: Int, dy: Int) {
            x = x + dx
            y = y + dy
            println ("Point x loc: " + x);
            println ("Point y loc: " + y);
        }
    }

    object Test {
        def main(args: Array[String]) {
            val pt = new Point(1,2);
            pt.move(33, 22);
        }
    }

    * override (param/method sig)
    * extends (class)
    * singleton objects with keyword -object


Traits - mixins

    trait Equal {
        def isEqual(x: Any) : Boolean
        def isNotEqual(x: Any) : Boolean = !isEqual(x)
    }

    class Point with Equal {
        // logic
    }


Pattern matching

    def matchTest(x: Int): String = x match {
        case 1 => "yks"
        case 2 => "kaks"
    }

    * regexes live in scala.util.matching.Regex -package


Exceptions

Extractors

    * unapply
    * apply

File I/O

    * java.io.File
    * val writer = new PrintWriter; writer.write(_str);

    * scala.io.Source
    * Source.fromFile("wat").foreach { print }

Sites

http://www.scala-lang.org/
http://www.scala-lang.org/api/current/index.html
http://www.tutorialspoint.com/scala/scala_quick_guide.htm


myMap.get(myKey) map (theValue => /* code to transform theValue */)

CSV file parsing
map

util for money, time
* Decimal rounding dollar to cent, time to 2 decimals
* hours between 18-06 +1.15/h
* overtime scala.collection.mutable.HashMap[0->1.25,2->1.5,4->2]



Output
"wages %m/%y"
"%ID, %NN, %SAL"