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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! Bifunctor type class for types with two type parameters.
//!
//! A `Bifunctor` is a type constructor that takes two type arguments and is a
//! functor in both of them. This means you can map over both type parameters
//! independently or simultaneously.
//!
//! # Example
//!
//! ```rust
//! use ordofp_core::bifunctor::Bifunctor;
//!
//! // Map over both sides of a Result
//! let result: Result<i32, &str> = Ok(42);
//! let mapped = result.bimap(|x| x * 2, |e| e.len());
//! assert_eq!(mapped, Ok(84));
//!
//! let err: Result<i32, &str> = Err("error");
//! let mapped = err.bimap(|x| x * 2, |e| e.len());
//! assert_eq!(mapped, Err(5));
//!
//! // Map over a tuple
//! let tuple = (10, "hello");
//! let mapped = tuple.bimap(|x| x * 2, |s: &str| s.len());
//! assert_eq!(mapped, (20, 5));
//! ```
/// A functor over two type parameters.
///
/// Bifunctor provides the `bimap` operation that allows mapping over both
/// type parameters simultaneously.
///
/// # Laws
///
/// A lawful Bifunctor must satisfy:
///
/// 1. **Identity**: `x.bimap(id, id) == x`
/// 2. **Composition**: `x.bimap(f, g).bimap(h, i) == x.bimap(|a| h(f(a)), |b| i(g(b)))`
pub trait Bifunctor {
/// The first type parameter.
type Left;
/// The second type parameter.
type Right;
/// The output type after transformation.
type Target<A, B>;
/// Map over both type parameters simultaneously.
///
/// # Example
///
/// ```rust
/// use ordofp_core::bifunctor::Bifunctor;
///
/// let result: Result<i32, String> = Ok(42);
/// let mapped = result.bimap(|x| x.to_string(), |e| e.len());
/// assert_eq!(mapped, Ok("42".to_string()));
/// ```
fn bimap<A, B, F, G>(self, f: F, g: G) -> Self::Target<A, B>
where
F: FnOnce(Self::Left) -> A,
G: FnOnce(Self::Right) -> B;
/// Map over the first (left) type parameter only.
///
/// # Example
///
/// ```rust
/// use ordofp_core::bifunctor::Bifunctor;
///
/// let result: Result<i32, &str> = Ok(42);
/// let mapped = result.map_left(|x| x.to_string());
/// assert_eq!(mapped, Ok("42".to_string()));
/// ```
#[inline]
fn map_left<A, F>(self, f: F) -> Self::Target<A, Self::Right>
where
Self: Sized,
Self::Right: Sized,
F: FnOnce(Self::Left) -> A,
{
self.bimap(f, |x| x)
}
/// Map over the second (right) type parameter only.
///
/// # Example
///
/// ```rust
/// use ordofp_core::bifunctor::Bifunctor;
///
/// let result: Result<i32, &str> = Err("error");
/// let mapped = result.map_right(|e| e.len());
/// assert_eq!(mapped, Err(5));
/// ```
#[inline]
fn map_right<B, G>(self, g: G) -> Self::Target<Self::Left, B>
where
Self: Sized,
Self::Left: Sized,
G: FnOnce(Self::Right) -> B,
{
self.bimap(|x| x, g)
}
}
// Implementation for Result<T, E>
impl<T, E> Bifunctor for Result<T, E> {
type Left = T;
type Right = E;
type Target<A, B> = Result<A, B>;
#[inline]
fn bimap<A, B, F, G>(self, f: F, g: G) -> Result<A, B>
where
F: FnOnce(T) -> A,
G: FnOnce(E) -> B,
{
match self {
Ok(t) => Ok(f(t)),
Err(e) => Err(g(e)),
}
}
}
// Implementation for tuples (T, U)
impl<T, U> Bifunctor for (T, U) {
type Left = T;
type Right = U;
type Target<A, B> = (A, B);
#[inline]
fn bimap<A, B, F, G>(self, f: F, g: G) -> (A, B)
where
F: FnOnce(T) -> A,
G: FnOnce(U) -> B,
{
(f(self.0), g(self.1))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[cfg(feature = "alloc")]
use alloc::{format, string::String, vec::Vec};
#[test]
fn test_result_bimap_ok() {
let result: Result<i32, &str> = Ok(42);
let mapped = result.bimap(|x| x * 2, str::len);
assert_eq!(mapped, Ok(84));
}
#[test]
fn test_result_bimap_err() {
let result: Result<i32, &str> = Err("error");
let mapped = result.bimap(|x| x * 2, str::len);
assert_eq!(mapped, Err(5));
}
#[test]
fn test_result_map_left_ok() {
let result: Result<i32, &str> = Ok(42);
let mapped = result.map_left(|x| x * 2);
assert_eq!(mapped, Ok(84));
}
#[test]
fn test_result_map_left_err() {
let result: Result<i32, &str> = Err("error");
let mapped = result.map_left(|x| x * 2);
assert_eq!(mapped, Err("error"));
}
#[test]
fn test_result_map_right_ok() {
let result: Result<i32, &str> = Ok(42);
let mapped = result.map_right(str::len);
assert_eq!(mapped, Ok(42));
}
#[test]
fn test_result_map_right_err() {
let result: Result<i32, &str> = Err("error");
let mapped = result.map_right(str::len);
assert_eq!(mapped, Err(5));
}
#[test]
fn test_tuple_bimap() {
let tuple = (10, "hello");
let mapped = tuple.bimap(|x| x * 2, |s: &str| s.len());
assert_eq!(mapped, (20, 5));
}
#[test]
fn test_tuple_map_left() {
let tuple = (10i32, "hello");
let mapped = tuple.map_left(|x| x * 2);
assert_eq!(mapped, (20, "hello"));
}
#[test]
fn test_tuple_map_right() {
let tuple = (10, "hello");
let mapped = tuple.map_right(|s: &str| s.len());
assert_eq!(mapped, (10, 5));
}
#[test]
fn test_bifunctor_identity_law() {
// bimap(id, id) == id
let result: Result<i32, &str> = Ok(42);
let mapped = result.bimap(|x| x, |e| e);
assert_eq!(mapped, Ok(42));
let tuple = (10, "hello");
let mapped = tuple.bimap(|x| x, |s| s);
assert_eq!(mapped, (10, "hello"));
}
#[test]
fn test_bifunctor_composition_law() {
// bimap(f, g).bimap(h, i) == bimap(h . f, i . g)
let tuple = (10, 20);
let f = |x: i32| x + 1;
let g = |x: i32| x * 2;
let h = |x: i32| x * 10;
let i = |x: i32| x - 5;
// Composed
let composed = tuple.bimap(f, g).bimap(h, i);
// Single bimap with composed functions
let single = tuple.bimap(|x| h(f(x)), |x| i(g(x)));
assert_eq!(composed, single);
}
#[cfg(feature = "alloc")]
#[test]
fn test_complex_types() {
// Test with more complex types
let result: Result<Vec<i32>, String> = Ok(alloc::vec![1, 2, 3]);
let mapped = result.bimap(|v| v.iter().sum::<i32>(), |e| format!("Error: {e}"));
assert_eq!(mapped, Ok(6));
let err_result: Result<Vec<i32>, String> = Err("oops".into());
let mapped = err_result.bimap(|v| v.iter().sum::<i32>(), |e| format!("Error: {e}"));
assert_eq!(mapped, Err("Error: oops".into()));
}
#[test]
fn test_nested_tuples() {
let tuple = ((1, 2), (3, 4));
let mapped = tuple.bimap(|(a, b)| a + b, |(c, d)| c * d);
assert_eq!(mapped, (3, 12));
}
#[cfg(feature = "alloc")]
#[test]
fn test_result_map_error_equivalent() {
// map_right is like map_err for Result
let result: Result<i32, &str> = Err("file not found");
let mapped = result.map_right(|e| format!("IO Error: {e}"));
assert_eq!(mapped, Err("IO Error: file not found".into()));
}
}