Last active
December 28, 2015 12:29
-
-
Save felipecrv/7500921 to your computer and use it in GitHub Desktop.
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
val sixInts = List(1, 2, 3, 4, 5, 6) | |
for { | |
x <- sixInts | |
y <- sixInts | |
if x % 2 == 0 && y % 2 == 0 | |
} yield (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
for { | |
x <- sixInts if x % 2 == 0 | |
y <- sixInts | |
if y % 2 == 0 // o if também pode ser colocado em outra linha | |
} yield (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
val sixInts = List(1, 2, 3, 4, 5, 6) | |
for { | |
x <- sixInts | |
y <- sixInts | |
if x % 2 == 0 && y % 2 == 0 | |
} yield (x, y) // List((2,2), (2,4), (2,6), (4,2), (4,4), (4,6), (6,2), (6,4), (6,6)) | |
// Aplicando a regra 3, equivale a | |
sixInts.flatMap(x => | |
for ( | |
y <- sixInts | |
if x % 2 == 0 && y % 2 == 0 | |
) yield (x, y) | |
) | |
// Aplicando a regra 2, equivale a | |
sixInts.flatMap(x => | |
for ( | |
y <- sixInts.withFilter(y => x % 2 == 0 && y % 2 == 0) | |
) yield (x, y) | |
) | |
// Aplicando a regra 1, equivale a | |
sixInts.flatMap(x => | |
sixInts.withFilter(y => x % 2 == 0 && y % 2 == 0).map(y => | |
(x, y) | |
) | |
) // List((2,2), (2,4), (2,6), (4,2), (4,4), (4,6), (6,2), (6,4), (6,6)) |
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
val sixInts = List(1, 2, 3, 4, 5, 6) | |
for { | |
x <- sixInts | |
if x % 2 == 0 | |
y <- sixInts | |
if y % 2 == 0 | |
} yield (x, y) // List((2,2), (2,4), (2,6), (4,2), (4,4), (4,6), (6,2), (6,4), (6,6)) | |
// Aplicando a regra 2, equivale a | |
for { | |
x <- sixInts.withFilter(x => x % 2 == 0) | |
y <- sixInts | |
if y % 2 == 0 | |
} yield (x, y) | |
// Aplicando a regra 3, equivale a | |
sixInts.withFilter(x => x % 2 == 0).flatMap(x => | |
for( | |
y <- sixInts | |
if y % 2 == 0 | |
) yield (x, y) | |
) | |
// Aplicando a regra 2, equivale a | |
sixInts.withFilter(x => x % 2 == 0).flatMap(x => | |
for ( | |
y <- sixInts.withFilter(y => y % 2 == 0) | |
) yield (x, y) | |
) | |
// Aplicando a regra 1, equivale a | |
sixInts.withFilter(x => x % 2 == 0).flatMap(x => | |
sixInts.withFilter(y => y % 2 == 0).map(y => | |
(x, y) | |
) | |
) // List((2,2), (2,4), (2,6), (4,2), (4,4), (4,6), (6,2), (6,4), (6,6)) |
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
val lines = List( | |
"Lorem ipsum dolor sit amet, consectetur", | |
"adipisicing elit, sed do eiusmod tempor", | |
"incididunt ut labore et dolore magna aliqua." | |
) | |
lines flatMap (line => line split "\\W+") // List[String] = List(Lorem, ipsum, dolor, sit, amet, | |
//| consectetur, adipisicing, elit, sed, do, | |
//| eiusmod, tempor, incididunt, ut, labore, et, | |
//| dolore, magna, aliqua) |
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
for (x <- e1 if p; s) yield e2 | |
// onde `p` é um expressão booleana | |
// `s` é uma sequência (possivelmente vazia) de geradores e filtros | |
// equivale a | |
for (x <- e1.withFilter(x => p); s) yield e2 | |
// e a tradução continua a partir dessa nova expressão. | |
// Particulamente para `s` vazio | |
for (x <- e1 if p) yield e2 | |
// equivale a | |
for (x <- e1.withFilter(x => p)) yield e2 | |
// que equivale a (pela regra 1) | |
e1.withFilter(x => p).map(x => e2) | |
// Para o caso em que `s` é não-vazio, a tradução segue | |
// aplicando-se a regra 3. |
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
/** | |
* Selects all elements of this list which satisfy a predicate. | |
* | |
* @param p the predicate used to test elements. | |
* @returns a new list consisting of all elements of this | |
* list that satisfy the given predicate p. | |
* The order of the elements is preserved. | |
*/ | |
def filter(p: (A) ⇒ Boolean): List[A] | |
/** | |
* Creates a non-strict filter of this list. | |
* | |
* Note: the difference between c filter p and c withFilter p | |
* is that the former creates a new collection, whereas the | |
* latter only restricts the domain of subsequent | |
* map, flatMap, foreach, and withFilter operations. | |
* | |
* @param p the predicate used to test elements. | |
* returns an object of class WithFilter, which supports | |
* map, flatMap, foreach, and withFilter operations. | |
* All these operations apply to those elements of this | |
* list which satisfy the predicate p. | |
*/ | |
def withFilter(p: (A) ⇒ Boolean): FilterMonadic[A, List[A]] | |
/** | |
* Builds a new collection by applying a function to all elements | |
* of this list. | |
* | |
* @param B the element type of the returned collection. | |
* @param f the function to apply to each element. | |
* returns a new list resulting from applying the given | |
* function f to each element of this list and | |
* collecting the results. | |
*/ | |
def map[B](f: (A) ⇒ B): List[B] | |
/** | |
* Builds a new collection by applying a function to all elements | |
* of this list and using the elements of the resulting collections. | |
* | |
* @param f the function to apply to each element. | |
* @returns a new list resulting from applying the given | |
* collection-valued function f to each element of | |
* this list and concatenating the results. | |
*/ | |
def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): List[B] |
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
for (x <- e1; y <- e2; s) yield e3 | |
// onde `s` é uma sequência (possivelmente vazia) de geradores e filtros | |
// equivale a | |
e1.flatMap(x => for (y <- e2; s) yield e3) | |
// e a tradução continua a partir dessa nova expressão. |
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
for (x <- e1) yield e2 | |
// onde `e1` e `e2` são expressões | |
// equivale a | |
e1.map(x => e2) |
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
// sixInts => e1 | |
// 2 * x => e2 | |
for (x <- sixInts) yield 2 * x //> List[Int] = List(2, 4, 6, 8, 10, 12) | |
// equivale a | |
sixInts.map(x => 2 * x) //> List[Int] = List(2, 4, 6, 8, 10, 12) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment