Last active
August 29, 2015 14:09
-
-
Save felipehummel/beeb770197edae6ac1e2 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Construtor da classe é (nome: String) | |
class MinhaClasse(nome: String) { | |
def olaQuem() = s"Olá $nome!" | |
} | |
val minha = new MinhaClasse("Felipe") | |
val msg = minha.olaQuem() | |
println(msg) // Olá Felipe! | |
class Pessoa(nome: String, idade: Int) { | |
def dizIdade() = println(s"$nome tem $idade anos") | |
} | |
val joao = new Pessoa("João", 30) | |
joao.nome // PANNNNN erro de compilação | |
// atributo nome não é acessível de fora da classe | |
// usar val ou var antes dos parâmetros do construtor | |
// os transformam em ATRIBUTOS acessíveis da classe | |
class BoaPessoa(val nome: String, var idade: Int) | |
val pedro = new BoaPessoa("Pedro", 50) | |
println(s"${pedro.nome} tem ${pedro.idade} anos") | |
pedro.idade = pedro.idade + 1 // ficou mais velho | |
println(pedro.idade) // 51 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// em Java: | |
class Ponto { | |
final int x; | |
final int y; | |
public Ponto(int x, int y) { | |
this.x = x; | |
this.y = y; | |
} | |
} | |
// ou em PHP: | |
class Ponto { | |
var $x; | |
var $y; | |
function __construct($x, $y) { | |
$this->x = $x; | |
$this->y = $y; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val nomes = List("José", "João", "Maria", "Fernanda") | |
val tamanhoNomes = nomes.map( pessoa => pessoa.size ) | |
println(tamanhoNomes) // List(4, 4, 5, 8) | |
def nomesMaioresQue(nomes: List[String], tamanho: Int) = | |
nomes.filter( nome => nome.size > tamanho) | |
nomesMaioresQue(nomes, 4) // List("Maria", "Fernanda") | |
nomesMaioresQue(nomes, 5) // List("Fernanda") | |
nomesMaioresQue(nomes, 3) // List("José", "João", "Maria", "Fernanda") | |
nomes.foreach ( nome => println(nome) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val megasena = List(25, 38, 44, 46, 53, 54) // lista encadeada | |
for (numeroSorteado <- megasena) { | |
println(numero) | |
} | |
var i = 0 | |
while(i < megasena.size) { | |
println(megasena(i)) | |
i += 1 | |
} | |
if (megasena.size > 5) { | |
println("Ewww!") | |
} | |
else { | |
println("WAT!?") | |
} | |
val dicionario = Map("Amazonas" -> "Manaus", | |
"Paraná" -> "Curitiba", | |
"Goiás" -> "Goiânia") | |
val curitiba = dicionario("Paraná") | |
for ((estado, cidade) <- dicionario) { | |
println(s"$cidade é a capital do estado $estado") | |
} | |
// conjunto de elementos únicos (remove duplicados) | |
val cidades = Set("Manaus", "Curitiba", "Curitiba", "Rio de Janeiro") | |
println(cidades.contains("Curitiba")) // True | |
println(cidades("Curitiba")) // True | |
println(cidades.size) // 3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val numero = 10 | |
val soma = numero + 10 | |
soma = 5 // PANNNNNN erro de compilação. Soma é um valor imutável | |
var acumulador = 0 | |
acumulador += 1 // continuando de boa | |
acumulador += 10 // ... de boa ... | |
println(acumulador) // imprime 11 | |
val texto = "textando texto texte" | |
val concatena = texto + " bem textado" // "textando texto texte bem textado" | |
// PANNNNN erro de compilação. concatena é imutável | |
concatena = "testando" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var texto = "meu texto favorito" // var => mutável | |
// PANNNNNN erro de compilação. | |
// Tentando atribuir um inteiro para uma variável do tipo String!! | |
texto = 10 | |
// Não preciso dizer que meuInteiro é do tipo Int | |
var meuInteiro = funcaoComplexaQueRetornaUmNúmero(texto) | |
meuInteiro = 20 // ... de boa ... | |
meuInteiro = texto // PANNNNNN erro de compilação! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def ola(nome: String): String = { | |
return "Olá " + nome + "!" | |
} | |
// mesma coisa que: | |
def ola(nome: String): String = { | |
// Scala assume que a última linha de uma função é o retorno | |
"Olá " + nome + "!" | |
} | |
// mesma coisa que: | |
// compilador infere que a função retorna String | |
def ola(nome: String) = { | |
"Olá " + nome + "!" | |
} | |
// mesma coisa que: | |
// forma mais compacta e usando interpolação de strings | |
// para usar interpolação é só colocar 's' e usar o $ | |
def ola(nome: String) = s"Olá $nome!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment