SlideShare uma empresa Scribd logo
1 de 76
Baixar para ler offline
‫نویسی‬ ‫برنامه‬ ‫اصول‬ ‫کارگاه‬
Hadoop & Spark(Scala)
‫کننده‬ ‫ارائه‬:‫امین‬ ‫دکتر‬‫نظارات‬
‫نور‬ ‫پیام‬ ‫علمی‬ ‫هیئت‬ ‫عضو‬
aminnezarat@gmail.com
www.hpclab.ir ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Scala programming
language
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Scala on JVM
scalac compiles Scala to Java bytecode
(regular .class files)
Any Java class can be used from Scala
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Origin
Started at 2001 by Martin Odersky at EPFL
Lausanne, Switzerland
Scala 2.0 released in 2006
Current version 2.7
Twitter backend runs on Scala
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Scala properties
Object oriented
Statically typed
Functional & imperative
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Static typing
Type checking done at compile time
Type associated with variable, not value
Better tools possible
More verbose code compared to dynamic
language
Can’t add methods to class at runtime
No duck typing – really?
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Functional programming
Functions are first class citizens
Immutability
Tuples
Currying
Recursion
Monads
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Introduction
Demo of Scala interpreter‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Variables & values, type
inference
var msg = "Hello“// msg is mutable
msg += " world"
msg = 5;// compiler error
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Variables & values, type
inference
val msg = "Hello world“// msg is immutable
msg += " world“// compiler error
val n : Int = 3// explicit type declaration
var n2 : Int = 3
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Immutability
Why?
Immutable objects are automatically thread-safe
(you don’t have to worry about object being
changed by another thread)
Compiler can reason better about immutable
values -> optimization
Steve Jenson from Twitter: “Start with
immutability, then use mutability where you find
appropriate.”
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Calling Java from Scala
Any Java class can be used seamlessly
import java.io._
val url = new URL("http://www.scala-lang.org")
demo
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Methods
def max(x : Int, y : Int) = if (x > y) x else y
// equivalent:
def neg(x : Int) : Int = -x
def neg(x : Int) : Int = { return –x; }
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Types
Int, Double, String, Char, Byte, BigInt, …
wrappers around Java types
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Lists
Lists are immutable (= contents cannot be
changed)
List[String] contains Strings
val lst = List("b", "c", "d")
lst.head // “b”
lst.tail // List(“c”, “d”)
val lst2 = "a" :: lst // cons operator
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Lists
Nil = synonym for empty list
val l = 1 :: 2 :: 3 :: Nil
List concatenation
val l2 = List(1, 2, 3) ::: List(4, 5)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Foreach
val list3 = List("mff", "cuni", "cz")
Following 3 calls are equivalent
list.foreach((s : String) => println(s))
list.foreach(s => println(s))
list.foreach(println)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
For comprehensions
for (s <- list)
println(s)
for (s <- list if s.length() == 4)
println(s)
for just calls foreach
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Arrays
Lists are immutable, arrays are mutable
val a = Array("Java", "rocks")
a(0) = "Scala";
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Covariance
Lists are covariant, arrays are invariant
// compiler error
val array : Array[Any] = Array(1, 2, 3);
// ok
val list : List[Any] = List(1, 2, 3);
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Arrays
val greets = new Array[String](2)
greets(0) = "Hello"
greets(1) = "world!n"
for (i <- 0 to 1)
print(greets(i))
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Arrays are no special type
greets(i)===greets.apply(i)
greets(i) = "Hi"=== greets.update(i,
"Hi")
Any class that defines apply / update can be
used like this‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Every operation is a method
call
“to” is not a keyword
for (i <- 0 to 2) print(greets(i))
0 to 2 === 0.to(2)
x – 1 === x.-(1)
map containsKey ‘a’ ===
map.containsKey(‘a’)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Associativity
If method name ends with colon, the method
is invoked on the right operand
val list = List("b", "c")
"a" :: list === list.::("a")
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Performance
Scala treats everything as objects
no primitive types, no arrays
So this comes with a cost, right?
Usually not, the scalac compiler uses Java
primitive types and arrays where possible
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Anonymous functions
val l = new List("mff", "cuni", "cz")
l.filter(s => s.length == 4)
val l = List[Person](new Person(…), …)
l.sort((p1, p2) => p1.lastName < p2.lastName)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Currying
Function with only some arguments specified
=
function expecting the rest of the arguments
Common concept in functional languages
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Currying
// Does n divide m?
def nDividesM(m : Int)(n : Int) = (n % m == 0)
// Currying,
// isEven is of type (Int) => Boolean
val isEven = nDividesM(2)_
println(isEven(4))
println(isEven(5))
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Tuples
Sequence of elements with different types
(10, List('c', 'm'), "cache");
type of this expression is Tuple3[Int, List[Char],
String]
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Tuples
def divMod(x : Int, y : Int) = (x / y, x % y)
val dm = divMod(10, 3) // Tuple2[Int, Int]
dm._1 // 3
dm._2 // 1
val (d, m) = divMod(10, 3);
println(d + " " + m);// 3 1
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Pattern matching
Like switch statement
But much more powerful
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Pattern matching
def flatten(list: List[Any]) : List[Any] =
list match {
case (x: List[Any]) :: xs =>
flatten(x) ::: flatten(xs)
case x :: xs => x :: flatten(xs)
case Nil => Nil
}
val nested = List(1, List(2, 3), 4);
val flat = flatten(nested); // List(1, 2, 3, 4)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Classes
/** A Person class.
* Constructor parameters become
* public members of the class.*/
class Person(val name: String, var age: Int) {
if (age < 0) {
throw …
}
}
var p = new Person(“Peter", 21);
p.age += 1;
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Objects
Scala’s way for “statics”
not quite – see next slide
(in Scala, there is no static keyword)
“Companion object” for a class
= object with same name as the class
demo
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Objects
// we declare singleton object "Person"
// this is a companion object of class Person
object Person {
def defaultName() = "nobody"
}
class Person(val name: String, var age: Int) {
def getName() : String = name
}
// surprise, Person is really an object
val singleton : Person = Person;
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Case classes
Implicitely override toString, equals,
hashCode
take object’s structure into account
abstract class Expr
case class Number(n: Int) extends Expr
case class Sum(e1: Expr, e2: Expr) extends Expr
// true thanks to overriden equals
Sum(Number(1), Number(2)) ==
Sum(Number(1), Number(2))
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Case classes
Needed if we want to pattern match on class
hiearchies
def eval(e: Expr): Int = e match {
case Number(n) => n
case Sum(l, r) => eval(l) + eval(r)
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Exceptions
object Main {
def main(args: Array[String]) {
try {
val elems = args.map(Integer.parseInt)
println("Sum is: " + elems.foldRight(0) (_ + _)) }
catch {
case e: NumberFormatException =>
println("Usage: scala Main <n1> <n2> ... ")
}
}
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Traits
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Traits
Like Java interfaces
But can contain implementations and fields
trait Pet {
var age: Int = 0
def greet(): String = {
return "Hi"
}
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Extending traits
class Dog extends Pet {
override def greet() = "Woof"
}
trait ExclamatoryGreeter extends Pet {
override def greet() = super.greet() + " !"
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Traits - mixins
Traits can be “mixed in” at instation time
trait ExclamatoryGreeter extends Pet {
override def greet() = super.greet() + " !"
}
val pet = new Dog with ExclamatoryGreeter
println(pet.greet())// Woof !
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Traits – common use
trait Ordered[A] {
def compare(that: A): Int// abstract method
def < (that: A): Boolean = (this compare that) < 0
def > (that: A): Boolean = (this compare that) > 0
}
class Health(val value : Int) extends Ordered[Health]
{
override def compare(other : Health) = {
this.value - other.value; }
def isCritical() = …
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Maps and Sets
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Map – simple example
import scala.collection._
val cache = new mutable.HashMap[String,String];
cache += "foo" -> "bar";
val c = cache("foo");
The rest of Map and Set interface looks as you would
expect
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
ListBuffer
ListBuffer[T] is a mutable List
Like Java’s ArrayList<T>
import scala.collection.mutable._
val list = new ListBuffer[String]
list += "Vicky"
list += "Christina"
val str = list(0)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Option
Like “Maybe” in Haskell
Example – 3 state Boolean
var sure : Option[Boolean] = Some(false);
sure = Some(true);
sure = None;
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors
Concurrency using threads is hard
Shared state – locks, race conditions, deadlocks
Solution – message passing + no shared
state
Inspired by Erlang language
Erlang used at Ericsson since 1987, open source
since 1998
Facebook chat backend runs on Erlang
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
What is an actor
Actor is an object that receives messages
Actor has a mailbox – queue of incoming
messages
Message send is by default asynchronous
Sending a message to an actor immediately
returns
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors – trivial example
We define messages
case object MsgPing
case object MsgPong
case object MsgStop
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors – trivial example
class Ping(count: Int, pong: Actor) extends Actor {
def act() {
var pingsSent = 0
println("Ping: sending ping " + pingsSent)
pong ! MsgPing; pingsSent += 1
while(true) {
receive {
case MsgPong =>
if (pingsSent < count) {
if (pingsSent % 1000 == 0)
println("Ping: sending ping " + pingsSent)
pong ! MsgPing; pingsSent += 1
} else {
println("Ping: sending stop")
pong ! MsgStop
exit()
}
}}}}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors – trivial example
class Pong extends Actor {
def act() {
var pongCount = 0
while(true) {
receive {
case MsgPing =>
if (pongCount % 1000 == 0)
println("Pong: replying " + pongCount)
sender ! MsgPong
pongCount += 1
case MsgStop =>
println("Pong: stop")
exit()
}
}
}
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors – trivial example
val pong = new Pong
val ping = new Ping(100000, pong)
ping.start
pong.start
// any following code here is not
blocked by the actors, each Actor
(Ping, Pong) runs in his own thread
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Actors – what else is
available?
actor ! message - asynchronous send
actor !? message- synchronous send
(awaits reply)
actor !! message - asynchronous, returs
future object
future object can be used later to get the result
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Creating “keywords”
From actors example, it seems that Scala
has built-in keywords like receive { } or !
Not true – actors are implemented as a
library
We already know that
pong ! MsgPing is equivalent to
pong.!(MsgPing) // ! is a method of Actor
class
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Creating “keywords”
Moreover, receive is just a method of Actor
class
Method arguments can be passed in curly
braces
Ability to create DSL-like languages
receive {
case MsgPong =>
…
}‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Creating keywords - lock in
Java
String x = "No"
l.lock();
try {
x = "Yes"
} finally {
l.unlock();
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Creating keywords - lock in
Scala
var x = "No"
lock(l) {
x = "Yes“
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Lock “keyword” implemetation
Lock “keyword” is really an ordinary method
// f is a function (piece of code) returning
// Unit (ie. void)
def lock(l : Lock)(f : => Unit) = {
l.lock();
try {
f// call f
} finally {
l.unlock();
}
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Parallelism
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Parallelism
What about parallelMap, parallelReduce etc.
?
Not present in Scala library yet 
Have to implement own versions
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Little more advanced
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
What exactly is the List?
List is an abstract class with 2 descendant
case classes:
Nil
::
What gets called for List(1, 2, 3) ?
object List {
// * means variable arguments
def apply[A](xs: A*): List[A] = xs.toList
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
scala.Seq
scala.Seq is the supertype that defines
methods like:
filter, fold, map, reduce, take, contains, …
List, Array, Maps… descend from Seq
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Yield, iterators
Syntax sugar for returning iterator object
Iterators allow to iterate over a sequence of
elements. They have hasNext() and next()
methods.
Lazy evaluation
when olderThan21 is called, the for loop is not
executed
def olderThan21(xs: Iterator[Person]): Iterator[String] =
{
for (p <- xs if p.age > 21) yield p.getName()
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Matching generic arguments?
Will this compile?
def genMatch(list: List[Any]) : String =
list match {
case (x: List[Int]) => "ints"
case (x: List[String]) => "strings"
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Matching generic arguments?
JVM has no runtime support for generics
(compiler uses erasure)
def genMatch(list: List[Any]) : String =
list match {
// warning: type argument is unchecked
case (x: List[Int]) => "ints"
// error: unreachable code
case (x: List[String]) => "strings"
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Adding methods to classes
Possible in dynamic languages (even at
runtime)
Possible using Extension methods in C#
(just syntax sugar for static methods)
How to do it in Scala?
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
“Adding methods” to classes
ScalaTest test framework
map should have value 7// legal scala code
We want to be able to call map.should
map does not have a “should” method
Solution – wrapper object
class Wrapper(wrappedObject : Any) {
def should() …
}
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
“Adding methods” to classes
class Wrapper(wrappedObject : Any) {
def added() { …}
}
Define implicit conversion method Any ->
Wrapper
implicit def wrap(o : Any) = new Wrapper(o)
object.added() compiles as
wrap(object).added()
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
“Adding methods” - demo
class CollectionWrapper[T](wrappedCollection : java.util.Collection[T]) {
def join(delim : String) : String = {
val iter = wrappedCollection.iterator();
val buffer = new StringBuffer(iter.next().toString());
while (iter.hasNext()) buffer.append(delim).append(iter.next().toString());
return buffer.toString();
}
}
implicit def wrapCollection[T](o : java.util.Collection[T]) = new
CollectionWrapper(o)
var javaList = new java.util.ArrayList[String]();
println(javaList.join("-"));// same as wrapCollection(javaList).join(“-“)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Structural types
{ val length : Int }
any object that has length field
{ def length() : Int }
any object that has length() method
Duck typing
Invoking methods on the object uses
reflection - slower
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
Traits – diamond inheritance?
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
XML
import scala.xml._
val df = java.text.DateFormat.getDateInstance()
val dateString = df.format(new java.util.Date())
def theDate(name: String) =
<dateMsg addressedTo={ name }>
Hello, { name }! Today is { dateString }
</dateMsg>;
println(theDate(“Amin Nezarat").toString())
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)
‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir -
aminnezarat@gmail.com)

Mais conteúdo relacionado

Mais procurados

High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak
 

Mais procurados (18)

JavaScript Arrays
JavaScript Arrays JavaScript Arrays
JavaScript Arrays
 
Meet scala
Meet scalaMeet scala
Meet scala
 
Joy of scala
Joy of scalaJoy of scala
Joy of scala
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Scala
ScalaScala
Scala
 
Scala Back to Basics: Type Classes
Scala Back to Basics: Type ClassesScala Back to Basics: Type Classes
Scala Back to Basics: Type Classes
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
ハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使うハイブリッド言語Scalaを使う
ハイブリッド言語Scalaを使う
 
ddd+scala
ddd+scaladdd+scala
ddd+scala
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
The Scala Programming Language
The Scala Programming LanguageThe Scala Programming Language
The Scala Programming Language
 
Scala collections
Scala collectionsScala collections
Scala collections
 
ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021ZIO Prelude - ZIO World 2021
ZIO Prelude - ZIO World 2021
 
A bit about Scala
A bit about ScalaA bit about Scala
A bit about Scala
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Meetup slides
Meetup slidesMeetup slides
Meetup slides
 
Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!Be Smart, Constrain Your Types to Free Your Brain!
Be Smart, Constrain Your Types to Free Your Brain!
 

Semelhante a Introduction to-scala

pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
Hiroshi Ono
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
Tomasz Wrobel
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
parag978978
 

Semelhante a Introduction to-scala (20)

Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdfpragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
pragmaticrealworldscalajfokus2009-1233251076441384-2.pdf
 
(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?(How) can we benefit from adopting scala?
(How) can we benefit from adopting scala?
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
 
Principles of functional progrmming in scala
Principles of functional progrmming in scalaPrinciples of functional progrmming in scala
Principles of functional progrmming in scala
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
 
Introducing scala
Introducing scalaIntroducing scala
Introducing scala
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
Scala Bootcamp 1
Scala Bootcamp 1Scala Bootcamp 1
Scala Bootcamp 1
 
R language introduction
R language introductionR language introduction
R language introduction
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Pragmatic Real-World Scala
Pragmatic Real-World ScalaPragmatic Real-World Scala
Pragmatic Real-World Scala
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
 
Scala taxonomy
Scala taxonomyScala taxonomy
Scala taxonomy
 

Mais de Hamid Jafarian

Mais de Hamid Jafarian (7)

PVM Presentaion For OCCC #76 Session
PVM Presentaion For OCCC #76 SessionPVM Presentaion For OCCC #76 Session
PVM Presentaion For OCCC #76 Session
 
PVM Cloud Open Source Foundation
PVM Cloud Open Source Foundation PVM Cloud Open Source Foundation
PVM Cloud Open Source Foundation
 
PDNSoft Entrepreneurship Experiences.
PDNSoft Entrepreneurship Experiences.PDNSoft Entrepreneurship Experiences.
PDNSoft Entrepreneurship Experiences.
 
Cloud Opportunities Threats
Cloud Opportunities ThreatsCloud Opportunities Threats
Cloud Opportunities Threats
 
PVM Presentaion - Afghanistan IT Minister Meeting
PVM Presentaion - Afghanistan IT Minister MeetingPVM Presentaion - Afghanistan IT Minister Meeting
PVM Presentaion - Afghanistan IT Minister Meeting
 
PVM presentation for DR.Vaezi
PVM presentation for DR.VaeziPVM presentation for DR.Vaezi
PVM presentation for DR.Vaezi
 
PDNSoft - FLOSS Collaboration Model - University/Industry
PDNSoft - FLOSS Collaboration Model - University/IndustryPDNSoft - FLOSS Collaboration Model - University/Industry
PDNSoft - FLOSS Collaboration Model - University/Industry
 

Último

Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
gajnagarg
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
only4webmaster01
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
gajnagarg
 
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
amitlee9823
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
gajnagarg
 
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
amitlee9823
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
amitlee9823
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
amitlee9823
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
amitlee9823
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
amitlee9823
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
amitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
amitlee9823
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
amitlee9823
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Último (20)

Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
Just Call Vip call girls Palakkad Escorts ☎️9352988975 Two shot with one girl...
 
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 9155563397 👗 Top Class Call Girl Service B...
 
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
Just Call Vip call girls Erode Escorts ☎️9352988975 Two shot with one girl (E...
 
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men  🔝Ongole🔝   Escorts S...
➥🔝 7737669865 🔝▻ Ongole Call-girls in Women Seeking Men 🔝Ongole🔝 Escorts S...
 
Anomaly detection and data imputation within time series
Anomaly detection and data imputation within time seriesAnomaly detection and data imputation within time series
Anomaly detection and data imputation within time series
 
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
Just Call Vip call girls Bellary Escorts ☎️9352988975 Two shot with one girl ...
 
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men  🔝Dindigul🔝   Escor...
➥🔝 7737669865 🔝▻ Dindigul Call-girls in Women Seeking Men 🔝Dindigul🔝 Escor...
 
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
Chintamani Call Girls: 🍓 7737669865 🍓 High Profile Model Escorts | Bangalore ...
 
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men  🔝Sambalpur🔝   Esc...
➥🔝 7737669865 🔝▻ Sambalpur Call-girls in Women Seeking Men 🔝Sambalpur🔝 Esc...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men  🔝Mathura🔝   Escorts...
➥🔝 7737669865 🔝▻ Mathura Call-girls in Women Seeking Men 🔝Mathura🔝 Escorts...
 
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Marol Naka Call On 9920725232 With Body to body massage...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
Escorts Service Kumaraswamy Layout ☎ 7737669865☎ Book Your One night Stand (B...
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
Call Girls Bannerghatta Road Just Call 👗 7737669865 👗 Top Class Call Girl Ser...
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 

Introduction to-scala

  • 1. ‫نویسی‬ ‫برنامه‬ ‫اصول‬ ‫کارگاه‬ Hadoop & Spark(Scala) ‫کننده‬ ‫ارائه‬:‫امین‬ ‫دکتر‬‫نظارات‬ ‫نور‬ ‫پیام‬ ‫علمی‬ ‫هیئت‬ ‫عضو‬ aminnezarat@gmail.com www.hpclab.ir ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 2. Scala programming language ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 3. Scala on JVM scalac compiles Scala to Java bytecode (regular .class files) Any Java class can be used from Scala ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 4. Origin Started at 2001 by Martin Odersky at EPFL Lausanne, Switzerland Scala 2.0 released in 2006 Current version 2.7 Twitter backend runs on Scala ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 5. Scala properties Object oriented Statically typed Functional & imperative ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 6. Static typing Type checking done at compile time Type associated with variable, not value Better tools possible More verbose code compared to dynamic language Can’t add methods to class at runtime No duck typing – really? ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 7. Functional programming Functions are first class citizens Immutability Tuples Currying Recursion Monads ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 8. Introduction Demo of Scala interpreter‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 9. Variables & values, type inference var msg = "Hello“// msg is mutable msg += " world" msg = 5;// compiler error ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 10. Variables & values, type inference val msg = "Hello world“// msg is immutable msg += " world“// compiler error val n : Int = 3// explicit type declaration var n2 : Int = 3 ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 11. Immutability Why? Immutable objects are automatically thread-safe (you don’t have to worry about object being changed by another thread) Compiler can reason better about immutable values -> optimization Steve Jenson from Twitter: “Start with immutability, then use mutability where you find appropriate.” ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 12. Calling Java from Scala Any Java class can be used seamlessly import java.io._ val url = new URL("http://www.scala-lang.org") demo ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 13. Methods def max(x : Int, y : Int) = if (x > y) x else y // equivalent: def neg(x : Int) : Int = -x def neg(x : Int) : Int = { return –x; } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 14. Types Int, Double, String, Char, Byte, BigInt, … wrappers around Java types ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 15. Lists Lists are immutable (= contents cannot be changed) List[String] contains Strings val lst = List("b", "c", "d") lst.head // “b” lst.tail // List(“c”, “d”) val lst2 = "a" :: lst // cons operator ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 16. Lists Nil = synonym for empty list val l = 1 :: 2 :: 3 :: Nil List concatenation val l2 = List(1, 2, 3) ::: List(4, 5) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 17. Foreach val list3 = List("mff", "cuni", "cz") Following 3 calls are equivalent list.foreach((s : String) => println(s)) list.foreach(s => println(s)) list.foreach(println) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 18. For comprehensions for (s <- list) println(s) for (s <- list if s.length() == 4) println(s) for just calls foreach ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 19. Arrays Lists are immutable, arrays are mutable val a = Array("Java", "rocks") a(0) = "Scala"; ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 20. Covariance Lists are covariant, arrays are invariant // compiler error val array : Array[Any] = Array(1, 2, 3); // ok val list : List[Any] = List(1, 2, 3); ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 21. Arrays val greets = new Array[String](2) greets(0) = "Hello" greets(1) = "world!n" for (i <- 0 to 1) print(greets(i)) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 22. Arrays are no special type greets(i)===greets.apply(i) greets(i) = "Hi"=== greets.update(i, "Hi") Any class that defines apply / update can be used like this‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 23. Every operation is a method call “to” is not a keyword for (i <- 0 to 2) print(greets(i)) 0 to 2 === 0.to(2) x – 1 === x.-(1) map containsKey ‘a’ === map.containsKey(‘a’) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 24. Associativity If method name ends with colon, the method is invoked on the right operand val list = List("b", "c") "a" :: list === list.::("a") ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 25. Performance Scala treats everything as objects no primitive types, no arrays So this comes with a cost, right? Usually not, the scalac compiler uses Java primitive types and arrays where possible ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 26. Anonymous functions val l = new List("mff", "cuni", "cz") l.filter(s => s.length == 4) val l = List[Person](new Person(…), …) l.sort((p1, p2) => p1.lastName < p2.lastName) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 27. Currying Function with only some arguments specified = function expecting the rest of the arguments Common concept in functional languages ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 28. Currying // Does n divide m? def nDividesM(m : Int)(n : Int) = (n % m == 0) // Currying, // isEven is of type (Int) => Boolean val isEven = nDividesM(2)_ println(isEven(4)) println(isEven(5)) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 29. Tuples Sequence of elements with different types (10, List('c', 'm'), "cache"); type of this expression is Tuple3[Int, List[Char], String] ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 30. Tuples def divMod(x : Int, y : Int) = (x / y, x % y) val dm = divMod(10, 3) // Tuple2[Int, Int] dm._1 // 3 dm._2 // 1 val (d, m) = divMod(10, 3); println(d + " " + m);// 3 1 ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 31. Pattern matching Like switch statement But much more powerful ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 32. Pattern matching def flatten(list: List[Any]) : List[Any] = list match { case (x: List[Any]) :: xs => flatten(x) ::: flatten(xs) case x :: xs => x :: flatten(xs) case Nil => Nil } val nested = List(1, List(2, 3), 4); val flat = flatten(nested); // List(1, 2, 3, 4) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 33. Classes /** A Person class. * Constructor parameters become * public members of the class.*/ class Person(val name: String, var age: Int) { if (age < 0) { throw … } } var p = new Person(“Peter", 21); p.age += 1; ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 34. Objects Scala’s way for “statics” not quite – see next slide (in Scala, there is no static keyword) “Companion object” for a class = object with same name as the class demo ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 35. Objects // we declare singleton object "Person" // this is a companion object of class Person object Person { def defaultName() = "nobody" } class Person(val name: String, var age: Int) { def getName() : String = name } // surprise, Person is really an object val singleton : Person = Person; ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 36. Case classes Implicitely override toString, equals, hashCode take object’s structure into account abstract class Expr case class Number(n: Int) extends Expr case class Sum(e1: Expr, e2: Expr) extends Expr // true thanks to overriden equals Sum(Number(1), Number(2)) == Sum(Number(1), Number(2)) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 37. Case classes Needed if we want to pattern match on class hiearchies def eval(e: Expr): Int = e match { case Number(n) => n case Sum(l, r) => eval(l) + eval(r) } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 38. Exceptions object Main { def main(args: Array[String]) { try { val elems = args.map(Integer.parseInt) println("Sum is: " + elems.foldRight(0) (_ + _)) } catch { case e: NumberFormatException => println("Usage: scala Main <n1> <n2> ... ") } } } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 39. Traits ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 40. Traits Like Java interfaces But can contain implementations and fields trait Pet { var age: Int = 0 def greet(): String = { return "Hi" } } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 41. Extending traits class Dog extends Pet { override def greet() = "Woof" } trait ExclamatoryGreeter extends Pet { override def greet() = super.greet() + " !" } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 42. Traits - mixins Traits can be “mixed in” at instation time trait ExclamatoryGreeter extends Pet { override def greet() = super.greet() + " !" } val pet = new Dog with ExclamatoryGreeter println(pet.greet())// Woof ! ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 43. Traits – common use trait Ordered[A] { def compare(that: A): Int// abstract method def < (that: A): Boolean = (this compare that) < 0 def > (that: A): Boolean = (this compare that) > 0 } class Health(val value : Int) extends Ordered[Health] { override def compare(other : Health) = { this.value - other.value; } def isCritical() = … } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 44. Maps and Sets ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 45. Map – simple example import scala.collection._ val cache = new mutable.HashMap[String,String]; cache += "foo" -> "bar"; val c = cache("foo"); The rest of Map and Set interface looks as you would expect ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 46. ListBuffer ListBuffer[T] is a mutable List Like Java’s ArrayList<T> import scala.collection.mutable._ val list = new ListBuffer[String] list += "Vicky" list += "Christina" val str = list(0) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 47. Option Like “Maybe” in Haskell Example – 3 state Boolean var sure : Option[Boolean] = Some(false); sure = Some(true); sure = None; ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 48. Actors ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 49. Actors Concurrency using threads is hard Shared state – locks, race conditions, deadlocks Solution – message passing + no shared state Inspired by Erlang language Erlang used at Ericsson since 1987, open source since 1998 Facebook chat backend runs on Erlang ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 50. What is an actor Actor is an object that receives messages Actor has a mailbox – queue of incoming messages Message send is by default asynchronous Sending a message to an actor immediately returns ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 51. Actors – trivial example We define messages case object MsgPing case object MsgPong case object MsgStop ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 52. Actors – trivial example class Ping(count: Int, pong: Actor) extends Actor { def act() { var pingsSent = 0 println("Ping: sending ping " + pingsSent) pong ! MsgPing; pingsSent += 1 while(true) { receive { case MsgPong => if (pingsSent < count) { if (pingsSent % 1000 == 0) println("Ping: sending ping " + pingsSent) pong ! MsgPing; pingsSent += 1 } else { println("Ping: sending stop") pong ! MsgStop exit() } }}}} ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 53. Actors – trivial example class Pong extends Actor { def act() { var pongCount = 0 while(true) { receive { case MsgPing => if (pongCount % 1000 == 0) println("Pong: replying " + pongCount) sender ! MsgPong pongCount += 1 case MsgStop => println("Pong: stop") exit() } } } } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 54. Actors – trivial example val pong = new Pong val ping = new Ping(100000, pong) ping.start pong.start // any following code here is not blocked by the actors, each Actor (Ping, Pong) runs in his own thread ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 55. Actors – what else is available? actor ! message - asynchronous send actor !? message- synchronous send (awaits reply) actor !! message - asynchronous, returs future object future object can be used later to get the result ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 56. Creating “keywords” From actors example, it seems that Scala has built-in keywords like receive { } or ! Not true – actors are implemented as a library We already know that pong ! MsgPing is equivalent to pong.!(MsgPing) // ! is a method of Actor class ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 57. Creating “keywords” Moreover, receive is just a method of Actor class Method arguments can be passed in curly braces Ability to create DSL-like languages receive { case MsgPong => … }‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 58. Creating keywords - lock in Java String x = "No" l.lock(); try { x = "Yes" } finally { l.unlock(); } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 59. Creating keywords - lock in Scala var x = "No" lock(l) { x = "Yes“ } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 60. Lock “keyword” implemetation Lock “keyword” is really an ordinary method // f is a function (piece of code) returning // Unit (ie. void) def lock(l : Lock)(f : => Unit) = { l.lock(); try { f// call f } finally { l.unlock(); } } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 61. Parallelism ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 62. Parallelism What about parallelMap, parallelReduce etc. ? Not present in Scala library yet  Have to implement own versions ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 63. Little more advanced ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 64. What exactly is the List? List is an abstract class with 2 descendant case classes: Nil :: What gets called for List(1, 2, 3) ? object List { // * means variable arguments def apply[A](xs: A*): List[A] = xs.toList ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 65. scala.Seq scala.Seq is the supertype that defines methods like: filter, fold, map, reduce, take, contains, … List, Array, Maps… descend from Seq ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 66. Yield, iterators Syntax sugar for returning iterator object Iterators allow to iterate over a sequence of elements. They have hasNext() and next() methods. Lazy evaluation when olderThan21 is called, the for loop is not executed def olderThan21(xs: Iterator[Person]): Iterator[String] = { for (p <- xs if p.age > 21) yield p.getName() } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 67. Matching generic arguments? Will this compile? def genMatch(list: List[Any]) : String = list match { case (x: List[Int]) => "ints" case (x: List[String]) => "strings" } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 68. Matching generic arguments? JVM has no runtime support for generics (compiler uses erasure) def genMatch(list: List[Any]) : String = list match { // warning: type argument is unchecked case (x: List[Int]) => "ints" // error: unreachable code case (x: List[String]) => "strings" } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 69. Adding methods to classes Possible in dynamic languages (even at runtime) Possible using Extension methods in C# (just syntax sugar for static methods) How to do it in Scala? ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 70. “Adding methods” to classes ScalaTest test framework map should have value 7// legal scala code We want to be able to call map.should map does not have a “should” method Solution – wrapper object class Wrapper(wrappedObject : Any) { def should() … } ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 71. “Adding methods” to classes class Wrapper(wrappedObject : Any) { def added() { …} } Define implicit conversion method Any -> Wrapper implicit def wrap(o : Any) = new Wrapper(o) object.added() compiles as wrap(object).added() ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 72. “Adding methods” - demo class CollectionWrapper[T](wrappedCollection : java.util.Collection[T]) { def join(delim : String) : String = { val iter = wrappedCollection.iterator(); val buffer = new StringBuffer(iter.next().toString()); while (iter.hasNext()) buffer.append(delim).append(iter.next().toString()); return buffer.toString(); } } implicit def wrapCollection[T](o : java.util.Collection[T]) = new CollectionWrapper(o) var javaList = new java.util.ArrayList[String](); println(javaList.join("-"));// same as wrapCollection(javaList).join(“-“) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 73. Structural types { val length : Int } any object that has length field { def length() : Int } any object that has length() method Duck typing Invoking methods on the object uses reflection - slower ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 74. Traits – diamond inheritance? ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 75. XML import scala.xml._ val df = java.text.DateFormat.getDateInstance() val dateString = df.format(new java.util.Date()) def theDate(name: String) = <dateMsg addressedTo={ name }> Hello, { name }! Today is { dateString } </dateMsg>; println(theDate(“Amin Nezarat").toString()) ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)
  • 76. ‫دهنده‬ ‫ارائه‬:‫نظارات‬ ‫امین‬ ‫دکتر‬( www.hpclab.ir - aminnezarat@gmail.com)