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