Skip to content

Instantly share code, notes, and snippets.

View reevik's full-sized avatar
:octocat:

Erhan Bağdemir reevik

:octocat:
View GitHub Profile
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();
}
}
Lion lion = new Lion(AnimalSoundLib.ROAR);
lion.sing(); // roarrr
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
trait M[T] { def flatMap[U](f: T => M[U]): M[U] }
@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)
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))
for (i 1 to n; if i > j) yield (i, j)
(2,1), (3,1), (3,2), (4,1), (4,2), ...
$ scala -Xprint:parser -e "for (i <- 1 to n; if i % 2 == 0) yield i"
(1 to m).flatMap(i =>
(1 to n).filter(j => i > j)
map(k => (i, k)))