Skip to content

Instantly share code, notes, and snippets.

View reevik's full-sized avatar
:octocat:

Erhan Bağdemir reevik

:octocat:
View GitHub Profile
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))
@reevik
reevik / list.scala
Last active February 14, 2022 19:54
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)
trait M[T] { def flatMap[U](f: T => M[U]): M[U] }
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
Lion lion = new Lion(AnimalSoundLib.ROAR);
lion.sing(); // roarrr
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();
}
}
public class Lion extends Cat {
public Lion(AnimalSound animalSound) {
super(animalSound);
}
public void hunt() {}
// and other lion related methods.
}
Cat cat1 = new Cat(AnimalSoundLib.MEOW);
cat1.sing(); // meooww
Cat cat2 = new Cat(AnimalSoundLib.MEOOO);
cat2.sing(); // meoooo
// more cats
public class Cat {
private AnimalSound sound;
public Cat(AnimalSound animalSound) {
this.sound = animalSound;
}
public void sing() {
soundEffect(sound);
}
}
;; 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))