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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//! # Transformation Utilities
//!
//! This module provides utilities for transforming functorial data types and building
//! transformation pipelines. These tools help you work with functors in an ergonomic
//! and composable way.
//!
//! ## Key Features
//!
//! - **Transformation Functions**: Apply transformations to functorial values
//! - **Pipeline Builder**: Build fluent transformation chains with the `Pipeline` type
//! - **Type-Safe Operations**: Leverage Rust's type system for safe transformations
//!
//! The utilities in this module work with any type that implements the `Functor` trait,
//! including standard library types like `Option` and `Result`, and library types
//! like `Either` and `Maybe`.
use crateFunctor;
// ===== Transformation Functions =====
/// Applies a transformation to all functorial values in a collection.
///
/// This function maps a transformation over each functorial value in the collection,
/// using the `Functor` trait's capabilities.
///
/// # Type Parameters
///
/// * `T`: A type that implements `Functor`
/// * `F`: The transformation function type
/// * `U`: The result type of the transformation
///
/// # Arguments
///
/// * `values`: The collection of functorial values to transform
/// * `f`: The transformation function to apply to the content of each functor
///
/// # Returns
///
/// A new vector containing the transformed functorial values
///
/// # Examples
///
/// ```rust
/// use rustica::utils::transform_utils::transform_all;
/// use rustica::prelude::*;
/// use rustica::datatypes::maybe::Maybe;
///
/// // Create a collection of Maybe values
/// let values: Vec<Maybe<i32>> = vec![
/// Maybe::Just(1),
/// Maybe::Just(2),
/// Maybe::Nothing,
/// Maybe::Just(4)
/// ];
///
/// // Transform the values inside each Maybe
/// let doubles = transform_all(&values, |&x| x * 2);
///
/// // The transformation is applied only to the Just values
/// assert_eq!(doubles[0], Maybe::Just(2));
/// assert_eq!(doubles[1], Maybe::Just(4));
/// assert_eq!(doubles[2], Maybe::Nothing);
/// assert_eq!(doubles[3], Maybe::Just(8));
/// ```
///
/// Works with standard library types too:
///
/// ```rust
/// use rustica::utils::transform_utils::transform_all;
/// use rustica::prelude::*;
///
/// // A collection of Option values
/// let options: Vec<Option<String>> = vec![
/// Some("hello".to_string()),
/// None,
/// Some("world".to_string())
/// ];
///
/// // Transform the values inside each Option
/// let uppercase = transform_all(&options, |s| s.to_uppercase());
///
/// assert_eq!(uppercase[0], Some("HELLO".to_string()));
/// assert_eq!(uppercase[1], None);
/// assert_eq!(uppercase[2], Some("WORLD".to_string()));
/// ```
/// Applies a transformation to a single optional value.
///
/// This function maps a transformation over the value inside an `Option` functor,
/// preserving the `None` case and transforming the `Some` case.
///
/// # Type Parameters
///
/// * `T`: A type that implements `Functor`
/// * `F`: The transformation function type
/// * `U`: The result type of the transformation
///
/// # Arguments
///
/// * `value`: The optional functorial value to transform
/// * `f`: The transformation function to apply to the content
///
/// # Returns
///
/// An optional transformed functorial value, or `None` if the input was `None`
///
/// # Examples
///
/// ```rust
/// use rustica::utils::transform_utils::transform_chain;
/// use rustica::prelude::*;
/// use rustica::datatypes::maybe::Maybe;
///
/// // Apply a transformation to a Just value
/// let maybe_just: Option<Maybe<i32>> = Some(Maybe::Just(5));
/// let result1 = transform_chain(maybe_just, |&x| x * 2);
/// assert_eq!(result1, Some(Maybe::Just(10)));
///
/// // Apply a transformation to a Nothing value
/// let maybe_nothing: Option<Maybe<i32>> = Some(Maybe::Nothing);
/// let result2 = transform_chain(maybe_nothing, |&x| x * 2);
/// assert_eq!(result2, Some(Maybe::Nothing));
///
/// // When the outer Option is None, the result is None
/// let none: Option<Maybe<i32>> = None;
/// let result3 = transform_chain(none, |&x| x * 2);
/// assert_eq!(result3, None);
/// ```
// ===== Pipeline Builder =====
/// A pipeline for building chains of transformations on functorial values.
///
/// This type provides a fluent interface for applying a sequence of transformations
/// to a functorial value. It wraps the value and allows chaining transformations
/// before finally extracting the result.
///
/// # Type Parameters
///
/// * `T`: The type of the functorial value in the pipeline
///
/// # Examples
///
/// ```rust
/// use rustica::utils::transform_utils::Pipeline;
/// use rustica::prelude::*;
///
/// // Create a pipeline with an Option value
/// let pipeline = Pipeline::new(Some(5));
///
/// // Apply a series of transformations
/// let result = pipeline
/// .map(|&x| x * 2) // Double the value
/// .map(|x| x.to_string()) // Convert to string
/// .extract();
///
/// assert_eq!(result, Some("10".to_string()));
///
/// // Works with Either as well
/// use rustica::datatypes::either::Either;
///
/// let err_pipeline = Pipeline::new(Either::<&str, i32>::left("error"));
/// let err_result = err_pipeline
/// .map(|&x| x * 2)
/// .extract();
///
/// assert_eq!(err_result, Either::left("error"));
///
/// let ok_pipeline = Pipeline::new(Either::<&str, i32>::right(7));
/// let ok_result = ok_pipeline
/// .map(|&x| x * 2)
/// .extract();
///
/// assert_eq!(ok_result, Either::right(14));
/// ```
;