In [1]:
object HelloWorld {
	def main(args: Array[String]) {
		println("Hello, world!")
	}
}


defined object HelloWorld

In [2]:
HelloWorld.main(Array())


Hello, world!


In [ ]:


In [3]:
val a = 5
val b = 6


a: Int = 5
b: Int = 6

In [4]:
a*b


res3: Int = 30

In [4]:
a = 7


Main.scala:25: reassignment to val
a = 7
  ^

In [5]:
var v = 2


v: Int = 2

In [6]:
v = 3




In [7]:
println(v)


3


In [8]:
val c: Long = 1000


c: Long = 1000L

In [9]:
c*c*c*c


res8: Long = 1000000000000L

In [ ]:


In [10]:
// You can use Java libraries:
import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._


import java.util.{Date, Locale}
import java.text.DateFormat
import java.text.DateFormat._

In [11]:
// WARNING: Import Flink Scala API like this:
// import org.apache.flink.api.scala._




In [12]:
val now = new Date
val df = getDateInstance(LONG, Locale.GERMANY)
df.format(now)


now: Date = Fri Oct 14 17:06:58 CEST 2016
df: DateFormat = java.text.SimpleDateFormat@919b7eca
res11_2: String = "14. Oktober 2016"

In [56]:
// Methods taking one argument can be used with an infix syntax
df.format(now)
df format now


res55_0: String = "14. Oktober 2016"
res55_1: String = "14. Oktober 2016"

In [133]:
// Everything is an object (Java distinguishes between primitive types and reference types)
1+2
1.+(2)  // And you can name a method +


res132_0: Int = 3
res132_1: Int = 3

In [57]:
class Foo {
    // no static methods here
}
object Foo { // companion object
    // here come the methods that would be static in Java
}


defined class Foo
defined object Foo

In [15]:
List(1,2,3)
List.apply(1,2,3)


res14_0: List[Int] = List(1, 2, 3)
res14_1: List[Int] = List(1, 2, 3)

In [16]:
val l = List(3,4,5)


l: List[Int] = List(3, 4, 5)

In [17]:
List[Long](3,4,5)


res16: List[Long] = List(3L, 4L, 5L)

Higher-order functions


In [18]:
l map (2*_)


res17: List[Int] = List(6, 8, 10)

In [19]:
l.map(n => 2*n)


res18: List[Int] = List(6, 8, 10)

In [20]:
val l2 = 1 to 10


l2: collection.immutable.Range.Inclusive = Range(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

In [21]:
l2.filter(_ % 2 == 0)


res20: collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10)

In [22]:
l2.sum


res21: Int = 55

In [58]:
l2.reduceLeft(_+_)


res57: Int = 55

In [24]:
l2.reduceLeft((a,b) => a + b)


res23: Int = 55

In [64]:
(((((((((1+2)+3)+4)+5)+6)+7)+8)+9)+10)


res63: Int = 55

In [26]:
val l3 = List(5,3,7,2)
l3.reduceLeft((a,b) => if (a > b) a else b)
l3.reduceLeft(Math.max(_,_))
l3.max


l3: List[Int] = List(5, 3, 7, 2)
res25_1: Int = 7
res25_2: Int = 7
res25_3: Int = 7

In [103]:
//l2.map(_.toString).reduceLeft("("+_+"+"+_+")")




In [75]:
l2.foldLeft(0)(_+_)


res74: Int = 55

In [77]:
l2.foldLeft(0)(_*_)


res76: Int = 0

In [92]:
def factorial(n: Int): Int = {
    (1 to n).reduceLeft(_*_)
}


defined function factorial

In [93]:
factorial(3)


res92: Int = 6

In [94]:
factorial(10)


res93: Int = 3628800

In [95]:
(1 to 18).map(factorial)


res94: collection.immutable.IndexedSeq[Int] = Vector(
  1,
  2,
  6,
  24,
  120,
  720,
  5040,
  40320,
  362880,
  3628800,
  39916800,
  479001600,
  1932053504,
  1278945280,
  2004310016,
  2004189184,
  -288522240,
  -898433024
)

In [84]:
1 to 18 map factorial


res83: collection.immutable.IndexedSeq[Int] = Vector(
  1,
  2,
  6,
  24,
  120,
  720,
  5040,
  40320,
  362880,
  3628800,
  39916800,
  479001600,
  1932053504,
  1278945280,
  2004310016,
  2004189184,
  -288522240,
  -898433024
)

