Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix PetKata and update documentation on slides.md - #348 #349

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
53 changes: 34 additions & 19 deletions docs/pet-kata/slides.md
Original file line number Diff line number Diff line change
Expand Up @@ -940,12 +940,31 @@ public void getAgeStatisticsOfPets()
}
```

Bob Smith's Pet Names as String
--------------------------
```java
@Test
public void bobSmithsPetNamesAsString()
{
//find Bob Smith
Person person = this.people
.detect(each -> each.named("Bob Smith"));

//get Bob Smith's pets' names
String names = person.getPets()
.collect(Pet::getName)
.makeString(" & ");

Assertions.assertEquals("Dolly & Spot", names);
}
```


Stream to EC refactor #1
Bob Smith's Pet Names as String
------------------------
```java
@Test
public void streamsToECRefactor1()
public void bobSmithsPetNamesAsString()
{
// Find Bob Smith
Person person = this.people.detect(each -> each.named("Bob Smith"));
Expand All @@ -960,33 +979,29 @@ public void streamsToECRefactor1()
```


Stream to EC refactor #2
------------------------
Immutable Pet Counts by Emoji
--------------------------
```java
@Test
public void streamsToECRefactor2()
public void immutablePetCountsByEmoji()
{
// Hint: Try to replace the Map<PetType, Long> with a Bag<PetType>
MutableBag<PetType> petTypes = this.people
.asUnmodifiable()
.flatCollect(Person::getPets)
.countBy(Pet::getType);

Assertions.assertEquals(2, petTypes.occurrencesOf(PetType.CAT));
Assertions.assertEquals(2, petTypes.occurrencesOf(PetType.DOG));
Assertions.assertEquals(2, petTypes.occurrencesOf(PetType.HAMSTER));
Assertions.assertEquals(1, petTypes.occurrencesOf(PetType.SNAKE));
Assertions.assertEquals(1, petTypes.occurrencesOf(PetType.TURTLE));
Assertions.assertEquals(1, petTypes.occurrencesOf(PetType.BIRD));
// Hint: Try to replace the immutable Map<String, Long> with an ImmutableBag<String>
ImmutableBag<String> countsByEmoji = Bags.immutable.withAll(
this.people.flatCollect(Person::getPetTypes).countBy(PetType::toString)
);
Assertions.assertEquals(
Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"),
countsByEmoji
);
}
```


Stream to EC refactor #3
Top Three Pets
------------------------
```java
@Test
public void streamsToECRefactor3()
public void topThreePets()
{
// Hint: The result of groupingBy/counting can almost always be replaced by a Bag
// Hint: Look for the API on Bag that might return the top 3 pet types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void immutablePetCountsByEmoji()
.collect(Collectors.groupingBy(pet -> pet.getType().toString(), Collectors.counting())));

Assertions.assertEquals(
Map.of("🐱", 2L, "🐶", 2L, "🐹", 2L, "🐍", 1L, "🐢", 1L, "🐦", 1L),
Bags.immutable.of("🐱", "🐱", "🐶", "🐶", "🐹", "🐹", "🐍", "🐢", "🐦"),
countsByEmoji
);
}
Expand Down