In [1]:
object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}
In [2]:
HelloWorld.main(Array())
In [ ]:
In [3]:
val a = 5
val b = 6
In [4]:
a*b
In [4]:
a = 7
In [5]:
var v = 2
In [6]:
v = 3
In [7]:
println(v)
In [8]:
val c: Long = 1000
In [9]:
c*c*c*c
In [ ]:
In [10]:
// You can use Java libraries:
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)
In [56]:
// Methods taking one argument can be used with an infix syntax
df.format(now)
df format now
In [133]:
// Everything is an object (Java distinguishes between primitive types and reference types)
1+2
1.+(2) // And you can name a method +
In [57]:
class Foo {
// no static methods here
}
object Foo { // companion object
// here come the methods that would be static in Java
}
In [15]:
List(1,2,3)
List.apply(1,2,3)
In [16]:
val l = List(3,4,5)
In [17]:
List[Long](3,4,5)
In [18]:
l map (2*_)
In [19]:
l.map(n => 2*n)
In [20]:
val l2 = 1 to 10
In [21]:
l2.filter(_ % 2 == 0)
In [22]:
l2.sum
In [58]:
l2.reduceLeft(_+_)
In [24]:
l2.reduceLeft((a,b) => a + b)
In [64]:
(((((((((1+2)+3)+4)+5)+6)+7)+8)+9)+10)
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
In [103]:
//l2.map(_.toString).reduceLeft("("+_+"+"+_+")")
In [75]:
l2.foldLeft(0)(_+_)
In [77]:
l2.foldLeft(0)(_*_)
In [92]:
def factorial(n: Int): Int = {
(1 to n).reduceLeft(_*_)
}
In [93]:
factorial(3)
In [94]:
factorial(10)
In [95]:
(1 to 18).map(factorial)
In [84]:
1 to 18 map factorial
In [96]:
def bigFactorial(n: Int): BigInt = {
(BigInt(1) to n).reduceLeft(_*_)
}
In [91]:
1 to 18 map bigFactorial
In [85]:
println(bigFactorial(1000))
In [108]:
val myFunc = () => println("Hello!")
In [114]:
myFunc()
In [109]:
def execute3times(f: () => Unit): Unit = {
f()
f()
f()
}
In [110]:
execute3times(myFunc)
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)
In [121]:
def myCompose(f1: Int => Int, f2: Int => Int): Int => Int = {
(x: Int) => f1(f2(x))
}
In [122]:
l map (myCompose(add2, add3))
In [132]:
def myComposeGeneric[A,B,C](f1: B => C, f2: A => B) = (x: A) => f1(f2(x))
In [131]:
l map (myComposeGeneric(add2, add3))
In [36]:
// Tuples
val t = (3,4)
In [37]:
t._1
In [38]:
(6, "aaa")
In [39]:
val t2 = (7, (8, 9), 3)
In [40]:
t2._2
In [41]:
t2._2._1
In [42]:
5L
5L.asInstanceOf[Int]
In [97]:
def factorial2(n: Int): Int = {
if (n == 1) {
1
} else {
n * factorial2(n - 1)
}
}
In [44]:
factorial2(5)
In [45]:
def factorial3(n: Int): Int = {
var i = 1
var p = 1
while (i <= n) {
p = p * i
i = i + 1
}
p
}
In [46]:
factorial3(5)
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)
}
In [48]:
factorial4(5)
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)
In [50]:
val p = new Person("John","Smith")
In [51]:
case class Person2(firstName: String, lastName: String)
In [99]:
val p2 = Person2("John","Smith")
In [53]:
val p3 = Person2("John","Smith")
p2 == p3
In [54]:
def greet(p: Person2) = {
p match {
case Person2(fn, ln) => s"Hello $fn $ln!"
}
}
In [55]:
greet(p2)
In [ ]: