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
| 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
| 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
| 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
| 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
| 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
| public class Lion extends Cat { | |
| public Lion(AnimalSound animalSound) { | |
| super(animalSound); | |
| } | |
| public void hunt() {} | |
| // and other lion related methods. | |
| } |
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 cat1 = new Cat(AnimalSoundLib.MEOW); | |
| cat1.sing(); // meooww | |
| Cat cat2 = new Cat(AnimalSoundLib.MEOOO); | |
| cat2.sing(); // meoooo | |
| // more cats |
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 Cat { | |
| private AnimalSound sound; | |
| public Cat(AnimalSound animalSound) { | |
| this.sound = animalSound; | |
| } | |
| public void sing() { | |
| soundEffect(sound); | |
| } | |
| } |
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
| ;; Multimethod definition showing that the run | |
| ;; might have multiple informations. The identity | |
| ;; function is used as dispatcher function to | |
| ;; determine which implementation of run is to be | |
| ;; called | |
| (defmulti run identity) | |
| ;; The run method implementation for temp. | |
| (defmethod run "temp" | |
| [city day] | |
| (get-temp city day)) |