Last active
January 25, 2019 14:10
-
-
Save elfer12/4690ee78ca1ff90b8b932e19aec9360f to your computer and use it in GitHub Desktop.
Fold Left Exercise
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
Dada una Persona (con peso) y Donuts (calorías), modelar los objetos necesarios para que la persona pueda comer una lista finita de donuts mientras aumenta de peso. | |
Utilizar fold left. | |
----------------- | |
Ejemplo de FoldLeft | |
List(1,2,3,4).foldLeft("Los números son: ")((acc, num) => acc + num.toString) | |
// "Los números son: 1234" | |
List(1,2,3,4).foldLeft(0)((acc, num) => acc + num) | |
//res0: Int = 10 | |
----------------- | |
class Donut(val fat: Int) | |
class Person(val weight: Int) { | |
def eat(donut:Donut):Person = { | |
new Person(weight + donut.fat) | |
} | |
} | |
val homer: Person = new Person(100) | |
val donuts = List(new Donut(10), new Donut(20), new Donut(300)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment