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
This commit is contained in:
Paul Campbell 2021-08-18 17:00:51 +01:00 committed by GitHub
parent 612fd7138a
commit 786569a995
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 64 additions and 7 deletions

View file

@ -11,7 +11,7 @@
</parent> </parent>
<artifactId>mon</artifactId> <artifactId>mon</artifactId>
<version>3.0.0</version> <version>3.1.0</version>
<name>Mon</name> <name>Mon</name>
<description>Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java. <description>Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java.
@ -60,7 +60,6 @@
<artifactId>apiguardian-api</artifactId> <artifactId>apiguardian-api</artifactId>
<version>${apiguardian-api.version}</version> <version>${apiguardian-api.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId> <artifactId>junit-jupiter-api</artifactId>

View file

@ -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 <E> The type of the environment required by the program.
* @param <R> The type of the result returned by the program.
*/
@API(status = API.Status.EXPERIMENTAL, since = "3.0.1")
@FunctionalInterface
public interface Reader<E, R> {
/**
* Executes the program.
*
* @param env the required environment
* @return the result of the program
*/
R run(E env);
}

View file

@ -0,0 +1 @@
package net.kemitix.mon.reader;

View file

@ -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<Environment, Integer> program = (Environment e) -> {
return e.intValue();
};
//when
Integer result = program.run(env);
//then
assertThat(result).isEqualTo(value);
}
private interface Environment {
Integer intValue();
}
}

View file

@ -1,10 +1,7 @@
package net.kemitix.mon; package net.kemitix.mon;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -18,8 +15,6 @@ class WrapperTest {
interface WrappedIterableString extends Wrapper<Iterable<String>> { } interface WrappedIterableString extends Wrapper<Iterable<String>> { }
interface WrappedListInteger extends Wrapper<List<Integer>> {}
interface AWrapper extends Wrapper<String> {} interface AWrapper extends Wrapper<String> {}
@Test @Test
@ -65,6 +60,7 @@ class WrapperTest {
final WrappedString wrappedString = () -> "1"; final WrappedString wrappedString = () -> "1";
final WrappedInteger wrappedInteger = () -> 1; final WrappedInteger wrappedInteger = () -> 1;
//then //then
//noinspection AssertBetweenInconvertibleTypes
assertThat(wrappedString).isNotEqualTo(wrappedInteger); assertThat(wrappedString).isNotEqualTo(wrappedInteger);
} }