commit 72e5fc2e0380d50afaf84186ded33152b493500d Author: Marco Cetica Date: Tue Sep 17 10:34:11 2024 +0200 First upload diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d238533 --- /dev/null +++ b/.gitignore @@ -0,0 +1,41 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store + +### IntelliJ ### +.idea/ diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..edf9b6b --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.ceticamarco + LambdaTonic + 1.0-SNAPSHOT + + + 22 + 22 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/com/ceticamarco/Either.java b/src/main/java/com/ceticamarco/Either.java new file mode 100644 index 0000000..565e7ad --- /dev/null +++ b/src/main/java/com/ceticamarco/Either.java @@ -0,0 +1,17 @@ +package com.ceticamarco; + +import java.util.function.Function; + +/** + *

+ * This protocol defines a new sum type and provides the methods + * to operate on the associated data. + * The Either type can be implemented only by the Left + * and the Right classes. + *

+ * @param The left type, representing the error value + * @param The right type, representing the actual value + */ +public sealed interface Either permits Left, Right { + T match(Function onLeft, Function onRight); +} \ No newline at end of file diff --git a/src/main/java/com/ceticamarco/Left.java b/src/main/java/com/ceticamarco/Left.java new file mode 100644 index 0000000..51ec3d7 --- /dev/null +++ b/src/main/java/com/ceticamarco/Left.java @@ -0,0 +1,18 @@ +package com.ceticamarco; + +import java.util.function.Function; + +/** + *

+ * + *

+ * @param value + * @param + * @param + */ +public record Left(L value) implements Either { + @Override + public T match(Function onLeft, Function onRight) { + return onLeft.apply(this.value); + } +} \ No newline at end of file diff --git a/src/main/java/com/ceticamarco/Right.java b/src/main/java/com/ceticamarco/Right.java new file mode 100644 index 0000000..6aee839 --- /dev/null +++ b/src/main/java/com/ceticamarco/Right.java @@ -0,0 +1,10 @@ +package com.ceticamarco; + +import java.util.function.Function; + +public record Right(R value) implements Either { + @Override + public T match(Function onLeft, Function onRight) { + return onRight.apply(this.value); + } +} \ No newline at end of file