mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-05-05 14:28:31 +00:00
some more translated+
This commit is contained in:
parent
8defb7f6fe
commit
373bc005ab
@ -188,62 +188,61 @@ val html = """<form id="daform">
|
|||||||
// 2. Funciones
|
// 2. Funciones
|
||||||
/////////////////////////////////////////////////
|
/////////////////////////////////////////////////
|
||||||
|
|
||||||
// Functions are defined like so:
|
// Las funciones se definen de la siguiente manera:
|
||||||
//
|
//
|
||||||
// def functionName(args...): ReturnType = { body... }
|
// def nombreFuncion(argumentos...): TipoRetorno = { cuerpo... }
|
||||||
//
|
//
|
||||||
// If you come from more traditional languages, notice the omission of the
|
// Si estás acostumbrado a lenguajes más tradicionales, observa la omisión de la palabra return.
|
||||||
// return keyword. In Scala, the last expression in the function block is the
|
// En Scala, la última expresión en el bloque de función es el valor de retorno.
|
||||||
// return value.
|
def sumaDeCuadrados(x: Int, y: Int): Int = {
|
||||||
def sumOfSquares(x: Int, y: Int): Int = {
|
|
||||||
val x2 = x * x
|
val x2 = x * x
|
||||||
val y2 = y * y
|
val y2 = y * y
|
||||||
x2 + y2
|
x2 + y2
|
||||||
}
|
}
|
||||||
|
|
||||||
// The { } can be omitted if the function body is a single expression:
|
// Los { } pueden omitirse si el cuerpo de la función es una única expresión:
|
||||||
def sumOfSquaresShort(x: Int, y: Int): Int = x * x + y * y
|
def sumaDeCuadradosCorta(x: Int, y: Int): Int = x * x + y * y
|
||||||
|
|
||||||
// Syntax for calling functions is familiar:
|
// La sintaxis para llamar funciones es familiar:
|
||||||
sumOfSquares(3, 4) // => 25
|
sumaDeCuadrados(3, 4) // => 25
|
||||||
|
|
||||||
// You can use parameters names to specify them in different order
|
// Puedes usar los nombres de los parámetros para llamarlos en orden diferente
|
||||||
def subtract(x: Int, y: Int): Int = x - y
|
def restar(x: Int, y: Int): Int = x - y
|
||||||
|
|
||||||
subtract(10, 3) // => 7
|
restar(10, 3) // => 7
|
||||||
subtract(y=10, x=3) // => -7
|
restar(y=10, x=3) // => -7
|
||||||
|
|
||||||
// In most cases (with recursive functions the most notable exception), function
|
// En la mayoría de los casos (siendo las funciones recursivas la excepción más notable),
|
||||||
// return type can be omitted, and the same type inference we saw with variables
|
// el tipo de retorno de la función puede ser omitido, y la misma inferencia de tipos que vimos con las variables
|
||||||
// will work with function return values:
|
// funcionará con los valores de retorno de las funciones:
|
||||||
def sq(x: Int) = x * x // Compiler can guess return type is Int
|
def sq(x: Int) = x * x // El compilador puede adivinar que el tipo de retorno es Int
|
||||||
|
|
||||||
// Functions can have default parameters:
|
// Las funciones pueden tener parametros por defecto:
|
||||||
def addWithDefault(x: Int, y: Int = 5) = x + y
|
def sumarConDefecto(x: Int, y: Int = 5) = x + y
|
||||||
addWithDefault(1, 2) // => 3
|
sumarConDefecto(1, 2) // => 3
|
||||||
addWithDefault(1) // => 6
|
sumarConDefecto(1) // => 6
|
||||||
|
|
||||||
|
|
||||||
// Anonymous functions look like this:
|
// Las funciones anónimas se escriben así:
|
||||||
(x: Int) => x * x
|
(x: Int) => x * x
|
||||||
|
|
||||||
// Unlike defs, even the input type of anonymous functions can be omitted if the
|
// Al contrario que los defs, incluso el tipo de entrada de las funciones anónimas puede ser omitido si
|
||||||
// context makes it clear. Notice the type "Int => Int" which means a function
|
// el contexto lo deja claro. Observa el tipo "Int => Int" que significa que es una función
|
||||||
// that takes Int and returns Int.
|
// que recibe Int y retorna Int.
|
||||||
val sq: Int => Int = x => x * x
|
val sq: Int => Int = x => x * x
|
||||||
|
|
||||||
// Anonymous functions can be called as usual:
|
// Las funciones anónimas pueden ser llamadas como las demás:
|
||||||
sq(10) // => 100
|
sq(10) // => 100
|
||||||
|
|
||||||
// If each argument in your anonymous function is
|
// Si cada argumento en tu función anónima es usado solo una vez,
|
||||||
// used only once, Scala gives you an even shorter way to define them. These
|
// Scala te da una manera incluso más corta de definirlos.
|
||||||
// anonymous functions turn out to be extremely common, as will be obvious in
|
// Estas funciones anónimas son extremadamente comunes,
|
||||||
// the data structure section.
|
// como será obvio en la sección de estructuras de datos.
|
||||||
val addOne: Int => Int = _ + 1
|
val sumarUno: Int => Int = _ + 1
|
||||||
val weirdSum: (Int, Int) => Int = (_ * 2 + _ * 3)
|
val sumaRara: (Int, Int) => Int = (_ * 2 + _ * 3)
|
||||||
|
|
||||||
addOne(5) // => 6
|
sumarUno(5) // => 6
|
||||||
weirdSum(2, 4) // => 16
|
sumaRara(2, 4) // => 16
|
||||||
|
|
||||||
|
|
||||||
// The return keyword exists in Scala, but it only returns from the inner-most
|
// The return keyword exists in Scala, but it only returns from the inner-most
|
||||||
|
Loading…
Reference in New Issue
Block a user