From 786569a995db71f91a7cbabd7a870df3efef4e98 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 18 Aug 2021 17:00:51 +0100 Subject: [PATCH] Implement a Reader monad (#214) * Add Reader monad * WrapperTest: clean up * Reader: add API status annotations * Version set to 3.1.0 * pom: remove duplicate dependency --- pom.xml | 3 +- .../java/net/kemitix/mon/reader/Reader.java | 23 +++++++++++ .../net/kemitix/mon/reader/package-info.java | 1 + src/test/java/net/kemitix/mon/ReaderTest.java | 38 +++++++++++++++++++ .../java/net/kemitix/mon/WrapperTest.java | 6 +-- 5 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/main/java/net/kemitix/mon/reader/Reader.java create mode 100644 src/main/java/net/kemitix/mon/reader/package-info.java create mode 100644 src/test/java/net/kemitix/mon/ReaderTest.java diff --git a/pom.xml b/pom.xml index b02268e..9aa63a0 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ mon - 3.0.0 + 3.1.0 Mon Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java. @@ -60,7 +60,6 @@ apiguardian-api ${apiguardian-api.version} - org.junit.jupiter junit-jupiter-api diff --git a/src/main/java/net/kemitix/mon/reader/Reader.java b/src/main/java/net/kemitix/mon/reader/Reader.java new file mode 100644 index 0000000..5a9c7a4 --- /dev/null +++ b/src/main/java/net/kemitix/mon/reader/Reader.java @@ -0,0 +1,23 @@ +package net.kemitix.mon.reader; + +import org.apiguardian.api.API; + +/** + * Returns a program ready to run upon the supply of a suitable environment. + * + * @param The type of the environment required by the program. + * @param The type of the result returned by the program. + */ +@API(status = API.Status.EXPERIMENTAL, since = "3.0.1") +@FunctionalInterface +public interface Reader { + + /** + * Executes the program. + * + * @param env the required environment + * @return the result of the program + */ + R run(E env); + +} diff --git a/src/main/java/net/kemitix/mon/reader/package-info.java b/src/main/java/net/kemitix/mon/reader/package-info.java new file mode 100644 index 0000000..77e50d7 --- /dev/null +++ b/src/main/java/net/kemitix/mon/reader/package-info.java @@ -0,0 +1 @@ +package net.kemitix.mon.reader; \ No newline at end of file diff --git a/src/test/java/net/kemitix/mon/ReaderTest.java b/src/test/java/net/kemitix/mon/ReaderTest.java new file mode 100644 index 0000000..0cf801f --- /dev/null +++ b/src/test/java/net/kemitix/mon/ReaderTest.java @@ -0,0 +1,38 @@ +package net.kemitix.mon; + +import net.kemitix.mon.reader.Reader; +import org.assertj.core.api.WithAssertions; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class ReaderTest + implements WithAssertions { + + @Test + @DisplayName("read a value and return it") + void readAndReturn() { + //given + Integer value = 123; + Environment env = new Environment() { + @Override + public Integer intValue() { + return value; + } + }; + Reader program = (Environment e) -> { + return e.intValue(); + }; + + //when + Integer result = program.run(env); + + //then + assertThat(result).isEqualTo(value); + } + + private interface Environment { + + Integer intValue(); + + } +} diff --git a/src/test/java/net/kemitix/mon/WrapperTest.java b/src/test/java/net/kemitix/mon/WrapperTest.java index 32e5f7e..1bfbf86 100644 --- a/src/test/java/net/kemitix/mon/WrapperTest.java +++ b/src/test/java/net/kemitix/mon/WrapperTest.java @@ -1,10 +1,7 @@ package net.kemitix.mon; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -18,8 +15,6 @@ class WrapperTest { interface WrappedIterableString extends Wrapper> { } - interface WrappedListInteger extends Wrapper> {} - interface AWrapper extends Wrapper {} @Test @@ -65,6 +60,7 @@ class WrapperTest { final WrappedString wrappedString = () -> "1"; final WrappedInteger wrappedInteger = () -> 1; //then + //noinspection AssertBetweenInconvertibleTypes assertThat(wrappedString).isNotEqualTo(wrappedInteger); }