Added bimap method and unit tests

This commit is contained in:
Marco Cetica 2024-09-18 16:09:34 +02:00
parent 4bdd2278a7
commit 6eabe26105
Signed by: marco
GPG Key ID: 45060A949E90D0FD
5 changed files with 44 additions and 0 deletions

View File

@ -64,6 +64,19 @@ public sealed interface Either<L, R> permits Left, Right {
*/ */
<T> Either<L, T> map(Function<R, T> fn); <T> Either<L, T> map(Function<R, T> fn);
/**
* <p>
* Applies <i>onLeft</i> function to the <i>Left</i> subtype or the
* <i>onRight</i> function to the <i>Right</i> subtype.
* </p>
* @param onLeft The function to apply to the <i>Left</i> subtyp
* @param onRight The function to apply to the <i>Right</i> subtype
* @return An <i>Either</i> functor
* @param <T> The return type of the <i>onLeft</i> function
* @param <K> The return type of the <i>onRight</i> function
*/
<T, K> Either<T, K> bimap(Function<L, T> onLeft, Function<R, K> onRight);
/** /**
* <p> * <p>
* Returns the content of <i>Right</i> or a default value * Returns the content of <i>Right</i> or a default value
@ -85,4 +98,5 @@ public sealed interface Either<L, R> permits Left, Right {
* @return The left value of <i>Either</i> or the default value * @return The left value of <i>Either</i> or the default value
*/ */
L fromLeft(L defaultValue); L fromLeft(L defaultValue);
} }

View File

@ -32,6 +32,11 @@ public record Left<L, R>(L value) implements Either<L, R> {
return new Left<>(this.value); return new Left<>(this.value);
} }
@Override
public <T, K> Either<T, K> bimap(Function<L, T> onLeft, Function<R, K> onRight) {
return new Left<>(onLeft.apply(this.value));
}
@Override @Override
public R fromRight(R defaultValue) { public R fromRight(R defaultValue) {
return defaultValue; return defaultValue;

View File

@ -32,6 +32,11 @@ public record Right<L, R>(R value) implements Either<L, R> {
return new Right<>(fn.apply(this.value)); return new Right<>(fn.apply(this.value));
} }
@Override
public <T, K> Either<T, K> bimap(Function<L, T> onLeft, Function<R, K> onRight) {
return new Right<>(onRight.apply(this.value));
}
@Override @Override
public R fromRight(R defaultValue) { public R fromRight(R defaultValue) {
return this.value; return this.value;

View File

@ -51,6 +51,16 @@ public class LeftTests {
), "Undefined variable"); ), "Undefined variable");
} }
@Test
public void testFunctorBiMapLeft() {
Function<Integer, Integer> f = x -> x + 1;
Function<String, String> g = String::toUpperCase;
Either<Integer, String> actual = this.resEither.bimap(f, g);
assertEquals(actual.fromLeft(-1), 20);
}
@Test @Test
public void testLeftFunctorIdentityMorphism() { public void testLeftFunctorIdentityMorphism() {
// Applying the map function with the identity function, // Applying the map function with the identity function,

View File

@ -51,6 +51,16 @@ public class RightTests {
), 16); ), 16);
} }
@Test
public void testFunctorBiMapRight() {
Function<Integer, Integer> f = x -> x + 1;
Function<String, String> g = String::toUpperCase;
Either<Integer, String> actual = this.resEither.bimap(f, g);
assertEquals(actual.fromRight("-1"), "QUERY EXECUTED SUCCESSFULLY");
}
@Test @Test
public void testRightFunctorIdentityMorphism() { public void testRightFunctorIdentityMorphism() {
// Applying the map function with the identity function, // Applying the map function with the identity function,