Créé par Adrien Cominotto [≡] / Alexis Vandendaele [≡]
Créé par Martin Odersky en 2003
Son nom vient de l'anglais Scalable language
Scala intègre les paradigmes de programmation orientée objet et de programmation fonctionnelle, avec un typage statique
public class Person { private final String name; private final String firstName; public Person(String name, String firstName) { this.name = name; this.firstName = firstName; } public String getName() { return name; } public String getFirstName() { return firstName; } }
class Person(val name: String, val firstName: String)
val height = 1.9d val name = "James" println(s"Hello, $name") println(s"1 + 1 = ${1 + 1}") println(f"$name%s is $height%.2f meters tall")
class Person(val name: String, val firstName: String) { override def toString = s"[name=$name, firstname=$firstName]" } val p = new Person("Odersky", "Martin")
class Person(val name: String, val firstName: String) { override def toString = s"[name=$name, firstname=$firstName]" } object Person { // Companion object def apply(name: String, firstName: String) = new Person(name, firstName) val all = List( Person("Odersky", "Martin"), Person("Kuhn", "Roland"), Person("Suereth","Josh")) } val vips = Person.all
class Person(val name: String, val firstName: String) { override def toString = s"[name=$name, firstname=$firstName]" }
case class Person(val name: String, val firstName: String) val p = Person("Odersky", "Martin")
class Animal { override def toString = "I'm an animal" } trait Flying extends Animal { override def toString = s"${super.toString} and I fly" } trait Swimming extends Animal { override def toString = s"${super.toString} and I swim" } val swimmingBird = new Animal with Flying with Swimming val flyingFish = new Animal with Swimming with Flying
val t = ("Alexis", 29) // Tuple2 t._1 // Premier membre t._2 // Second membre
def profile(arg: Int, fct: Int => Int) = { val r = fct(arg) println(s"value = $r") r } val add42 = {i: Int => i + 42} profile(3, add42)
def parseArg(arg: String) = arg match { case "--help" | "-h" => println("Afficher l'aide") case "--version" | "-v" => println("Afficher la version") case unknownArg => println(s"Argument non reconnu: $unknownArg") }
case class MyClass(a: Int, b: String) MyClass(scala.util.Random.nextInt(100), "foo") match { case MyClass(42, s) => println("Bravo! $s, tu as trouvé la réponse!") case MyClass(n, "foo") if n < 42 => println(s"$n, trop bas"); case MyClass(n, "foo") if n > 42 => println(s"$n trop haut"); case MyClass(n, s) => println(s"Désolé $s, $n n'est pas la réponse") }
case class FooException(msg: String) extends RuntimeException(msg) try { throw FooException("bar") } catch { case FooException(msg) => println(s"Exception $msg") case e: RuntimeException => println("RuntimeException catched and rethrown") throw e case scala.util.control.NonFatal(e) => println("Something not that bad occured") throw e }
val foo = Option(42) foo match { case Some(v) => println(v) case None => println("Whoops, nothing...") }
speakers.filter(s => s.age <= 25)
speakers.map(s => s.name + " " + s.firstName)
speakers.flatMap(s => s :: imaginaryChildren)
for { s <- speakers ic <- imaginaryChildren if (ic.name == s.name) } yield (s, ic)
speakers.flatMap{s => imaginaryChildren .filter(_.name == s.name) .map(ic => (s, ic)) }
val l = List(1,2,3) l.foldLeft(""){ (acc, e) => acc + e }
persons.foldLeft((0,0)) { (acc, p) => p.sex match { case Female => (acc._1 + 1, acc._2) case Male => (acc._1, acc._2 + 1) case _ => acc } }
implicit val x = 5 def inc(implicit i: Int) = i + 1
case class Person(age: Int, name: String) { def sayHi = println(s"Hi!, I'm $name and I'm $age") } implicit def tupleToPerson(t: (Int, String)): Person = { Person(t._1, t._2) } (29, "Alexis").sayHi
implicit object AgeOrdering extends Ordering[Person] { def compare(a: Person, b: Person) = a.age compare b.age } person.sorted
val socket = Socket() val paquet = socket.readFromMemory() socket.sendToUSA(paquet)
val socket = Socket() val paquet = socket.readFromMemory() // 250 000 ns socket.sendToUSA(paquet) // 150 000 000 ns
1 nanoseconde = 1 seconde
val socket = Socket() val paquet = socket.readFromMemory() // 3 jours socket.sendToUSA(paquet) // 5 ans
import scala.concurrent.ExecutionContext.Implicits.global import scala.concurrent.Future val socket = Socket() val paquet = Future { socket.readFromMemory() } paquet.map(socket.sendToUSA)
for { paquet <- Future{ socket.readFromMemory() } } yield socket.sendToUSA(paquet)
import scala.concurrent._ import ExecutionContext.Implicits.global val promise = Promise[String]() val first = for(first <- promise.future) yield (s"$first FIRST!") def sendToUsa(message: String) = Future { Thread.sleep(scala.util.Random.nextInt(5000)) message }.map(m => promise.success(s"USA received $m")) def sendToChina(message: String) = Future { Thread.sleep(scala.util.Random.nextInt(5000)) message }.map(m => promise.success(s"China received $m")) sendToUsa("Hello!"); sendToChina("Ni hao!")
source: Les joies du code