scala> val a: Int = 5 a: Int = 5
scala> val a = 5 a: Int = 5
scala> def add(x:Int, y:Int) = x + y # add: (x: Int, y: Int) => Int scala> add(5, 3) res0: Int = 8
class User {
private String name;
private List orders;
public User() { orders = new ArrayList(); }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public List getOrders() { return orders; }
public void setOrders(List orders) { this.orders = orders; }
}
public class Order {
private int id;
private List products;
public Order() { products = new ArrayList(); }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public List getProducts() { return products; }
public void setProducts(List products) { this.products = products; }
}
public class Product {
private int id;
private String category;
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
}
case class User(name: String, orders: Seq[Order])
case class Order(id: Int, products: Seq[Product])
case class Product(id: Int, category: String)
val product = Product(1, "Fidget spinner")
val order = Order(1, Seq(product))
val user = User("Magnus", Seq(order))
# user: User = User(Magnus, List(
Order(1, List(
Product(1,Fidget spinner)))))
case class User(firstName: String, surname: String)
val user = new User("Java", "Forum")
# user: User = User("Java", "Forum")
val user = User.apply("Java", "Forum")
# user: User = User("Java", "Forum")
val user = User("Java", "Forum")
# user: User = User("Java", "Forum")
val user2 = user.copy(firstName = "Magnus")
# user2: User = User(Magnus,Forum)
case class User(firstName: String, surname: Option[String])
def matchUser(u: User) =
u match {
case User(fname, None) =>
println(s"Got first name: $fname but no surname"))
case User(_, Some(lName)) =>
println(s"Got lastname: $lName and ignored first name")
}
trait Person
case class Customer(vip: Boolean) extends Person
case class Employee(jobTitle: String) extends Person
case object Nerd extends Person
case object User extends Person
def matchUser(p: Person) = p match {
case Customer(true) => println("Customer is VIP"))
case Customer(false) => println("Customer is not VIP"))
case Nerd => println(s"Great, we got a nerd!")
case _ => println("all other cases")
}
Seq(1, "JavaForum").map {
case i: Int => i
case s: String => s
}
# Seq[Any] = List(1, JavaForum)
Seq(1, "JavaForum", true).map {
case i: Int => i.toString
case s: String => s
}
# scala.MatchError: true (of class java.lang.Boolean)
Seq(1, "JavaForum", true).collect {
case i: Int => i.toString
case s: String => s
}
# Seq[String] = List(1, JavaForum)
users.flatMap(u => u.orders.flatMap(o => o.products)) # Seq[Product]
for { user <- users orders <- user.orders products <- orders.products } yield products # Seq[Product]
val seq = (1 to 10000)
seq.map(_ + 42)
seq.par.map(_ + 42)
val usdQuote = Future { connection.getCurrentValue(USD) }
val sekQuote = Future { connection.getCurrentValue(SEK) }
val purchase = for {
usd <- usdQuote
sek <- sekQuote
if isProfitable(usd, sek)
} yield connection.buy(amount, sek)
purchase foreach { _ =>
println("Purchased " + amount + " SEK")
}