Skip to content
Permalink
Browse files

Refactor removed map knowledge from location

  • Loading branch information...
Liutauras Vilda
Liutauras Vilda committed Oct 11, 2018
1 parent 4cf272f commit f381544b2a9efc93fa5fec6d92af6cf8f42e20b7
@@ -2,8 +2,8 @@

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public final class Location {

@@ -19,8 +19,8 @@ public static Location of(int r, int c) {
return new Location(r, c);
}

long numberOfLivingNeighborsInA(Map<Location, Mutable> map) {
List<Location> neighborhood = Arrays.asList(
List<Location> neighborhood() {

This comment has been minimized.

Copy link
@Nibsi

Nibsi Oct 12, 2018

Bit of a pity that you're returning your stream as a list, because a method like this is almost assuredly going to be used in another stream operation. Just create and return the stream, and let the client collect it when they're done adding their own operations to the pipeline.

This comment has been minimized.

Copy link
@liutaurasvilda

liutaurasvilda Oct 12, 2018

Owner

Never even thought about such option. Priceless tip!

List<Location> distances = Arrays.asList(
Location.of(-1, -1),
Location.of(-1, 0),
Location.of(-1, +1),
@@ -30,11 +30,9 @@ long numberOfLivingNeighborsInA(Map<Location, Mutable> map) {
Location.of(+1, 0),
Location.of(+1, +1)
);
return neighborhood.stream()
return distances.stream()
.map(this::neighbor)
.map(map::get)
.filter(mutable -> mutable.equals(Cell.ALIVE))
.count();
.collect(Collectors.toList());
}

private Location neighbor(Location distance) {
@@ -12,8 +12,8 @@
private World() {
this.map = new LinkedHashMap<>();
IntStream.range(0, SIZE)
.forEach(r -> IntStream.range(0, SIZE)
.forEach(c -> map.put(Location.of(r, c), Cell.DEAD)));
.forEach(r -> IntStream.range(0, SIZE)
.forEach(c -> map.put(Location.of(r, c), Cell.DEAD)));
}

public static World empty() {
@@ -32,24 +32,33 @@ public boolean hasPopulation() {
@Override
public World nextGeneration() {
World newWorld = empty();
ConwaysRules.Builder rules = new ConwaysRules.Builder();
IntStream.range(0, SIZE)
.forEach(r -> IntStream.range(0, SIZE)
.forEach(c -> {
ConwaysRules.Builder rules = new ConwaysRules.Builder();
rules.withLivingNeighbors(Location.of(r, c).numberOfLivingNeighborsInA(map));
newWorld.map.put(Location.of(r, c), this.map.get(Location.of(r, c)).mutate(rules.build()));
}));
.forEach(r -> IntStream.range(0, SIZE)
.forEach(c -> {
rules.withLivingNeighbors(count(Location.of(r, c)));
newWorld.map.put(Location.of(r, c), mutableAt(Location.of(r, c)).mutate(rules.build()));
}));
return newWorld;
}

private long count(Location location) {
return location.neighborhood().stream()
.map(map::get).filter(n -> n.equals(Cell.ALIVE)).count();
}

private Mutable mutableAt(Location location) {
return map.get(location);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
IntStream.range(0, SIZE)
.forEach(r -> { IntStream.range(0, SIZE)
.forEach(c -> sb.append(map.get(Location.of(r, c))));
sb.append("\n");
});
.forEach(r -> { IntStream.range(0, SIZE)
.forEach(c -> sb.append(map.get(Location.of(r, c))));
sb.append("\n");
});
return sb.toString();
}
}

0 comments on commit f381544

Please sign in to comment.
You can’t perform that action at this time.