Commit a7eab22d authored by Colin DAMON's avatar Colin DAMON
Browse files

Merge branch '109-upgrade-prettier-for-records' into 'master'

Resolve "Upgrade prettier for records"

Closes #109

See merge request !90
parents fb845bfd 5866d891
...@@ -2,7 +2,7 @@ package fr.ippon.life; ...@@ -2,7 +2,7 @@ package fr.ippon.life;
import java.util.Set; import java.util.Set;
public record Cell(int row, int column) { public record Cell(int row, int column) {
private static final int LOW_POPULATION_THRESHOLD = 2; private static final int LOW_POPULATION_THRESHOLD = 2;
private static final int HIGH_POPULATION_THRESHOLD = 3; private static final int HIGH_POPULATION_THRESHOLD = 3;
private static final int BORN_THRESHOLD = HIGH_POPULATION_THRESHOLD; private static final int BORN_THRESHOLD = HIGH_POPULATION_THRESHOLD;
...@@ -10,8 +10,7 @@ public record Cell(int row, int column) { ...@@ -10,8 +10,7 @@ public record Cell(int row, int column) {
boolean stayAlive(Set<Cell> aliveCells) { boolean stayAlive(Set<Cell> aliveCells) {
long neighbourgCount = countNeigbours(aliveCells); long neighbourgCount = countNeigbours(aliveCells);
return neighbourgCount == LOW_POPULATION_THRESHOLD return neighbourgCount == LOW_POPULATION_THRESHOLD || neighbourgCount == HIGH_POPULATION_THRESHOLD;
|| neighbourgCount == HIGH_POPULATION_THRESHOLD;
} }
boolean born(Set<Cell> aliveCells) { boolean born(Set<Cell> aliveCells) {
...@@ -19,9 +18,7 @@ public record Cell(int row, int column) { ...@@ -19,9 +18,7 @@ public record Cell(int row, int column) {
} }
private long countNeigbours(Set<Cell> aliveCells) { private long countNeigbours(Set<Cell> aliveCells) {
return aliveCells.stream() return aliveCells.stream().filter(this::isNeighbour).count();
.filter(this::isNeighbour)
.count();
} }
private boolean isNeighbour(Cell other) { private boolean isNeighbour(Cell other) {
...@@ -29,8 +26,7 @@ public record Cell(int row, int column) { ...@@ -29,8 +26,7 @@ public record Cell(int row, int column) {
return false; return false;
} }
return delta(row, other.row) <= 1 return delta(row, other.row) <= 1 && delta(column, other.column) <= 1;
&& delta(column, other.column) <= 1;
} }
private static int delta(int first, int second) { private static int delta(int first, int second) {
......
...@@ -10,10 +10,7 @@ import java.util.stream.IntStream; ...@@ -10,10 +10,7 @@ import java.util.stream.IntStream;
public class Diamond { public class Diamond {
public static List<String> of(char size) { public static List<String> of(char size) {
List<String> diamond = IntStream List<String> diamond = IntStream.range((int) 'A', ((int) size) + 1).mapToObj(toLine(size)).collect(Collectors.toList());
.range((int) 'A', ((int) size) + 1)
.mapToObj(toLine(size))
.collect(Collectors.toList());
List<String> secondPart = new ArrayList<>(diamond); List<String> secondPart = new ArrayList<>(diamond);
Collections.reverse(secondPart); Collections.reverse(secondPart);
...@@ -25,8 +22,6 @@ public class Diamond { ...@@ -25,8 +22,6 @@ public class Diamond {
} }
private static IntFunction<String> toLine(char size) { private static IntFunction<String> toLine(char size) {
return current -> Line.of((char) current, size) return current -> Line.of((char) current, size).representation();
.representation();
} }
} }
package fr.craft.kata; package fr.craft.kata;
public record Line(String representation) { public record Line(String representation) {
public static Line of(char character, char size) { public static Line of(char character, char size) {
String outerSpaces = outerSpaces(character, size); String outerSpaces = outerSpaces(character, size);
...@@ -9,17 +8,14 @@ public record Line(String representation) { ...@@ -9,17 +8,14 @@ public record Line(String representation) {
return new Line(outerSpaces + "A" + outerSpaces); return new Line(outerSpaces + "A" + outerSpaces);
} }
return new Line(outerSpaces + character return new Line(outerSpaces + character + innerSpaces(character) + character + outerSpaces);
+ innerSpaces(character) + character + outerSpaces);
} }
private static String outerSpaces(char character, private static String outerSpaces(char character, char size) {
char size) {
return " ".repeat(size - character); return " ".repeat(size - character);
} }
private static String innerSpaces(char character) { private static String innerSpaces(char character) {
return " ".repeat(((character - 'A') - 1) * 2 + 1); return " ".repeat(((character - 'A') - 1) * 2 + 1);
} }
} }
...@@ -6,7 +6,6 @@ import java.io.IOException; ...@@ -6,7 +6,6 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
class DiamondTest { class DiamondTest {
...@@ -33,9 +32,7 @@ class DiamondTest { ...@@ -33,9 +32,7 @@ class DiamondTest {
private List<String> diamond(char file) { private List<String> diamond(char file) {
try { try {
return Files.readAllLines(Paths return Files.readAllLines(Paths.get("src/test/resources/" + String.valueOf(file).toLowerCase() + ".txt"));
.get("src/test/resources/" + String.valueOf(file)
.toLowerCase() + ".txt"));
} catch (IOException e) { } catch (IOException e) {
throw new AssertionError(e.getMessage(), e); throw new AssertionError(e.getMessage(), e);
} }
......
...@@ -8,32 +8,26 @@ class LineTest { ...@@ -8,32 +8,26 @@ class LineTest {
@Test @Test
void shouldGetLetterALineForSizeA() { void shouldGetLetterALineForSizeA() {
assertThat(Line.of('A', 'A') assertThat(Line.of('A', 'A').representation()).isEqualTo("A");
.representation()).isEqualTo("A");
} }
@Test @Test
void shouldGetLetterALineForSizeB() { void shouldGetLetterALineForSizeB() {
assertThat(Line.of('A', 'B') assertThat(Line.of('A', 'B').representation()).isEqualTo(" A ");
.representation()).isEqualTo(" A ");
} }
@Test @Test
void shouldGetLetterBLineForSizeB() { void shouldGetLetterBLineForSizeB() {
assertThat(Line.of('B', 'B') assertThat(Line.of('B', 'B').representation()).isEqualTo("B B");
.representation()).isEqualTo("B B");
} }
@Test @Test
void shouldGetLetterBLineForSizeC() { void shouldGetLetterBLineForSizeC() {
assertThat(Line.of('B', 'C') assertThat(Line.of('B', 'C').representation()).isEqualTo(" B B ");
.representation()).isEqualTo(" B B ");
} }
@Test @Test
void shouldGetLetterDLineForSizeD() { void shouldGetLetterDLineForSizeD() {
assertThat(Line.of('D', 'D') assertThat(Line.of('D', 'D').representation()).isEqualTo("D" + " ".repeat(5) + "D");
.representation())
.isEqualTo("D" + " ".repeat(5) + "D");
} }
} }
...@@ -24,8 +24,7 @@ public class Maybe<T> { ...@@ -24,8 +24,7 @@ public class Maybe<T> {
} }
public <U> Maybe<U> bind(Function<T, Maybe<U>> other) { public <U> Maybe<U> bind(Function<T, Maybe<U>> other) {
return value.map(other) return value.map(other).orElse(Maybe.fail());
.orElse(Maybe.fail());
} }
@Override @Override
......
...@@ -22,20 +22,19 @@ public class State<S, T> { ...@@ -22,20 +22,19 @@ public class State<S, T> {
return new State<>(state -> new Result<>(state, state)); return new State<>(state -> new Result<>(state, state));
} }
public <U> State<S, U> bind( public <U> State<S, U> bind(Function<T, State<S, U>> binder) {
Function<T, State<S, U>> binder) { return new State<>(
return new State<>(initial -> { initial -> {
Result<S, T> result = runner.apply(initial); Result<S, T> result = runner.apply(initial);
return binder.apply(result.result()) return binder.apply(result.result()).run(result.updated());
.run(result.updated()); }
}); );
} }
public Result<S, T> run(S initial) { public Result<S, T> run(S initial) {
return runner.apply(initial); return runner.apply(initial);
} }
public static record Result<S, T> (S updated, T result) { public static record Result<S, T>(S updated, T result) {}
}
} }
...@@ -8,23 +8,16 @@ class MaybeTest { ...@@ -8,23 +8,16 @@ class MaybeTest {
@Test @Test
void shouldMapFromIntegerToString() { void shouldMapFromIntegerToString() {
assertThat(Maybe.pure(42) assertThat(Maybe.pure(42).map(String::valueOf)).usingRecursiveComparison().isEqualTo(Maybe.pure("42"));
.map(String::valueOf)).usingRecursiveComparison()
.isEqualTo(Maybe.pure("42"));
} }
@Test @Test
void shouldNotMapFailedMaybe() { void shouldNotMapFailedMaybe() {
assertThat(Maybe.fail() assertThat(Maybe.fail().map(String::valueOf)).usingRecursiveComparison().isEqualTo(Maybe.fail());
.map(String::valueOf)).usingRecursiveComparison()
.isEqualTo(Maybe.fail());
} }
@Test @Test
void shouldBindPureToPure() { void shouldBindPureToPure() {
assertThat(Maybe.pure(42) assertThat(Maybe.pure(42).bind(value -> Maybe.pure(String.valueOf(value)))).usingRecursiveComparison().isEqualTo(Maybe.pure("42"));
.bind(value -> Maybe.pure(String.valueOf(value))))
.usingRecursiveComparison()
.isEqualTo(Maybe.pure("42"));
} }
} }
...@@ -9,20 +9,18 @@ class StateTest { ...@@ -9,20 +9,18 @@ class StateTest {
@Test @Test
void shouldWriteSyracuseSequence() { void shouldWriteSyracuseSequence() {
State<Integer, Integer> syracuse = intState() State<Integer, Integer> syracuse = intState()
.bind((previous) -> { .bind(
Integer next = previous % 2 == 0 ? previous / 2 previous -> {
: previous * 3 + 1; Integer next = previous % 2 == 0 ? previous / 2 : previous * 3 + 1;
return State.set(next) return State.set(next).bind(dummy -> State.pure(next));
.bind(dummy -> State.pure(next)); }
}); );
assertThat(syracuse.run(5)) assertThat(syracuse.run(5)).isEqualTo(new State.Result<>(16, 16));
.isEqualTo(new State.Result<>(16, 16));
} }
private State<Integer, Integer> intState() { private State<Integer, Integer> intState() {
return State.get(); return State.get();
} }
} }
...@@ -11,7 +11,6 @@ public class PlayingWithBigDecimal { ...@@ -11,7 +11,6 @@ public class PlayingWithBigDecimal {
public void roundingSubstract() { public void roundingSubstract() {
BigDecimal a = new BigDecimal(2.00); BigDecimal a = new BigDecimal(2.00);
BigDecimal b = new BigDecimal(0.99); BigDecimal b = new BigDecimal(0.99);
// assertThat(a.subtract(b).toString()).isEqualTo("1"); // assertThat(a.subtract(b).toString()).isEqualTo("1");
// assertThat(a.subtract(b).toString()).isEqualTo("1.01"); // assertThat(a.subtract(b).toString()).isEqualTo("1.01");
// assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625"); // assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625");
......
This diff is collapsed.
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
], ],
"devDependencies": { "devDependencies": {
"husky": "^6.0.0", "husky": "^6.0.0",
"lint-staged": "8.1.4", "lint-staged": "^11.0.0",
"onchange": "^7.1.0", "onchange": "^7.1.0",
"prettier": "^2.2.1", "prettier": "^2.2.1",
"prettier-plugin-java": "1.0.2", "prettier-plugin-java": "^1.1.1",
"pretty-quick": "^3.1.0" "pretty-quick": "^3.1.0"
}, },
"engines": { "engines": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment