Try
Functions
val add = (x : Int, y : Int) => {
x + y
}
Methods
def add(x : Int, y : Int) = {
x + y
}
Declaring classes
class Fraction(numerator : Int, denumerator : Int) {
override def toString() : String = {
numerator.toString() + "/" +denumerator.toString()
}
}
val oneHalf = new Fraction(1, 2)
oneHalf.toString()
Objects
object Calculator {
def plus(a : Int, b : Int) = a + b
}
Calculator.plus(3, 4)
Simple is often erroneously mistaken for easy.
- "Easy" means "to be at hand", "to be approachable".
- "Simple" is the opposite of "complex" which means "being intertwined", "being tied together".
Rich Hickey (from the talk Simple Made Easy
val res = if (a == 3) "abc" else "cde"
val someComputation = {
val a = 3
a + 5
}
val newSeq = for (i <- 0 until 10) yield i + 1
res: String = "cde" someComputation: Int = 8 newSeq: IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
Example:
1 + 3
+
is methodCreate a class Complex for representing complex numbers
case class Complex(re : Double, imag : Double)
Implement a method called +
to add two complex numbers
Try out the method:
.
to call it?Implement a method called #*--!
// type your solution here
def foo() : Unit = {
import collection.immutable.List
case class KeyValue(key : String, value : Int)
val list = List(KeyValue("A", 3), KeyValue("B", 2))
def keyIsA(kv : KeyValue) : Boolean = { kv.key == "A" }
list.count(keyIsA)
}
defined function foo
val people = Seq("bob martin", "john doe", "william tell")
people: Seq[String] = List("bob martin", "john doe", "william tell")
people.map(name => name.toUpperCase)
res19: Seq[String] = List("BOB MARTIN", "JOHN DOE", "WILLIAM TELL")
people.filter(name => name.startsWith("b"))
res20: Seq[String] = List("bob martin")
people.flatMap(name => name.split(" "))
res22: Seq[String] = List("bob", "martin", "john", "doe", "william", "tell")
val t : Tuple2[Int, String] = (1, "abc")
t: (Int, String) = (1, "abc")
val a = Seq(1, 2, 3, 4)
val b = Seq("a", "b", "c", "d")
val zippedSeq : Seq[(Int, String)] = a.zip(b)
println("zipped list: " +zippedSeq)
val (aUnzipped, bUnzipped) : (Seq[Int], Seq[String]) = zippedSeq.unzip
zipped list: List((1,a), (2,b), (3,c), (4,d))
a: Seq[Int] = List(1, 2, 3, 4) b: Seq[String] = List("a", "b", "c", "d") zippedSeq: Seq[(Int, String)] = List((1, "a"), (2, "b"), (3, "c"), (4, "d")) aUnzipped: Seq[Int] = List(1, 2, 3, 4) bUnzipped: Seq[String] = List("a", "b", "c", "d")
1, 2, 3, 2, 3, 4, 3, 4, 5, ...
Scala has also for loops
for (i <- 0 until 10) {
print(i + " ")
}
val evenNumbers = for (i <- 0 until 10) yield {
i * 2
}
For loops are only syntactic sugar
The two expressions are the same:
(0 until 10).map(i => i * 2)
for (i <- 0 until 10) yield {
i * 2
}
For loops are only syntactic sugar
The two expressions are the same:
(0 until 10).filter(i => i % 2 == 0)
for (i <- 0 until 10 if i % 2 == 0) yield {
i
}
For loops are only syntactic sugar
The two expressions are the same:
(0 until 10).flatMap(i =>(i until i + 2))
for (i <- (0 until 10;
iSeq <- i until i + 2) yield iSeq
For loops are only syntactic sugar
Makes complicated expressions look simple
for (i <- 0 until 10;
j <- 0 until 10;
if (i + j) == 7) yield (i , j)