In [96]:
def bigFactorial(n: Int): BigInt = {
    (BigInt(1) to n).reduceLeft(_*_)
}


defined function bigFactorial

In [91]:
1 to 18 map bigFactorial


res90: collection.immutable.IndexedSeq[BigInt] = Vector(
  1,
  2,
  6,
  24,
  120,
  720,
  5040,
  40320,
  362880,
  3628800,
  39916800,
  479001600,
  6227020800,
  87178291200,
  1307674368000,
  20922789888000,
  355687428096000,
  6402373705728000
)

In [85]:
println(bigFactorial(1000))


402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000


In [108]:
val myFunc = () => println("Hello!")


myFunc: () => Unit = <function0>

In [114]:
myFunc()


Hello!


In [109]:
def execute3times(f: () => Unit): Unit = {
    f()
    f()
    f()
}


defined function execute3times

In [110]:
execute3times(myFunc)


Hello!
Hello!
Hello!


In [120]:
val l = List(1,2,3)
val add2 = (x: Int) => x + 2
val add3 = (x: Int) => x + 3
l map (add2 compose add3)


l: List[Int] = List(1, 2, 3)
add2: Int => Int = <function1>
add3: Int => Int = <function1>
res119_3: List[Int] = List(6, 7, 8)

In [121]:
def myCompose(f1: Int => Int, f2: Int => Int): Int => Int = {
    (x: Int) => f1(f2(x))
}


defined function myCompose

In [122]:
l map (myCompose(add2, add3))


res121: List[Int] = List(6, 7, 8)

In [132]:
def myComposeGeneric[A,B,C](f1: B => C, f2: A => B) = (x: A) => f1(f2(x))


defined function myComposeGeneric

In [131]:
l map (myComposeGeneric(add2, add3))


res130: List[Int] = List(6, 7, 8)

In [36]:
// Tuples
val t = (3,4)


t: (Int, Int) = (3, 4)

In [37]:
t._1


res36: Int = 3

In [38]:
(6, "aaa")


res37: (Int, String) = (6, "aaa")

In [39]:
val t2 = (7, (8, 9), 3)


t2: (Int, (Int, Int), Int) = (7, (8, 9), 3)

In [40]:
t2._2


res39: (Int, Int) = (8, 9)

In [41]:
t2._2._1


res40: Int = 8

In [42]:
5L
5L.asInstanceOf[Int]


res41_0: Long = 5L
res41_1: Int = 5

In [97]:
def factorial2(n: Int): Int = {
    if (n == 1) {
        1
    } else {
        n * factorial2(n - 1)
    }
}


defined function factorial2

In [44]:
factorial2(5)


res43: Int = 120

In [45]:
def factorial3(n: Int): Int = {
    var i = 1
    var p = 1
    while (i <= n) {
        p = p * i
        i = i + 1
    }
    p
}


defined function factorial3

In [46]:
factorial3(5)


res45: Int = 120

In [47]:
def factorial4(n: Int): Int = {
    def inner(i: Int, p: Int): Int = {
        if (i <= n) {
            inner(i + 1, p * i)
        } else {
            p
        }
    }
    inner(1,1)
}


defined function factorial4

In [48]:
factorial4(5)


res47: Int = 120

Case classes


In [98]:
/* Java:

public class Person {

	private final String firstName;
	private final String lastName;
    
	public Person(String firstName, String lastName){
		this.firstName = firstName;
		this.lastName = lastName;
	}
    
	public String getFirstName() {
		return this.firstName;
	}
    
	public String getLastName() {
		return this.lastName;
	}
}

*/

// Scala:
class Person(firstName: String, lastName: String)


defined class Person

In [50]:
val p = new Person("John","Smith")


p: Person = cmd48$$user$Person@ca19473

In [51]:
case class Person2(firstName: String, lastName: String)


defined class Person2

In [99]:
val p2 = Person2("John","Smith")


p2: Person2 = Person2("John", "Smith")

In [53]:
val p3 = Person2("John","Smith")
p2 == p3


p3: Person2 = Person2("John", "Smith")
res52_1: Boolean = true

In [54]:
def greet(p: Person2) = {
    p match {
        case Person2(fn, ln) => s"Hello $fn $ln!"
    }
}


defined function greet

In [55]:
greet(p2)


res54: String = "Hello John Smith!"

In [ ]: