This file contains 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
object Classes { | |
class MyFirstClass | |
val myFirstClass = new MyFirstClass | |
//---------------------------------------------// | |
class Vehicle( | |
var passengers: Int, //number of passengers |
This file contains 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
object Functions extends App { | |
//Existen dos formas de declarar las funciones | |
// Una función básica y conocida acá como callByValue | |
def sum(x: Int, y: Int) = x + y | |
// Como la invocamos | |
sum(1, 2) // 3 | |
// Otra conocida acá como callByName donde pasamos otra función | |
def product(x: => Int, y: => Int) = x * y |
This file contains 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
object Numbers extends App { | |
// Podemos inicializamos una lista y al mismo tiempo le pre-populamos valores | |
val numbers = List(1, 2, 3, 4, 5) | |
// Recorremos la lista de números y filtramos los pares | |
def pairNumbers: List[Int] = | |
for { | |
number <- numbers | |
if number % 2 == 0 | |
} yield number |
This file contains 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
object HelloWorldVar extends App { | |
// Declaramos una variable mutable vacía de tipo String | |
var value: String = "" | |
// Le asignamos un valor | |
value = "mundo" | |
// Imprimimos el valor y acá podemos ver como usar string interpolation | |
println(s"¡Hola, $value!") //¡Hola, mundo! | |
} |
This file contains 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
object HelloWorldIfElse extends App { | |
// Podemos ver como vimos antes que si no ponemos tipo scala lo infiere por nosotros | |
val language = "en" | |
// podemos notar que si no tiene parámetros la función no necesita paréntesis | |
def message = { | |
if (language == "es") "¡Hola, mundo!" | |
else if (language == "pt") "¡Olá, mundo!" | |
else "Hello, world!" | |
} |
This file contains 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
object HelloWorldPatternMatching extends App { | |
private val language = "pt" | |
//no hay necesidad de colocar break el rompe donde primero coincida y si no en el default, es decir ' _ ' | |
private val message = language match { | |
case "es" => "¡Hola, mundo!" | |
case "pt" => "¡Olá, mundo!" | |
case _ => "Hello, world!" | |
} | |
println(message) //¡Olá, mundo! |