click below
click below
Normal Size Small Size show me how
Scala
Trabalho para disciplina de Conceitos e Linguagem de Programação
Conceito | Implementação |
---|---|
Valores imutáveis | int x = 10 PS: Uma vez atribuído o valor, ele não poderá ter seu valor alterado durante todo o tempo de vida. |
Coleções | var listaString = List("palavra", "outra", "mais uma") var listaInt = List(3, 4, 2, 7) |
Funções de alta ordem | def chamandoFuncao(callback: () => Unit){ println("\nChamando uma função...") callback() } |
Casamento de padrões | abstract class T case class Num(x: int) extends T case class Plus(left: T, right: T) extends T object Interpreter { def eval(term: T): int = term match { case Num(x) => x case Plus(left, right) => eval(left) + eval(right) } } |
Recursividade em Cauda | def fatorial(num: Int):Int = { def fatorialIter(num: Int, acc: Int):Int = { if(num == 0) acc else fatorialIter(num - 1, acc * num) } fatorialIter(num, 1) } |
Funções Anônimas | object TimerAnonymous { def oncePerSecond(callback: () => unit) { while( true ) { callback(); Thread sleep 1000 } } def main(args: Array[String]) { oncePerSecond(() => println("o tempo corre como um raio...")) } } |
Curryng | def mod(div: Int)(num :Int) = (num % div) == 0) def main(args: Array[String]):Unit = { for(valor <- 1 to 20) { println("Valor " + valor + " é " + (if (mod(2) (valor)) "par" else "impar" )) } } |
Programação orienta a Objeto(Classe) | class Complex(real: Double, imaginary: Double) { def re() = real def im() = imaginary } |
Herança | abstract class Person(val name:String,val age:Int){ override def toString = "Name:"+name+"age:"+age def doSomething; } class Student(name:String,age:Int)extends Person(name,age){ override def doSomething = println(this + " I'm studying!") } |
Programação Funcional | def qsort(l: List[Int]): List[Int] = { l match { case List() => l case _ => qsort(for(x <- l.tail if x < l.head) yield x) ::: List(l.head) ::: qsort(for(x <- l.tail if x >= l.head) yield x) } } |
Membros abstratos | abstract class AbsCell { type T; val init: T; private var value: T = init; def get: T = value; def set(x: T): unit = { value = x } } val cell = new AbsCell { type T = int; val init = 1 } |
Métodos Genérico | def swap[T](x: GenCell[T], y: GenCell[T]): unit = { val t = x.get; x.set(y.get); y.set(t) } |
Classe Case | abstract class Tree case class Sum(l : Tree, r:Tree) extends Tree case class Var(n : String) extends Tree case class Const(v : Int) extends Tree |
For-comprehention | object ComprehensionTest1 extends Application { def even(from: Int, to: Int): List[Int] = for (i <- List.range(from, to) if i % 2 == 0) yield i Console.println(even(0, 20)) } |
Traits | abstract class Animal {def walk:String} trait Injury extends Animal { abstract override def walk = super.walk + quot; But I'm injured :( quot; } class Dog extends Animal{ def walk = quot;I'm walking.quot; } val goodDog = new Dog goodDog.walk |
Pattern Match | def fac(x : Int) : Int = x match { case 0 => 1 case xpto => xpto * fac(xpto 1) } match val p = new Person(18, quot;Gabrielquot;) val (age, name) = (p.age, p.name) age: Int = 18 name: String = Gabriel |
Tupla | val t = new Tuple3(1, "hello", Console) |
Iterators | val it: Iterator[Int] = Iterator.range(1, 100) while (it.hasNext) { val x = it.next println(x * x) } |
Streams | val fibs: Stream[BigInt] = BigInt(0) #:: BigInt(1) #:: fibs.zip( fibs.tail).map(n => { println("Adding %d and %d".format(n._1, n._2)) n._1 + n._2 }) fibs take 5 foreach println |
Expressões Regulares | import scala.util.matching.Regex object Test { def main(args: Array[String]) { val pattern = "Scala".r val str = "Scala is Scalable and cool" println(pattern findFirstIn str) } } |