forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriter.java
More file actions
65 lines (49 loc) · 1.23 KB
/
Writer.java
File metadata and controls
65 lines (49 loc) · 1.23 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package fj.data;
import fj.*;
/**
* Created by MarkPerry on 7/07/2014.
*/
public final class Writer<W, A> {
private final A val;
private final W logValue;
private final Monoid<W> monoid;
private Writer(A a, W w, Monoid<W> m) {
val = a;
logValue = w;
monoid = m;
}
public P2<W, A> run() {
return P.p(logValue, val);
}
public A value() {
return val;
}
public W log() {
return logValue;
}
public Monoid<W> monoid() {
return monoid;
}
public static <W, A> Writer<W, A> unit(A a, W w, Monoid<W> m) {
return new Writer<>(a, w, m);
}
public static <W, A> Writer<W, A> unit(A a, Monoid<W> m) {
return new Writer<>(a, m.zero(), m);
}
public Writer<W, A> tell(W w) {
return unit(val, monoid.sum(logValue, w), monoid);
}
public <B> Writer<W, B> map(F<A, B> f) {
return unit(f.f(val), logValue, monoid);
}
public <B> Writer<W, B> flatMap(F<A, Writer<W, B>> f) {
Writer<W, B> writer = f.f(val);
return unit(writer.val, writer.monoid.sum(logValue, writer.logValue), writer.monoid);
}
public static <B> Writer<String, B> unit(B b) {
return unit(b, Monoid.stringMonoid);
}
public static <A> F<A, Writer<String, A>> stringLogger() {
return a -> unit(a, Monoid.stringMonoid);
}
}