- Jobb i par
- Diskuter i par/felleskap
- Spør om hjelp om dere sitter fast
- Hvem er du ?
- Hva har du lyst til å lære ?
"hello" // string
true false // boolean
5 // int
5.5 // double
5f // float
1e30f // float exponential
1.0e-100 // exponential double
0xA // 10 hex int
012 // 10 octal int
1000L // long
'a' // character
'\u0041' // unicode character
"""a multi
line "string"
""" // multiline escaped string
<hello/> // xml
'alright // symbol
var foo = 5
foo = 6
// eksplisitt typet
var foo:Int = 5
val x = "Hello"
// eksplisitt typet
val x:String = "Hello"
def foo:Int = {
return 5
}
def foo:Int = {
5
}
// siste expression returneres alltid
def foo = {
5
}
// return type inference
def foo = 5
// single expression
def something(i:Int):Unit = {
println("Do " + i)
}
def something(i:Int) {
println("Do " + i)
}
// returnerer alltid Unit (ingen =)
def printSomething() = {
println("something")
}
// () indikerer sideeffekt som konvensjon
val nesting = {
def plus(a:Int, b:Int) = a + b
val x = {
val y = 5 - 3
y - 2
}
val z = plus(x, 10)
z / 2
}
lazy val person = {
println("fra DB")
DB.findPersonByPk(5)
}
println("etter")
person.name
// etter
// fra DB
val a = new String("Hello")
val b = new String("Hello")
a == b // true (java equals)
a eq b // false (java ==)
val msg = "World"
s"Hello ${msg}"
class Person {
private final String name;
private Integer age;
public Person(String name, Integer age){
this.name = name;
this.age = age;
}
public String getName(){
return name;
}
public Integer getAge(){
return age;
}
public void setAge(Integer age){
this.age = age;
}
public static Person create30(String name){
return new Person(name, 30);
}
@Override
public String toString(){
return "["+name+","+age+"]";
}
}
object Person {
def create30(name:String) = new Person(name, 30)
}
class Person(val name: String, var age: Int){
override def toString = s"[$name,$age]"
}
class Person(private val _name: String,
private var _age: Int) {
def name = _name
def age = _age
def age_=(a:Int){
_age = a
}
}
val person = Person.create30("foo")
person.name // foo
person.age = 29
person.age // 29
val ab = (5, "Hello")
val ab = 5 -> "Hello"
val abc = (5.5, "World", List(1, 2, 3))
ab._1 // 5
ab._2 // "Hello"
abc._3 // List(1, 2, 3)
val list = List(1, 2, 3)
val map = Map(1 -> "a", 2 -> "3")
val set = Set("a", "b", "c")
val vector = Vector("a", "b", "c")
val array = Array(1, 2, 3)
val range = 1 to 10
val list = List(1, 2, 3)
val cons = 1 :: 2 :: 3 :: Nil
val map = Map(1 -> "Hello")
val helloWorld = map + (2 -> "World")
val world = helloWorld - 1
val vector = Vector(1, 2)
val appended = vector :+ 3
val prepended = 1 +: appended
case class Person(name:String, age:Int)
val people = List(Person("kid-a", 10),
Person("kid-b", 12),
Person("mom", 42),
Person("dad", 43))
var ages = new ListBuffer[Int]
for(person <- people){
ages += person.age
}
val ages = people.map(person => person.age)
// List(10, 12, 42, 43)
var kids = new ListBuffer[Person]
for(person <- people){
if(person.age < 18)
kids += person
}
val kids = people.filter(person => person.age < 18)
// List(Person("kid-a", 10), Person("kid-b", 12))
case class Owner(pets:List[String])
val owners = List(Owner(List("Dog", "Cat")),
Owner(List("Fish")))
var pets = new ListBuffer[String]
for(owner <- owners){
pets ++= owner.pets
}
val pets = owners.flatMap(owner => owner.pets)
// List("Dog", "Cat", "Fish")
var kids = new ListBuffer[Person]
var adults = new ListBuffer[Person]
for(person <- people){
if(person.age < 18)
kids += person
else
adults += person
}
val (kids, adults) = people.partition(person => person.age < 18)
val (kids, adults) = people.partition(_.age < 18)
val first10 = list.take(10)
val dropped = list.drop(5)
case class Person(fornavn:String, etternavn:String)
val personer:List[Person] = ...
val familier:Map[String, List[Person]] =
personer.groupBy(_.etternavn)
val fornavn:Map[String, List[String]] =
familier.mapValues(personer => personer.map(_.fornavn))
val sortert:Seq[(String, List[String])] =
fornavn.toSeq.sortBy(_._1.size)
val list = List(1,2,3)
list(0) // of its indexes
val map = Map("A" -> 1, "B" -> 2)
map("A") // of its keys
val set = Set(1,2,3)
set(1) // contains
import java.util.HashMap
val map = new HashMap[Int, String]
map.put(0, null)
...
map.get(0) // null
map.get(1) // null
if(map.containsKey(0)){
return map.get(0)
}
val map = Map(1 -> "Hello", 2 -> "World")
map.get(1) // Some("Hello")
map.get(0) // None
map(1) // "Hello"
map(0) // java.util.NoSuchElementException
trait Option[+A] {
def get:A // eller java.util.NoSuchElementException
def getOrElse(a:A):A
def map(f:A => B):Option[B]
def flatMap(f:A => Option[B]):Option[B]
}
object None extends Option[Nothing]
case class Some[A](value:A) extends Option[A]
class Stuff {
/* can be null */
public String getIt(){ ... }
}
val stuff:Option[String] = Option(stuff.getIt)
https://github.com/arktekk/scala-kurs-oppgaver/tree/master/buzzword