Scala refresher

Posted by Andrey Marushkevych on May 26, 2014

Scala is one of those languages (for me at least) that need constant use in order to stay in my head. I completed “Functional programming in Scala” course more then year ago, doing pretty advance stuff in Scala, but now going back to it I found lots of gaps in my Scala part of the brain.

There are lots of resources on the net, but for me this book is the best way to refresh my Scala: Seven Languages in Seven Weeks

Official Scala cheatsheet is very complete and concise.

Method Invocation guide is very useful too.

Here is great post on partial functions and pattern matching.

Here I’ll document the most helpful reminders…

Pattern Matching Anonymous Functions

val b = List(1, 2)
b map {
    case 1 => "one"
    case 2 => "two"
}

Anonymous Functions as blocks

def myMethod(x: String => Unit) = x("foo")

myMethod { s =>
  println("printing " + s)
} 

// If there are more argument need  to use parents
def myMethod(s: String, x: String => Unit) = x(s)

myMethod("foo", s => {
  println("printing " + s)
}) 
	

// An alternative is to define myMethod as a curried function
def myMethod(s: String)( x: String => Unit) = x(s)

myMethod("foo") { s =>
  println("printing " + s)
}

ranges

val inclusiveRange = 0 to 9
// is same as
val range = 0 until 10

for loop using range: for(i <- 0 until args.length)

tuples

val person = ("Elvis", "Presley")
person._1
person._2

val (first, last) =  ("Elvis", "Presley")

classes

class Person(firstName: String, lastName: String)
val gump = new Person("Forrest", "Gump")

// Constructors
class Person(firstName: String) {
    println("Outer constructor")
    def this(firstName: String, lastName: String) {
        this(firstName)
        println("Inner constructor") 
    }
    def talk() = println("Hi") 
}

// static elements - use singleton objects
object Person {
    def staticMethod = println("i'm static")
}

Person.staticMethod()

companion object factory

class Person(firstName: String, lastName: String)

object Person {
    def apply(fullName: String){
        new Person(parseFirstName(fullName), parseLastName(fullName)
    }
}

val person = Person("Elvis Presley");

Inheritance

class Person(val name: String) {
    def talk(message: String) = println(name + " says " + message) 
    def id(): String = name
}

class Employee(override val name: String, val number: Int) extends Person(name) {
    override def talk(message: String) {
        println(name + " with number " + number + " says " + message)
    }
    override def id():String = number.toString 
}

traits

class Person(val name:String)
trait Nice {
    def greet() = println("Howdily doodily.")
}
class Character(override val name:String) extends Person(name) with Nice
val flanders = new Character("Ned") 
flanders.greet

// or extend trait directly:
class Person(val name: String) extends Nice


blog comments powered by Disqus