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:
parent
612fd7138a
commit
786569a995
5 changed files with 64 additions and 7 deletions
3
pom.xml
3
pom.xml
|
@ -11,7 +11,7 @@
|
|||
</parent>
|
||||
|
||||
<artifactId>mon</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<version>3.1.0</version>
|
||||
|
||||
<name>Mon</name>
|
||||
<description>Wrapper, TypeAlias, Maybe, Result, Tree, Lazy, Either and Combinators for Java.
|
||||
|
@ -60,7 +60,6 @@
|
|||
<artifactId>apiguardian-api</artifactId>
|
||||
<version>${apiguardian-api.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
|
|
23
src/main/java/net/kemitix/mon/reader/Reader.java
Normal file
23
src/main/java/net/kemitix/mon/reader/Reader.java
Normal 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);
|
||||
|
||||
}
|
1
src/main/java/net/kemitix/mon/reader/package-info.java
Normal file
1
src/main/java/net/kemitix/mon/reader/package-info.java
Normal file
|
@ -0,0 +1 @@
|
|||
package net.kemitix.mon.reader;
|
38
src/test/java/net/kemitix/mon/ReaderTest.java
Normal file
38
src/test/java/net/kemitix/mon/ReaderTest.java
Normal 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();
|
||||
|
||||
}
|
||||
}
|
|
@ -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<Iterable<String>> { }
|
||||
|
||||
interface WrappedListInteger extends Wrapper<List<Integer>> {}
|
||||
|
||||
interface AWrapper extends Wrapper<String> {}
|
||||
|
||||
@Test
|
||||
|
@ -65,6 +60,7 @@ class WrapperTest {
|
|||
final WrappedString wrappedString = () -> "1";
|
||||
final WrappedInteger wrappedInteger = () -> 1;
|
||||
//then
|
||||
//noinspection AssertBetweenInconvertibleTypes
|
||||
assertThat(wrappedString).isNotEqualTo(wrappedInteger);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue