forked from palatable/lambda
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonadReaderLaws.java
More file actions
40 lines (34 loc) · 1.68 KB
/
MonadReaderLaws.java
File metadata and controls
40 lines (34 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package testsupport.traits;
import com.jnape.palatable.lambda.adt.Maybe;
import com.jnape.palatable.lambda.functions.Fn1;
import com.jnape.palatable.lambda.io.IO;
import com.jnape.palatable.lambda.monad.MonadReader;
import com.jnape.palatable.lambda.monoid.builtin.Present;
import static com.jnape.palatable.lambda.adt.Maybe.just;
import static com.jnape.palatable.lambda.adt.Maybe.nothing;
import static com.jnape.palatable.lambda.functions.builtin.fn1.Id.id;
import static com.jnape.palatable.lambda.io.IO.throwing;
import static java.util.Collections.singletonList;
public class MonadReaderLaws<M extends MonadReader<?, ?, M>> implements EquivalenceTrait<MonadReader<?, ?, M>> {
@Override
public Class<? super MonadReader<?, ?, ?>> type() {
return MonadReader.class;
}
@Override
public void test(Equivalence<MonadReader<?, ?, M>> equivalence) {
Present.<String>present((x, y) -> x + "\n\t - " + y)
.<Fn1<Equivalence<MonadReader<?, ?, M>>, Maybe<String>>>foldMap(
f -> f.apply(equivalence),
singletonList(this::testLocalIdentity)
)
.match(IO::io,
s -> throwing(new AssertionError("The following MonadReader laws did not hold for instance of "
+ equivalence + ": \n\t - " + s)))
.unsafePerformIO();
}
private Maybe<String> testLocalIdentity(Equivalence<MonadReader<?, ?, M>> equivalence) {
return equivalence.invMap(mr -> mr.local(id())).equals(equivalence)
? nothing()
: just("local identity (mr.local(id()).equals(mr))");
}
}