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
| public class CatBar { | |
| private List<Cat> catFriends = new ArrayList<>(); | |
| public void enter(Cat cat) { | |
| catFriends.add(cat); | |
| } | |
| public void singTogether() { | |
| for (Cat cat: catFriends) { | |
| cat.sing(); | |
| } | |
| } |
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
| Lion lion = new Lion(AnimalSoundLib.ROAR); | |
| lion.sing(); // roarrr |
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
| Cat cat = catService.getCat(); // a real cat created which meows | |
| catbar.enter(cat); | |
| Cat lion = catService.getCat(); // this time the service gives you a lion instance back. | |
| catbar.enter(lion); | |
| catbar.singTogether(); // party begins: meoww moewww ROOARRR meow |
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
| trait M[T] { def flatMap[U](f: T => M[U]): M[U] } |
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 list = List(1,2,3,4,5) | |
| list.flatMap(e => List(e - 1, e, e + 1)) | |
| > res0: List[Int] = List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6) |
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 list = List(1,2,3,4,5) | |
| list.map(e => List(e - 1, e, e + 1)) | |
| > res0: List[List[Int]] = List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6)) |
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
| for (i 1 to n; if i > j) yield (i, j) |
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
| (2,1), (3,1), (3,2), (4,1), (4,2), ... |
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
| $ scala -Xprint:parser -e "for (i <- 1 to n; if i % 2 == 0) yield i" |
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
| (1 to m).flatMap(i => | |
| (1 to n).filter(j => i > j) | |
| map(k => (i, k))) |