summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/funcdata/theory/Bifunctor.java
blob: 72fccffa52f7516071758333b43cbaf913461f4f (plain)
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package bjc.funcdata.theory;

import java.util.function.Function;

/**
 * A functor over a pair of heterogeneous types.
 *
 * @author ben
 *
 * @param <LeftType>
 *                    The type stored on the 'left' of the pair.
 *
 * @param <RightType>
 *                    The type stored on the 'right' of the pair.
 */
public interface Bifunctor<LeftType, RightType> {
	/**
	 * Alias for functor mapping.
	 *
	 * @author EVE
	 *
	 * @param <OldLeft>
	 *                   The old left type.
	 *
	 * @param <OldRight>
	 *                   The old right type.
	 *
	 * @param <NewLeft>
	 *                   The new left type.
	 *
	 * @param <NewRight>
	 *                   The new right type.
	 */
	public interface BifunctorMap<OldLeft, OldRight, NewLeft, NewRight>
			extends Function<Bifunctor<OldLeft, OldRight>, Bifunctor<NewLeft, NewRight>> {
		/*
		 * Alias
		 */
	}

	/**
	 * Alias for left functor mapping.
	 *
	 * @author EVE
	 *
	 * @param <OldLeft>
	 *                   The old left type.
	 *
	 * @param <OldRight>
	 *                   The old right type.
	 *
	 * @param <NewLeft>
	 *                   The new left type.
	 */
	public interface LeftBifunctorMap<OldLeft, OldRight, NewLeft>
			extends BifunctorMap<OldLeft, OldRight, NewLeft, OldRight> {
		/*
		 * Alias
		 */
	}

	/**
	 * Alias for right functor mapping.
	 *
	 * @author EVE
	 *
	 * @param <OldLeft>
	 *                   The old left type.
	 *
	 * @param <OldRight>
	 *                   The old right type.
	 *
	 * @param <NewRight>
	 *                   The new right type.
	 */
	public interface RightBifunctorMap<OldLeft, OldRight, NewRight>
			extends BifunctorMap<OldLeft, OldRight, OldLeft, NewRight> {
		/*
		 * Alias
		 */
	}

	/**
	 * Lift a pair of functions to a single function that maps over both parts of a
	 * pair.
	 *
	 * @param <OldLeft>
	 *                   The old left type of the pair.
	 *
	 * @param <OldRight>
	 *                   The old right type of the pair.
	 *
	 * @param <NewLeft>
	 *                   The new left type of the pair.
	 *
	 * @param <NewRight>
	 *                   The new right type of the pair.
	 *
	 * @param leftFunc
	 *                   The function that maps over the left of the pair.
	 *
	 * @param rightFunc
	 *                   The function that maps over the right of the pair.
	 *
	 * @return A function that maps over both parts of the pair.
	 */
	public default <OldLeft, OldRight, NewLeft, NewRight>
			BifunctorMap<OldLeft, OldRight, NewLeft, NewRight>
			bimap(final Function<OldLeft, NewLeft> leftFunc,
					final Function<OldRight, NewRight> rightFunc) {
		final BifunctorMap<OldLeft, OldRight, NewLeft, NewRight> bimappedFunc
				= argPair -> {
					final LeftBifunctorMap<OldLeft, OldRight, NewLeft> leftMapper
							= argPair.fmapLeft(leftFunc);

					final Bifunctor<NewLeft, OldRight> leftMappedFunctor
							= leftMapper.apply(argPair);
					final RightBifunctorMap<NewLeft, OldRight, NewRight> rightMapper
							= leftMappedFunctor.fmapRight(rightFunc);

					return rightMapper.apply(leftMappedFunctor);
				};

		return bimappedFunc;
	}

	/**
	 * Lift a function to operate over the left part of this pair.
	 *
	 * @param <OldLeft>
	 *                   The old left type of the pair.
	 *
	 * @param <OldRight>
	 *                   The old right type of the pair.
	 *
	 * @param <NewLeft>
	 *                   The new left type of the pair.
	 *
	 * @param func
	 *                   The function to lift to work over the left side of the
	 *                   pair.
	 *
	 * @return The function lifted to work over the left side of bifunctors.
	 */
	public <OldLeft, OldRight, NewLeft> LeftBifunctorMap<OldLeft, OldRight, NewLeft>
			fmapLeft(Function<OldLeft, NewLeft> func);

	/**
	 * Lift a function to operate over the right part of this pair.
	 *
	 * @param <OldLeft>
	 *                   The old left type of the pair.
	 *
	 * @param <OldRight>
	 *                   The old right type of the pair.
	 *
	 * @param <NewRight>
	 *                   The new right type of the pair.
	 *
	 * @param func
	 *                   The function to lift to work over the right side of the
	 *                   pair.
	 *
	 * @return The function lifted to work over the right side of bifunctors.
	 */
	public <OldLeft, OldRight, NewRight> RightBifunctorMap<OldLeft, OldRight, NewRight>
			fmapRight(Function<OldRight, NewRight> func);

	/**
	 * Get the value contained on the left of this bifunctor.
	 *
	 * @return The value on the left side of this bifunctor.
	 */
	public LeftType getLeft();

	/**
	 * Get the value contained on the right of this bifunctor.
	 *
	 * @return The value on the right of this bifunctor.
	 */
	public RightType getRight();
}