Skip to main content

fp_library/types/
result.rs

1//! Functional programming trait implementations for the standard library [`Result`] type.
2//!
3//! Extends `Result` with dual functor/monad instances: [`ResultErrAppliedBrand`](crate::brands::ResultErrAppliedBrand) (standard Result monad) functors over the success value, while [`ResultOkAppliedBrand`](crate::brands::ResultOkAppliedBrand) functors over the error value.
4
5#[fp_macros::document_module]
6mod inner {
7	use {
8		crate::{
9			Apply,
10			brands::{
11				ResultBrand,
12				ResultErrAppliedBrand,
13				ResultOkAppliedBrand,
14			},
15			classes::{
16				Applicative,
17				ApplyFirst,
18				ApplySecond,
19				Bifoldable,
20				Bifunctor,
21				Bitraversable,
22				CloneableFn,
23				Foldable,
24				Functor,
25				Lift,
26				MonadRec,
27				Monoid,
28				Pointed,
29				Semiapplicative,
30				Semimonad,
31				Traversable,
32			},
33			impl_kind,
34			kinds::*,
35		},
36		core::ops::ControlFlow,
37		fp_macros::*,
38	};
39
40	impl_kind! {
41		/// HKT branding for the `Result` type.
42		///
43		/// The type parameters for `Of` are ordered `E`, then `A` (Error, then Success).
44		/// This follows functional programming conventions (like Haskell's `Either e a`)
45		/// where the right-most type parameter is the "success" value, allowing the
46		/// type to form a `Monad` over the success type by fixing the error type.
47		for ResultBrand {
48			type Of<A, B> = Result<B, A>;
49		}
50	}
51
52	impl_kind! {
53		/// HKT branding for the `Result` type with lifetimes.
54		///
55		/// The type parameters for `Of` are ordered `E`, then `A` (Error, then Success).
56		for ResultBrand {
57			type Of<'a, A: 'a, B: 'a>: 'a = Result<B, A>;
58		}
59	}
60
61	impl Bifunctor for ResultBrand {
62		/// Maps functions over the values in the result.
63		///
64		/// This method applies one function to the error value and another to the success value.
65		#[document_signature]
66		///
67		#[document_type_parameters(
68			"The lifetime of the values.",
69			"The type of the error value.",
70			"The type of the mapped error value.",
71			"The type of the success value.",
72			"The type of the mapped success value."
73		)]
74		///
75		#[document_parameters(
76			"The function to apply to the error.",
77			"The function to apply to the success.",
78			"The result to map over."
79		)]
80		///
81		#[document_returns("A new result containing the mapped values.")]
82		#[document_examples]
83		///
84		/// ```
85		/// use fp_library::{
86		/// 	brands::*,
87		/// 	classes::bifunctor::*,
88		/// 	functions::*,
89		/// };
90		///
91		/// let x: Result<i32, i32> = Ok(5);
92		/// assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x), Ok(10));
93		///
94		/// let y: Result<i32, i32> = Err(5);
95		/// assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, y), Err(6));
96		/// ```
97		fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
98			f: impl Fn(A) -> B + 'a,
99			g: impl Fn(C) -> D + 'a,
100			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
101		) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
102			match p {
103				Ok(c) => Ok(g(c)),
104				Err(a) => Err(f(a)),
105			}
106		}
107	}
108
109	impl Bifoldable for ResultBrand {
110		/// Folds a result using two step functions, right-associatively.
111		///
112		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values.
113		#[document_signature]
114		///
115		#[document_type_parameters(
116			"The lifetime of the values.",
117			"The brand of the cloneable function to use.",
118			"The error type (first position).",
119			"The success type (second position).",
120			"The accumulator type."
121		)]
122		///
123		#[document_parameters(
124			"The step function applied to the error value.",
125			"The step function applied to the success value.",
126			"The initial accumulator.",
127			"The result to fold."
128		)]
129		///
130		#[document_returns("`f(a, z)` for `Err(a)`, or `g(b, z)` for `Ok(b)`.")]
131		#[document_examples]
132		///
133		/// ```
134		/// use fp_library::{
135		/// 	brands::*,
136		/// 	functions::*,
137		/// };
138		///
139		/// assert_eq!(
140		/// 	bi_fold_right::<RcFnBrand, ResultBrand, _, _, _>(
141		/// 		|e: i32, acc| acc - e,
142		/// 		|s: i32, acc| acc + s,
143		/// 		10,
144		/// 		Err(3),
145		/// 	),
146		/// 	7
147		/// );
148		/// assert_eq!(
149		/// 	bi_fold_right::<RcFnBrand, ResultBrand, _, _, _>(
150		/// 		|e: i32, acc| acc - e,
151		/// 		|s: i32, acc| acc + s,
152		/// 		10,
153		/// 		Ok(5),
154		/// 	),
155		/// 	15
156		/// );
157		/// ```
158		fn bi_fold_right<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
159			f: impl Fn(A, C) -> C + 'a,
160			g: impl Fn(B, C) -> C + 'a,
161			z: C,
162			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
163		) -> C {
164			match p {
165				Err(a) => f(a, z),
166				Ok(b) => g(b, z),
167			}
168		}
169
170		/// Folds a result using two step functions, left-associatively.
171		///
172		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values.
173		#[document_signature]
174		///
175		#[document_type_parameters(
176			"The lifetime of the values.",
177			"The brand of the cloneable function to use.",
178			"The error type (first position).",
179			"The success type (second position).",
180			"The accumulator type."
181		)]
182		///
183		#[document_parameters(
184			"The step function applied to the error value.",
185			"The step function applied to the success value.",
186			"The initial accumulator.",
187			"The result to fold."
188		)]
189		///
190		#[document_returns("`f(z, a)` for `Err(a)`, or `g(z, b)` for `Ok(b)`.")]
191		#[document_examples]
192		///
193		/// ```
194		/// use fp_library::{
195		/// 	brands::*,
196		/// 	functions::*,
197		/// };
198		///
199		/// assert_eq!(
200		/// 	bi_fold_left::<RcFnBrand, ResultBrand, _, _, _>(
201		/// 		|acc, e: i32| acc - e,
202		/// 		|acc, s: i32| acc + s,
203		/// 		10,
204		/// 		Err(3),
205		/// 	),
206		/// 	7
207		/// );
208		/// assert_eq!(
209		/// 	bi_fold_left::<RcFnBrand, ResultBrand, _, _, _>(
210		/// 		|acc, e: i32| acc - e,
211		/// 		|acc, s: i32| acc + s,
212		/// 		10,
213		/// 		Ok(5),
214		/// 	),
215		/// 	15
216		/// );
217		/// ```
218		fn bi_fold_left<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
219			f: impl Fn(C, A) -> C + 'a,
220			g: impl Fn(C, B) -> C + 'a,
221			z: C,
222			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
223		) -> C {
224			match p {
225				Err(a) => f(z, a),
226				Ok(b) => g(z, b),
227			}
228		}
229
230		/// Maps a result's value to a monoid using two functions and returns the result.
231		///
232		/// Dispatches to `f` for `Err(a)` and `g` for `Ok(b)`, returning the monoid value.
233		#[document_signature]
234		///
235		#[document_type_parameters(
236			"The lifetime of the values.",
237			"The brand of the cloneable function to use.",
238			"The error type (first position).",
239			"The success type (second position).",
240			"The monoid type."
241		)]
242		///
243		#[document_parameters(
244			"The function mapping the error to the monoid.",
245			"The function mapping the success to the monoid.",
246			"The result to fold."
247		)]
248		///
249		#[document_returns("`f(a)` for `Err(a)`, or `g(b)` for `Ok(b)`.")]
250		#[document_examples]
251		///
252		/// ```
253		/// use fp_library::{
254		/// 	brands::*,
255		/// 	functions::*,
256		/// };
257		///
258		/// assert_eq!(
259		/// 	bi_fold_map::<RcFnBrand, ResultBrand, _, _, _>(
260		/// 		|e: i32| e.to_string(),
261		/// 		|s: i32| s.to_string(),
262		/// 		Err::<i32, i32>(3),
263		/// 	),
264		/// 	"3".to_string()
265		/// );
266		/// assert_eq!(
267		/// 	bi_fold_map::<RcFnBrand, ResultBrand, _, _, _>(
268		/// 		|e: i32| e.to_string(),
269		/// 		|s: i32| s.to_string(),
270		/// 		Ok::<i32, i32>(5),
271		/// 	),
272		/// 	"5".to_string()
273		/// );
274		/// ```
275		fn bi_fold_map<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
276			f: impl Fn(A) -> M + 'a,
277			g: impl Fn(B) -> M + 'a,
278			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
279		) -> M
280		where
281			M: Monoid + 'a, {
282			match p {
283				Err(a) => f(a),
284				Ok(b) => g(b),
285			}
286		}
287	}
288
289	impl Bitraversable for ResultBrand {
290		/// Traverses a result with two effectful functions.
291		///
292		/// Dispatches to `f` for `Err(a)` values and `g` for `Ok(b)` values,
293		/// wrapping the result in the applicative context.
294		#[document_signature]
295		///
296		#[document_type_parameters(
297			"The lifetime of the values.",
298			"The error type (first position).",
299			"The success type (second position).",
300			"The output error type.",
301			"The output success type.",
302			"The applicative context."
303		)]
304		///
305		#[document_parameters(
306			"The function applied to the error value.",
307			"The function applied to the success value.",
308			"The result to traverse."
309		)]
310		///
311		#[document_returns(
312			"`f(a)` wrapped in context for `Err(a)`, or `g(b)` wrapped in context for `Ok(b)`."
313		)]
314		#[document_examples]
315		///
316		/// ```
317		/// use fp_library::{
318		/// 	brands::*,
319		/// 	functions::*,
320		/// };
321		///
322		/// assert_eq!(
323		/// 	bi_traverse::<ResultBrand, _, _, _, _, OptionBrand>(
324		/// 		|e: i32| Some(e + 1),
325		/// 		|s: i32| Some(s * 2),
326		/// 		Err::<i32, i32>(3),
327		/// 	),
328		/// 	Some(Err(4))
329		/// );
330		/// assert_eq!(
331		/// 	bi_traverse::<ResultBrand, _, _, _, _, OptionBrand>(
332		/// 		|e: i32| Some(e + 1),
333		/// 		|s: i32| Some(s * 2),
334		/// 		Ok::<i32, i32>(5),
335		/// 	),
336		/// 	Some(Ok(10))
337		/// );
338		/// ```
339		fn bi_traverse<
340			'a,
341			A: 'a + Clone,
342			B: 'a + Clone,
343			C: 'a + Clone,
344			D: 'a + Clone,
345			F: Applicative,
346		>(
347			f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
348			g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
349			p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
350		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>)>)
351		{
352			match p {
353				Err(a) => F::map(|c| Err(c), f(a)),
354				Ok(b) => F::map(|d| Ok(d), g(b)),
355			}
356		}
357	}
358
359	// ResultErrAppliedBrand<E> (Functor over T)
360
361	impl_kind! {
362		#[document_type_parameters("The error type.")]
363		impl<E: 'static> for ResultErrAppliedBrand<E> {
364			type Of<'a, A: 'a>: 'a = Result<A, E>;
365		}
366	}
367
368	#[document_type_parameters("The error type.")]
369	impl<E: 'static> Functor for ResultErrAppliedBrand<E> {
370		/// Maps a function over the value in the result.
371		///
372		/// This method applies a function to the value inside the result if it is `Ok`, producing a new result with the transformed value. If the result is `Err`, it is returned unchanged.
373		#[document_signature]
374		///
375		#[document_type_parameters(
376			"The lifetime of the values.",
377			"The type of the value inside the result.",
378			"The type of the result of applying the function."
379		)]
380		///
381		#[document_parameters("The function to apply.", "The result to map over.")]
382		///
383		#[document_returns(
384			"A new result containing the result of applying the function, or the original error."
385		)]
386		///
387		#[document_examples]
388		///
389		/// ```
390		/// use fp_library::{
391		/// 	brands::*,
392		/// 	functions::*,
393		/// };
394		///
395		/// assert_eq!(map::<ResultErrAppliedBrand<()>, _, _>(|x: i32| x * 2, Ok(5)), Ok(10));
396		/// assert_eq!(map::<ResultErrAppliedBrand<i32>, _, _>(|x: i32| x * 2, Err(1)), Err(1));
397		/// ```
398		fn map<'a, A: 'a, B: 'a>(
399			func: impl Fn(A) -> B + 'a,
400			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
401		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
402			fa.map(func)
403		}
404	}
405
406	#[document_type_parameters("The error type.")]
407	impl<E: Clone + 'static> Lift for ResultErrAppliedBrand<E> {
408		/// Lifts a binary function into the result context.
409		///
410		/// This method lifts a binary function to operate on values within the result context.
411		#[document_signature]
412		///
413		#[document_type_parameters(
414			"The lifetime of the values.",
415			"The type of the first value.",
416			"The type of the second value.",
417			"The type of the result."
418		)]
419		///
420		#[document_parameters(
421			"The binary function to apply.",
422			"The first result.",
423			"The second result."
424		)]
425		///
426		#[document_returns(
427			"`Ok(f(a, b))` if both results are `Ok`, otherwise the first error encountered."
428		)]
429		#[document_examples]
430		///
431		/// ```
432		/// use fp_library::{
433		/// 	brands::*,
434		/// 	functions::*,
435		/// };
436		///
437		/// assert_eq!(
438		/// 	lift2::<ResultErrAppliedBrand<()>, _, _, _>(|x: i32, y: i32| x + y, Ok(1), Ok(2)),
439		/// 	Ok(3)
440		/// );
441		/// assert_eq!(
442		/// 	lift2::<ResultErrAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Ok(1), Err(2)),
443		/// 	Err(2)
444		/// );
445		/// assert_eq!(
446		/// 	lift2::<ResultErrAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Err(1), Ok(2)),
447		/// 	Err(1)
448		/// );
449		/// assert_eq!(
450		/// 	lift2::<ResultErrAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Err(1), Err(2)),
451		/// 	Err(1)
452		/// );
453		/// ```
454		fn lift2<'a, A, B, C>(
455			func: impl Fn(A, B) -> C + 'a,
456			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
457			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
458		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
459		where
460			A: Clone + 'a,
461			B: Clone + 'a,
462			C: 'a, {
463			match (fa, fb) {
464				(Ok(a), Ok(b)) => Ok(func(a, b)),
465				(Err(e), _) => Err(e),
466				(_, Err(e)) => Err(e),
467			}
468		}
469	}
470
471	#[document_type_parameters("The error type.")]
472	impl<E: 'static> Pointed for ResultErrAppliedBrand<E> {
473		/// Wraps a value in a result.
474		///
475		/// This method wraps a value in the `Ok` variant of a `Result`.
476		#[document_signature]
477		///
478		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
479		///
480		#[document_parameters("The value to wrap.")]
481		///
482		#[document_returns("`Ok(a)`.")]
483		///
484		#[document_examples]
485		///
486		/// ```
487		/// use fp_library::{
488		/// 	brands::ResultErrAppliedBrand,
489		/// 	functions::*,
490		/// };
491		///
492		/// assert_eq!(pure::<ResultErrAppliedBrand<()>, _>(5), Ok(5));
493		/// ```
494		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
495			Ok(a)
496		}
497	}
498
499	#[document_type_parameters("The error type.")]
500	impl<E: Clone + 'static> ApplyFirst for ResultErrAppliedBrand<E> {}
501
502	#[document_type_parameters("The error type.")]
503	impl<E: Clone + 'static> ApplySecond for ResultErrAppliedBrand<E> {}
504
505	#[document_type_parameters("The error type.")]
506	impl<E: Clone + 'static> Semiapplicative for ResultErrAppliedBrand<E> {
507		/// Applies a wrapped function to a wrapped value.
508		///
509		/// This method applies a function wrapped in a result to a value wrapped in a result.
510		#[document_signature]
511		///
512		#[document_type_parameters(
513			"The lifetime of the values.",
514			"The brand of the cloneable function wrapper.",
515			"The type of the input value.",
516			"The type of the output value."
517		)]
518		///
519		#[document_parameters(
520			"The result containing the function.",
521			"The result containing the value."
522		)]
523		///
524		#[document_returns("`Ok(f(a))` if both are `Ok`, otherwise the first error encountered.")]
525		#[document_examples]
526		///
527		/// ```
528		/// use fp_library::{
529		/// 	Apply,
530		/// 	Kind,
531		/// 	brands::*,
532		/// 	classes::*,
533		/// 	functions::*,
534		/// };
535		///
536		/// let f: Result<_, ()> = Ok(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
537		/// assert_eq!(apply::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(f, Ok(5)), Ok(10));
538		/// let f: Result<_, i32> = Ok(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
539		/// assert_eq!(apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, Err(1)), Err(1));
540		///
541		/// let f_err: Result<_, i32> = Err(1);
542		/// assert_eq!(apply::<RcFnBrand, ResultErrAppliedBrand<i32>, i32, i32>(f_err, Ok(5)), Err(1));
543		/// ```
544		fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
545			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
546			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
547		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
548			match (ff, fa) {
549				(Ok(f), Ok(a)) => Ok(f(a)),
550				(Err(e), _) => Err(e),
551				(_, Err(e)) => Err(e),
552			}
553		}
554	}
555
556	#[document_type_parameters("The error type.")]
557	impl<E: Clone + 'static> Semimonad for ResultErrAppliedBrand<E> {
558		/// Chains result computations.
559		///
560		/// This method chains two computations, where the second computation depends on the result of the first.
561		#[document_signature]
562		///
563		#[document_type_parameters(
564			"The lifetime of the values.",
565			"The type of the result of the first computation.",
566			"The type of the result of the second computation."
567		)]
568		///
569		#[document_parameters(
570			"The first result.",
571			"The function to apply to the value inside the result."
572		)]
573		///
574		#[document_returns(
575			"The result of applying `f` to the value if `ma` is `Ok`, otherwise the original error."
576		)]
577		#[document_examples]
578		///
579		/// ```
580		/// use fp_library::{
581		/// 	brands::ResultErrAppliedBrand,
582		/// 	functions::*,
583		/// };
584		///
585		/// assert_eq!(bind::<ResultErrAppliedBrand<()>, _, _>(Ok(5), |x| Ok(x * 2)), Ok(10));
586		/// assert_eq!(bind::<ResultErrAppliedBrand<i32>, _, _>(Ok(5), |_| Err::<i32, _>(1)), Err(1));
587		/// assert_eq!(bind::<ResultErrAppliedBrand<i32>, _, _>(Err(1), |x: i32| Ok(x * 2)), Err(1));
588		/// ```
589		fn bind<'a, A: 'a, B: 'a>(
590			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
591			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
592		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
593			ma.and_then(func)
594		}
595	}
596
597	#[document_type_parameters("The error type.")]
598	impl<E: 'static> Foldable for ResultErrAppliedBrand<E> {
599		/// Folds the result from the right.
600		///
601		/// This method performs a right-associative fold of the result.
602		#[document_signature]
603		///
604		#[document_type_parameters(
605			"The lifetime of the values.",
606			"The brand of the cloneable function to use.",
607			"The type of the elements in the structure.",
608			"The type of the accumulator."
609		)]
610		///
611		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
612		///
613		#[document_returns("`func(a, initial)` if `fa` is `Ok(a)`, otherwise `initial`.")]
614		///
615		#[document_examples]
616		///
617		/// ```
618		/// use fp_library::{
619		/// 	brands::*,
620		/// 	functions::*,
621		/// };
622		///
623		/// assert_eq!(
624		/// 	fold_right::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(|x, acc| x + acc, 0, Ok(5)),
625		/// 	5
626		/// );
627		/// assert_eq!(
628		/// 	fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(|x: i32, acc| x + acc, 0, Err(1)),
629		/// 	0
630		/// );
631		/// ```
632		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
633			func: impl Fn(A, B) -> B + 'a,
634			initial: B,
635			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
636		) -> B
637		where
638			FnBrand: CloneableFn + 'a, {
639			match fa {
640				Ok(a) => func(a, initial),
641				Err(_) => initial,
642			}
643		}
644
645		/// Folds the result from the left.
646		///
647		/// This method performs a left-associative fold of the result.
648		#[document_signature]
649		///
650		#[document_type_parameters(
651			"The lifetime of the values.",
652			"The brand of the cloneable function to use.",
653			"The type of the elements in the structure.",
654			"The type of the accumulator."
655		)]
656		///
657		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
658		///
659		#[document_returns("`func(initial, a)` if `fa` is `Ok(a)`, otherwise `initial`.")]
660		///
661		#[document_examples]
662		///
663		/// ```
664		/// use fp_library::{
665		/// 	brands::*,
666		/// 	functions::*,
667		/// };
668		///
669		/// assert_eq!(
670		/// 	fold_left::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(|acc, x| acc + x, 0, Ok(5)),
671		/// 	5
672		/// );
673		/// assert_eq!(
674		/// 	fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(|acc, x: i32| acc + x, 0, Err(1)),
675		/// 	0
676		/// );
677		/// ```
678		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
679			func: impl Fn(B, A) -> B + 'a,
680			initial: B,
681			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
682		) -> B
683		where
684			FnBrand: CloneableFn + 'a, {
685			match fa {
686				Ok(a) => func(initial, a),
687				Err(_) => initial,
688			}
689		}
690
691		/// Maps the value to a monoid and returns it.
692		///
693		/// This method maps the element of the result to a monoid and then returns it.
694		#[document_signature]
695		///
696		#[document_type_parameters(
697			"The lifetime of the values.",
698			"The brand of the cloneable function to use.",
699			"The type of the elements in the structure.",
700			"The type of the monoid."
701		)]
702		///
703		#[document_parameters("The mapping function.", "The result to fold.")]
704		///
705		#[document_returns("`func(a)` if `fa` is `Ok(a)`, otherwise `M::empty()`.")]
706		///
707		#[document_examples]
708		///
709		/// ```
710		/// use fp_library::{
711		/// 	brands::*,
712		/// 	functions::*,
713		/// };
714		///
715		/// assert_eq!(
716		/// 	fold_map::<RcFnBrand, ResultErrAppliedBrand<()>, _, _>(|x: i32| x.to_string(), Ok(5)),
717		/// 	"5".to_string()
718		/// );
719		/// assert_eq!(
720		/// 	fold_map::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(|x: i32| x.to_string(), Err(1)),
721		/// 	"".to_string()
722		/// );
723		/// ```
724		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
725			func: impl Fn(A) -> M + 'a,
726			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
727		) -> M
728		where
729			M: Monoid + 'a,
730			FnBrand: CloneableFn + 'a, {
731			match fa {
732				Ok(a) => func(a),
733				Err(_) => M::empty(),
734			}
735		}
736	}
737
738	#[document_type_parameters("The error type.")]
739	impl<E: Clone + 'static> Traversable for ResultErrAppliedBrand<E> {
740		/// Traverses the result with an applicative function.
741		///
742		/// This method maps the element of the result to a computation, evaluates it, and combines the result into an applicative context.
743		#[document_signature]
744		///
745		#[document_type_parameters(
746			"The lifetime of the values.",
747			"The type of the elements in the traversable structure.",
748			"The type of the elements in the resulting traversable structure.",
749			"The applicative context."
750		)]
751		///
752		#[document_parameters("The function to apply.", "The result to traverse.")]
753		///
754		#[document_returns("The result wrapped in the applicative context.")]
755		///
756		#[document_examples]
757		///
758		/// ```
759		/// use fp_library::{
760		/// 	brands::{
761		/// 		OptionBrand,
762		/// 		ResultErrAppliedBrand,
763		/// 	},
764		/// 	functions::*,
765		/// };
766		///
767		/// assert_eq!(
768		/// 	traverse::<ResultErrAppliedBrand<()>, _, _, OptionBrand>(|x| Some(x * 2), Ok(5)),
769		/// 	Some(Ok(10))
770		/// );
771		/// assert_eq!(
772		/// 	traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(|x: i32| Some(x * 2), Err(1)),
773		/// 	Some(Err(1))
774		/// );
775		/// assert_eq!(
776		/// 	traverse::<ResultErrAppliedBrand<()>, _, _, OptionBrand>(|_| None::<i32>, Ok(5)),
777		/// 	None
778		/// );
779		/// ```
780		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
781			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
782			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
783		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
784		where
785			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
786			match ta {
787				Ok(a) => F::map(|b| Ok(b), func(a)),
788				Err(e) => F::pure(Err(e)),
789			}
790		}
791
792		/// Sequences a result of applicative.
793		///
794		/// This method evaluates the computation inside the result and accumulates the result into an applicative context.
795		#[document_signature]
796		///
797		#[document_type_parameters(
798			"The lifetime of the values.",
799			"The type of the elements in the traversable structure.",
800			"The applicative context."
801		)]
802		///
803		#[document_parameters("The result containing the applicative value.")]
804		///
805		#[document_returns("The result wrapped in the applicative context.")]
806		///
807		#[document_examples]
808		///
809		/// ```
810		/// use fp_library::{
811		/// 	brands::{
812		/// 		OptionBrand,
813		/// 		ResultErrAppliedBrand,
814		/// 	},
815		/// 	functions::*,
816		/// };
817		///
818		/// assert_eq!(sequence::<ResultErrAppliedBrand<()>, _, OptionBrand>(Ok(Some(5))), Some(Ok(5)));
819		/// assert_eq!(
820		/// 	sequence::<ResultErrAppliedBrand<i32>, i32, OptionBrand>(Err::<Option<i32>, _>(1)),
821		/// 	Some(Err::<i32, i32>(1))
822		/// );
823		/// assert_eq!(sequence::<ResultErrAppliedBrand<()>, _, OptionBrand>(Ok(None::<i32>)), None);
824		/// ```
825		fn sequence<'a, A: 'a + Clone, F: Applicative>(
826			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
827		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
828		where
829			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
830			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
831			match ta {
832				Ok(fa) => F::map(|a| Ok(a), fa),
833				Err(e) => F::pure(Err(e)),
834			}
835		}
836	}
837
838	// ResultOkAppliedBrand<T> (Functor over E)
839
840	impl_kind! {
841		#[document_type_parameters("The success type.")]
842		impl<T: 'static> for ResultOkAppliedBrand<T> {
843			type Of<'a, A: 'a>: 'a = Result<T, A>;
844		}
845	}
846
847	#[document_type_parameters("The success type.")]
848	impl<T: 'static> Functor for ResultOkAppliedBrand<T> {
849		/// Maps a function over the error value in the result.
850		///
851		/// This method applies a function to the error value inside the result if it is `Err`, producing a new result with the transformed error. If the result is `Ok`, it is returned unchanged.
852		#[document_signature]
853		///
854		#[document_type_parameters(
855			"The lifetime of the values.",
856			"The type of the error value inside the result.",
857			"The type of the result of applying the function."
858		)]
859		///
860		#[document_parameters("The function to apply to the error.", "The result to map over.")]
861		///
862		#[document_returns(
863			"A new result containing the mapped error, or the original success value."
864		)]
865		///
866		#[document_examples]
867		///
868		/// ```
869		/// use fp_library::{
870		/// 	brands::*,
871		/// 	functions::*,
872		/// };
873		///
874		/// assert_eq!(map::<ResultOkAppliedBrand<i32>, _, _>(|x: i32| x * 2, Err(5)), Err(10));
875		/// assert_eq!(map::<ResultOkAppliedBrand<i32>, _, _>(|x: i32| x * 2, Ok(1)), Ok(1));
876		/// ```
877		fn map<'a, A: 'a, B: 'a>(
878			func: impl Fn(A) -> B + 'a,
879			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
880		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
881			match fa {
882				Ok(t) => Ok(t),
883				Err(e) => Err(func(e)),
884			}
885		}
886	}
887
888	#[document_type_parameters("The success type.")]
889	impl<T: Clone + 'static> Lift for ResultOkAppliedBrand<T> {
890		/// Lifts a binary function into the result context (over error).
891		///
892		/// This method lifts a binary function to operate on error values within the result context.
893		#[document_signature]
894		///
895		#[document_type_parameters(
896			"The lifetime of the values.",
897			"The type of the first error value.",
898			"The type of the second error value.",
899			"The type of the result error value."
900		)]
901		///
902		#[document_parameters(
903			"The binary function to apply to the errors.",
904			"The first result.",
905			"The second result."
906		)]
907		///
908		#[document_returns(
909			"`Err(f(a, b))` if both results are `Err`, otherwise the first success encountered."
910		)]
911		#[document_examples]
912		///
913		/// ```
914		/// use fp_library::{
915		/// 	brands::*,
916		/// 	functions::*,
917		/// };
918		///
919		/// assert_eq!(
920		/// 	lift2::<ResultOkAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Err(1), Err(2)),
921		/// 	Err(3)
922		/// );
923		/// assert_eq!(
924		/// 	lift2::<ResultOkAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Err(1), Ok(2)),
925		/// 	Ok(2)
926		/// );
927		/// assert_eq!(
928		/// 	lift2::<ResultOkAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Ok(1), Err(2)),
929		/// 	Ok(1)
930		/// );
931		/// assert_eq!(
932		/// 	lift2::<ResultOkAppliedBrand<i32>, _, _, _>(|x: i32, y: i32| x + y, Ok(1), Ok(2)),
933		/// 	Ok(1)
934		/// );
935		/// ```
936		fn lift2<'a, A, B, C>(
937			func: impl Fn(A, B) -> C + 'a,
938			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
939			fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
940		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
941		where
942			A: Clone + 'a,
943			B: Clone + 'a,
944			C: 'a, {
945			match (fa, fb) {
946				(Err(a), Err(b)) => Err(func(a, b)),
947				(Ok(t), _) => Ok(t),
948				(_, Ok(t)) => Ok(t),
949			}
950		}
951	}
952
953	#[document_type_parameters("The success type.")]
954	impl<T: 'static> Pointed for ResultOkAppliedBrand<T> {
955		/// Wraps a value in a result (as error).
956		///
957		/// This method wraps a value in the `Err` variant of a `Result`.
958		#[document_signature]
959		///
960		#[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
961		///
962		#[document_parameters("The value to wrap.")]
963		///
964		#[document_returns("`Err(a)`.")]
965		///
966		#[document_examples]
967		///
968		/// ```
969		/// use fp_library::{
970		/// 	brands::ResultOkAppliedBrand,
971		/// 	functions::*,
972		/// };
973		///
974		/// assert_eq!(pure::<ResultOkAppliedBrand<()>, _>(5), Err(5));
975		/// ```
976		fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
977			Err(a)
978		}
979	}
980
981	#[document_type_parameters("The success type.")]
982	impl<T: Clone + 'static> ApplyFirst for ResultOkAppliedBrand<T> {}
983
984	#[document_type_parameters("The success type.")]
985	impl<T: Clone + 'static> ApplySecond for ResultOkAppliedBrand<T> {}
986
987	#[document_type_parameters("The success type.")]
988	impl<T: Clone + 'static> Semiapplicative for ResultOkAppliedBrand<T> {
989		/// Applies a wrapped function to a wrapped value (over error).
990		///
991		/// This method applies a function wrapped in a result (as error) to a value wrapped in a result (as error).
992		#[document_signature]
993		///
994		#[document_type_parameters(
995			"The lifetime of the values.",
996			"The brand of the cloneable function wrapper.",
997			"The type of the input value.",
998			"The type of the output value."
999		)]
1000		///
1001		#[document_parameters(
1002			"The result containing the function (in Err).",
1003			"The result containing the value (in Err)."
1004		)]
1005		///
1006		#[document_returns(
1007			"`Err(f(a))` if both are `Err`, otherwise the first success encountered."
1008		)]
1009		#[document_examples]
1010		///
1011		/// ```
1012		/// use fp_library::{
1013		/// 	Apply,
1014		/// 	Kind,
1015		/// 	brands::*,
1016		/// 	classes::*,
1017		/// 	functions::*,
1018		/// };
1019		///
1020		/// let f: Result<(), _> = Err(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1021		/// assert_eq!(apply::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(f, Err(5)), Err(10));
1022		/// let f: Result<i32, _> = Err(cloneable_fn_new::<RcFnBrand, _, _>(|x: i32| x * 2));
1023		/// assert_eq!(apply::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, Ok(1)), Ok(1));
1024		///
1025		/// let f_ok: Result<i32, _> = Ok(1);
1026		/// assert_eq!(apply::<RcFnBrand, ResultOkAppliedBrand<i32>, i32, i32>(f_ok, Err(5)), Ok(1));
1027		/// ```
1028		fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
1029			ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
1030			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1031		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1032			match (ff, fa) {
1033				(Err(f), Err(a)) => Err(f(a)),
1034				(Ok(t), _) => Ok(t),
1035				(_, Ok(t)) => Ok(t),
1036			}
1037		}
1038	}
1039
1040	#[document_type_parameters("The success type.")]
1041	impl<T: Clone + 'static> Semimonad for ResultOkAppliedBrand<T> {
1042		/// Chains result computations (over error).
1043		///
1044		/// This method chains two computations, where the second computation depends on the result of the first (over error).
1045		#[document_signature]
1046		///
1047		#[document_type_parameters(
1048			"The lifetime of the values.",
1049			"The type of the result of the first computation.",
1050			"The type of the result of the second computation."
1051		)]
1052		///
1053		#[document_parameters("The first result.", "The function to apply to the error value.")]
1054		///
1055		#[document_returns(
1056			"The result of applying `f` to the error if `ma` is `Err`, otherwise the original success."
1057		)]
1058		///
1059		#[document_examples]
1060		///
1061		/// ```
1062		/// use fp_library::{
1063		/// 	brands::ResultOkAppliedBrand,
1064		/// 	functions::*,
1065		/// };
1066		///
1067		/// assert_eq!(bind::<ResultOkAppliedBrand<()>, _, _>(Err(5), |x| Err(x * 2)), Err(10));
1068		/// assert_eq!(bind::<ResultOkAppliedBrand<i32>, _, _>(Err(5), |_| Ok::<_, i32>(1)), Ok(1));
1069		/// assert_eq!(bind::<ResultOkAppliedBrand<i32>, _, _>(Ok(1), |x: i32| Err(x * 2)), Ok(1));
1070		/// ```
1071		fn bind<'a, A: 'a, B: 'a>(
1072			ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1073			func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1074		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1075			match ma {
1076				Ok(t) => Ok(t),
1077				Err(e) => func(e),
1078			}
1079		}
1080	}
1081
1082	#[document_type_parameters("The success type.")]
1083	impl<T: 'static> Foldable for ResultOkAppliedBrand<T> {
1084		/// Folds the result from the right (over error).
1085		///
1086		/// This method performs a right-associative fold of the result (over error).
1087		#[document_signature]
1088		///
1089		#[document_type_parameters(
1090			"The lifetime of the values.",
1091			"The brand of the cloneable function to use.",
1092			"The type of the elements in the structure.",
1093			"The type of the accumulator."
1094		)]
1095		///
1096		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1097		///
1098		#[document_returns("`func(a, initial)` if `fa` is `Err(a)`, otherwise `initial`.")]
1099		///
1100		#[document_examples]
1101		///
1102		/// ```
1103		/// use fp_library::{
1104		/// 	brands::*,
1105		/// 	functions::*,
1106		/// };
1107		///
1108		/// assert_eq!(
1109		/// 	fold_right::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _>(|x: i32, acc| x + acc, 0, Err(1)),
1110		/// 	1
1111		/// );
1112		/// assert_eq!(
1113		/// 	fold_right::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(|x: i32, acc| x + acc, 0, Ok(())),
1114		/// 	0
1115		/// );
1116		/// ```
1117		fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1118			func: impl Fn(A, B) -> B + 'a,
1119			initial: B,
1120			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1121		) -> B
1122		where
1123			FnBrand: CloneableFn + 'a, {
1124			match fa {
1125				Err(e) => func(e, initial),
1126				Ok(_) => initial,
1127			}
1128		}
1129
1130		/// Folds the result from the left (over error).
1131		///
1132		/// This method performs a left-associative fold of the result (over error).
1133		#[document_signature]
1134		///
1135		#[document_type_parameters(
1136			"The lifetime of the values.",
1137			"The brand of the cloneable function to use.",
1138			"The type of the elements in the structure.",
1139			"The type of the accumulator."
1140		)]
1141		///
1142		#[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1143		///
1144		#[document_returns("`func(initial, a)` if `fa` is `Err(a)`, otherwise `initial`.")]
1145		///
1146		#[document_examples]
1147		///
1148		/// ```
1149		/// use fp_library::{
1150		/// 	brands::*,
1151		/// 	functions::*,
1152		/// };
1153		///
1154		/// assert_eq!(
1155		/// 	fold_left::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(|acc, x: i32| acc + x, 0, Err(5)),
1156		/// 	5
1157		/// );
1158		/// assert_eq!(
1159		/// 	fold_left::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _>(|acc, x: i32| acc + x, 0, Ok(1)),
1160		/// 	0
1161		/// );
1162		/// ```
1163		fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1164			func: impl Fn(B, A) -> B + 'a,
1165			initial: B,
1166			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1167		) -> B
1168		where
1169			FnBrand: CloneableFn + 'a, {
1170			match fa {
1171				Err(e) => func(initial, e),
1172				Ok(_) => initial,
1173			}
1174		}
1175
1176		/// Maps the value to a monoid and returns it (over error).
1177		///
1178		/// This method maps the element of the result to a monoid and then returns it (over error).
1179		#[document_signature]
1180		///
1181		#[document_type_parameters(
1182			"The lifetime of the values.",
1183			"The brand of the cloneable function to use.",
1184			"The type of the elements in the structure.",
1185			"The type of the monoid."
1186		)]
1187		///
1188		#[document_parameters("The mapping function.", "The result to fold.")]
1189		///
1190		#[document_returns("`func(a)` if `fa` is `Err(a)`, otherwise `M::empty()`.")]
1191		///
1192		#[document_examples]
1193		///
1194		/// ```
1195		/// use fp_library::{
1196		/// 	brands::*,
1197		/// 	functions::*,
1198		/// };
1199		///
1200		/// assert_eq!(
1201		/// 	fold_map::<RcFnBrand, ResultOkAppliedBrand<()>, _, _>(|x: i32| x.to_string(), Err(5)),
1202		/// 	"5".to_string()
1203		/// );
1204		/// assert_eq!(
1205		/// 	fold_map::<RcFnBrand, ResultOkAppliedBrand<i32>, _, _>(|x: i32| x.to_string(), Ok(1)),
1206		/// 	"".to_string()
1207		/// );
1208		/// ```
1209		fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1210			func: impl Fn(A) -> M + 'a,
1211			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1212		) -> M
1213		where
1214			M: Monoid + 'a,
1215			FnBrand: CloneableFn + 'a, {
1216			match fa {
1217				Err(e) => func(e),
1218				Ok(_) => M::empty(),
1219			}
1220		}
1221	}
1222
1223	#[document_type_parameters("The success type.")]
1224	impl<T: Clone + 'static> Traversable for ResultOkAppliedBrand<T> {
1225		/// Traverses the result with an applicative function (over error).
1226		///
1227		/// This method maps the element of the result to a computation, evaluates it, and combines the result into an applicative context (over error).
1228		#[document_signature]
1229		///
1230		#[document_type_parameters(
1231			"The lifetime of the values.",
1232			"The type of the elements in the traversable structure.",
1233			"The type of the elements in the resulting traversable structure.",
1234			"The applicative context."
1235		)]
1236		///
1237		#[document_parameters("The function to apply.", "The result to traverse.")]
1238		///
1239		#[document_returns("The result wrapped in the applicative context.")]
1240		///
1241		#[document_examples]
1242		///
1243		/// ```
1244		/// use fp_library::{
1245		/// 	brands::{
1246		/// 		OptionBrand,
1247		/// 		ResultOkAppliedBrand,
1248		/// 	},
1249		/// 	functions::*,
1250		/// };
1251		///
1252		/// assert_eq!(
1253		/// 	traverse::<ResultOkAppliedBrand<()>, _, _, OptionBrand>(|x| Some(x * 2), Err(5)),
1254		/// 	Some(Err(10))
1255		/// );
1256		/// assert_eq!(
1257		/// 	traverse::<ResultOkAppliedBrand<i32>, _, _, OptionBrand>(|x: i32| Some(x * 2), Ok(1)),
1258		/// 	Some(Ok(1))
1259		/// );
1260		/// assert_eq!(
1261		/// 	traverse::<ResultOkAppliedBrand<()>, _, _, OptionBrand>(|_| None::<i32>, Err(5)),
1262		/// 	None
1263		/// );
1264		/// ```
1265		fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1266			func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1267			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1268		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1269		where
1270			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1271			match ta {
1272				Err(e) => F::map(|b| Err(b), func(e)),
1273				Ok(t) => F::pure(Ok(t)),
1274			}
1275		}
1276
1277		/// Sequences a result of applicative (over error).
1278		///
1279		/// This method evaluates the computation inside the result and accumulates the result into an applicative context (over error).
1280		#[document_signature]
1281		///
1282		#[document_type_parameters(
1283			"The lifetime of the values.",
1284			"The type of the elements in the traversable structure.",
1285			"The applicative context."
1286		)]
1287		///
1288		#[document_parameters("The result containing the applicative value.")]
1289		///
1290		#[document_returns("The result wrapped in the applicative context.")]
1291		///
1292		#[document_examples]
1293		///
1294		/// ```
1295		/// use fp_library::{
1296		/// 	brands::{
1297		/// 		OptionBrand,
1298		/// 		ResultOkAppliedBrand,
1299		/// 	},
1300		/// 	functions::*,
1301		/// };
1302		///
1303		/// assert_eq!(sequence::<ResultOkAppliedBrand<()>, _, OptionBrand>(Err(Some(5))), Some(Err(5)));
1304		/// assert_eq!(
1305		/// 	sequence::<ResultOkAppliedBrand<i32>, i32, OptionBrand>(Ok::<_, Option<i32>>(1)),
1306		/// 	Some(Ok::<i32, i32>(1))
1307		/// );
1308		/// assert_eq!(sequence::<ResultOkAppliedBrand<()>, _, OptionBrand>(Err(None::<i32>)), None);
1309		/// ```
1310		fn sequence<'a, A: 'a + Clone, F: Applicative>(
1311			ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1312		) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1313		where
1314			Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1315			Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1316			match ta {
1317				Err(fe) => F::map(|e| Err(e), fe),
1318				Ok(t) => F::pure(Ok(t)),
1319			}
1320		}
1321	}
1322
1323	/// [`MonadRec`] implementation for [`ResultOkAppliedBrand`].
1324	#[document_type_parameters("The success type.")]
1325	impl<T: Clone + 'static> MonadRec for ResultOkAppliedBrand<T> {
1326		/// Performs tail-recursive monadic computation over [`Result`] (error channel).
1327		///
1328		/// Iteratively applies the step function. If the function returns [`Ok`],
1329		/// the computation short-circuits with that success value. If it returns
1330		/// `Err(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
1331		/// `Err(ControlFlow::Break(b))`, the computation completes with `Err(b)`.
1332		#[document_signature]
1333		///
1334		#[document_type_parameters(
1335			"The lifetime of the computation.",
1336			"The type of the initial value and loop state.",
1337			"The type of the result."
1338		)]
1339		///
1340		#[document_parameters("The step function.", "The initial value.")]
1341		///
1342		#[document_returns(
1343			"The result of the computation, or a success if the step function returned `Ok`."
1344		)]
1345		///
1346		#[document_examples]
1347		///
1348		/// ```
1349		/// use {
1350		/// 	core::ops::ControlFlow,
1351		/// 	fp_library::{
1352		/// 		brands::*,
1353		/// 		functions::*,
1354		/// 		types::*,
1355		/// 	},
1356		/// };
1357		///
1358		/// let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1359		/// 	|n| {
1360		/// 		if n < 10 { Err(ControlFlow::Continue(n + 1)) } else { Err(ControlFlow::Break(n)) }
1361		/// 	},
1362		/// 	0,
1363		/// );
1364		/// assert_eq!(result, Err(10));
1365		/// ```
1366		fn tail_rec_m<'a, A: 'a, B: 'a>(
1367			func: impl Fn(
1368				A,
1369			)
1370				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1371			+ 'a,
1372			initial: A,
1373		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1374			let mut current = initial;
1375			loop {
1376				match func(current) {
1377					Ok(t) => return Ok(t),
1378					Err(ControlFlow::Continue(next)) => current = next,
1379					Err(ControlFlow::Break(b)) => return Err(b),
1380				}
1381			}
1382		}
1383	}
1384
1385	/// [`MonadRec`] implementation for [`ResultErrAppliedBrand`].
1386	#[document_type_parameters("The error type.")]
1387	impl<E: Clone + 'static> MonadRec for ResultErrAppliedBrand<E> {
1388		/// Performs tail-recursive monadic computation over [`Result`].
1389		///
1390		/// Iteratively applies the step function. If the function returns [`Err`],
1391		/// the computation short-circuits with that error. If it returns
1392		/// `Ok(ControlFlow::Continue(a))`, the loop continues with the new state. If it returns
1393		/// `Ok(ControlFlow::Break(b))`, the computation completes with `Ok(b)`.
1394		#[document_signature]
1395		///
1396		#[document_type_parameters(
1397			"The lifetime of the computation.",
1398			"The type of the initial value and loop state.",
1399			"The type of the result."
1400		)]
1401		///
1402		#[document_parameters("The step function.", "The initial value.")]
1403		///
1404		#[document_returns(
1405			"The result of the computation, or an error if the step function returned `Err`."
1406		)]
1407		///
1408		#[document_examples]
1409		///
1410		/// ```
1411		/// use {
1412		/// 	core::ops::ControlFlow,
1413		/// 	fp_library::{
1414		/// 		brands::*,
1415		/// 		functions::*,
1416		/// 		types::*,
1417		/// 	},
1418		/// };
1419		///
1420		/// let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
1421		/// 	|n| {
1422		/// 		if n < 10 { Ok(ControlFlow::Continue(n + 1)) } else { Ok(ControlFlow::Break(n)) }
1423		/// 	},
1424		/// 	0,
1425		/// );
1426		/// assert_eq!(result, Ok(10));
1427		/// ```
1428		fn tail_rec_m<'a, A: 'a, B: 'a>(
1429			func: impl Fn(
1430				A,
1431			)
1432				-> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1433			+ 'a,
1434			initial: A,
1435		) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1436			let mut current = initial;
1437			loop {
1438				match func(current) {
1439					Err(e) => return Err(e),
1440					Ok(ControlFlow::Continue(next)) => current = next,
1441					Ok(ControlFlow::Break(b)) => return Ok(b),
1442				}
1443			}
1444		}
1445	}
1446}
1447
1448#[cfg(test)]
1449mod tests {
1450
1451	use {
1452		crate::{
1453			brands::*,
1454			classes::{
1455				CloneableFn,
1456				bifunctor::*,
1457			},
1458			functions::*,
1459		},
1460		quickcheck_macros::quickcheck,
1461	};
1462
1463	// Bifunctor Tests
1464
1465	/// Tests `bimap` on `Ok` and `Err`.
1466	#[test]
1467	fn test_bimap() {
1468		let x: Result<i32, i32> = Ok(5);
1469		assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x), Ok(10));
1470
1471		let y: Result<i32, i32> = Err(5);
1472		assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, y), Err(6));
1473	}
1474
1475	// Bifunctor Laws
1476
1477	/// Tests the identity law for Bifunctor.
1478	#[quickcheck]
1479	fn bifunctor_identity(x: Result<i32, i32>) -> bool {
1480		bimap::<ResultBrand, _, _, _, _>(identity, identity, x) == x
1481	}
1482
1483	/// Tests the composition law for Bifunctor.
1484	#[quickcheck]
1485	fn bifunctor_composition(x: Result<i32, i32>) -> bool {
1486		let f = |x: i32| x.wrapping_add(1);
1487		let g = |x: i32| x.wrapping_mul(2);
1488		let h = |x: i32| x.wrapping_sub(1);
1489		let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1490
1491		bimap::<ResultBrand, _, _, _, _>(compose(f, g), compose(h, i), x)
1492			== bimap::<ResultBrand, _, _, _, _>(f, h, bimap::<ResultBrand, _, _, _, _>(g, i, x))
1493	}
1494
1495	// Functor Laws
1496
1497	/// Tests the identity law for Functor.
1498	#[quickcheck]
1499	fn functor_identity(x: Result<i32, i32>) -> bool {
1500		map::<ResultErrAppliedBrand<i32>, _, _>(identity, x) == x
1501	}
1502
1503	/// Tests the composition law for Functor.
1504	#[quickcheck]
1505	fn functor_composition(x: Result<i32, i32>) -> bool {
1506		let f = |x: i32| x.wrapping_add(1);
1507		let g = |x: i32| x.wrapping_mul(2);
1508		map::<ResultErrAppliedBrand<i32>, _, _>(compose(f, g), x)
1509			== map::<ResultErrAppliedBrand<i32>, _, _>(
1510				f,
1511				map::<ResultErrAppliedBrand<i32>, _, _>(g, x),
1512			)
1513	}
1514
1515	// Applicative Laws
1516
1517	/// Tests the identity law for Applicative.
1518	#[quickcheck]
1519	fn applicative_identity(v: Result<i32, i32>) -> bool {
1520		apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1521			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(identity)),
1522			v,
1523		) == v
1524	}
1525
1526	/// Tests the homomorphism law for Applicative.
1527	#[quickcheck]
1528	fn applicative_homomorphism(x: i32) -> bool {
1529		let f = |x: i32| x.wrapping_mul(2);
1530		apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1531			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f)),
1532			pure::<ResultErrAppliedBrand<i32>, _>(x),
1533		) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
1534	}
1535
1536	/// Tests the composition law for Applicative.
1537	#[quickcheck]
1538	fn applicative_composition(
1539		w: Result<i32, i32>,
1540		u_is_ok: bool,
1541		v_is_ok: bool,
1542	) -> bool {
1543		let v_fn = |x: i32| x.wrapping_mul(2);
1544		let u_fn = |x: i32| x.wrapping_add(1);
1545
1546		let v = if v_is_ok {
1547			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(v_fn))
1548		} else {
1549			Err(100)
1550		};
1551		let u = if u_is_ok {
1552			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(u_fn))
1553		} else {
1554			Err(200)
1555		};
1556
1557		// RHS: u <*> (v <*> w)
1558		let vw = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
1559		let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
1560
1561		// LHS: pure(compose) <*> u <*> v <*> w
1562		// equivalent to (u . v) <*> w
1563		let uv = match (u, v) {
1564			(Ok(uf), Ok(vf)) => {
1565				let composed = move |x| uf(vf(x));
1566				Ok(<RcFnBrand as CloneableFn>::new(composed))
1567			}
1568			(Err(e), _) => Err(e),
1569			(_, Err(e)) => Err(e),
1570		};
1571
1572		let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
1573
1574		lhs == rhs
1575	}
1576
1577	/// Tests the interchange law for Applicative.
1578	#[quickcheck]
1579	fn applicative_interchange(y: i32) -> bool {
1580		// u <*> pure y = pure ($ y) <*> u
1581		let f = |x: i32| x.wrapping_mul(2);
1582		let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f));
1583
1584		let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1585			u.clone(),
1586			pure::<ResultErrAppliedBrand<i32>, _>(y),
1587		);
1588
1589		let rhs_fn =
1590			<RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1591		let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1592			pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
1593			u,
1594		);
1595
1596		lhs == rhs
1597	}
1598
1599	// Monad Laws
1600
1601	/// Tests the left identity law for Monad.
1602	#[quickcheck]
1603	fn monad_left_identity(a: i32) -> bool {
1604		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1605		bind::<ResultErrAppliedBrand<i32>, _, _>(pure::<ResultErrAppliedBrand<i32>, _>(a), f)
1606			== f(a)
1607	}
1608
1609	/// Tests the right identity law for Monad.
1610	#[quickcheck]
1611	fn monad_right_identity(m: Result<i32, i32>) -> bool {
1612		bind::<ResultErrAppliedBrand<i32>, _, _>(m, pure::<ResultErrAppliedBrand<i32>, _>) == m
1613	}
1614
1615	/// Tests the associativity law for Monad.
1616	#[quickcheck]
1617	fn monad_associativity(m: Result<i32, i32>) -> bool {
1618		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1619		let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
1620		bind::<ResultErrAppliedBrand<i32>, _, _>(bind::<ResultErrAppliedBrand<i32>, _, _>(m, f), g)
1621			== bind::<ResultErrAppliedBrand<i32>, _, _>(m, |x| {
1622				bind::<ResultErrAppliedBrand<i32>, _, _>(f(x), g)
1623			})
1624	}
1625
1626	// Edge Cases
1627
1628	/// Tests `map` on `Err`.
1629	#[test]
1630	fn map_err() {
1631		assert_eq!(
1632			map::<ResultErrAppliedBrand<i32>, _, _>(|x: i32| x + 1, Err::<i32, i32>(1)),
1633			Err(1)
1634		);
1635	}
1636
1637	/// Tests `bind` on `Err`.
1638	#[test]
1639	fn bind_err() {
1640		assert_eq!(
1641			bind::<ResultErrAppliedBrand<i32>, _, _>(Err::<i32, i32>(1), |x: i32| Ok(x + 1)),
1642			Err(1)
1643		);
1644	}
1645
1646	/// Tests `bind` returning `Err`.
1647	#[test]
1648	fn bind_returning_err() {
1649		assert_eq!(bind::<ResultErrAppliedBrand<i32>, _, _>(Ok(1), |_| Err::<i32, i32>(2)), Err(2));
1650	}
1651
1652	/// Tests `fold_right` on `Err`.
1653	#[test]
1654	fn fold_right_err() {
1655		assert_eq!(
1656			crate::classes::foldable::fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1657				|x: i32, acc| x + acc,
1658				0,
1659				Err(1)
1660			),
1661			0
1662		);
1663	}
1664
1665	/// Tests `fold_left` on `Err`.
1666	#[test]
1667	fn fold_left_err() {
1668		assert_eq!(
1669			crate::classes::foldable::fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1670				|acc, x: i32| acc + x,
1671				0,
1672				Err(1)
1673			),
1674			0
1675		);
1676	}
1677
1678	/// Tests `traverse` on `Err`.
1679	#[test]
1680	fn traverse_err() {
1681		assert_eq!(
1682			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1683				|x: i32| Some(x + 1),
1684				Err(1)
1685			),
1686			Some(Err(1))
1687		);
1688	}
1689
1690	/// Tests `traverse` returning `Err`.
1691	#[test]
1692	fn traverse_returning_err() {
1693		assert_eq!(
1694			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1695				|_: i32| None::<i32>,
1696				Ok(1)
1697			),
1698			None
1699		);
1700	}
1701
1702	// MonadRec tests
1703
1704	/// Tests the MonadRec identity law: `tail_rec_m(|a| pure(Done(a)), x) == pure(x)`.
1705	#[quickcheck]
1706	fn monad_rec_identity(x: i32) -> bool {
1707		use {
1708			crate::classes::monad_rec::tail_rec_m,
1709			core::ops::ControlFlow,
1710		};
1711		tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(|a| Ok(ControlFlow::Break(a)), x) == Ok(x)
1712	}
1713
1714	/// Tests a recursive computation that sums a range via `tail_rec_m`.
1715	#[test]
1716	fn monad_rec_sum_range() {
1717		use {
1718			crate::classes::monad_rec::tail_rec_m,
1719			core::ops::ControlFlow,
1720		};
1721		let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
1722			|(n, acc)| {
1723				if n == 0 {
1724					Ok(ControlFlow::Break(acc))
1725				} else {
1726					Ok(ControlFlow::Continue((n - 1, acc + n)))
1727				}
1728			},
1729			(100i64, 0i64),
1730		);
1731		assert_eq!(result, Ok(5050));
1732	}
1733
1734	/// Tests that `tail_rec_m` short-circuits on `Err`.
1735	#[test]
1736	fn monad_rec_short_circuit() {
1737		use {
1738			crate::classes::monad_rec::tail_rec_m,
1739			core::ops::ControlFlow,
1740		};
1741		let result: Result<i32, &str> = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
1742			|n| {
1743				if n == 5 { Err("stopped") } else { Ok(ControlFlow::Continue(n + 1)) }
1744			},
1745			0,
1746		);
1747		assert_eq!(result, Err("stopped"));
1748	}
1749
1750	/// Tests stack safety: `tail_rec_m` handles large iteration counts.
1751	#[test]
1752	fn monad_rec_stack_safety() {
1753		use {
1754			crate::classes::monad_rec::tail_rec_m,
1755			core::ops::ControlFlow,
1756		};
1757		let iterations: i64 = 200_000;
1758		let result = tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(
1759			|acc| {
1760				if acc < iterations {
1761					Ok(ControlFlow::Continue(acc + 1))
1762				} else {
1763					Ok(ControlFlow::Break(acc))
1764				}
1765			},
1766			0i64,
1767		);
1768		assert_eq!(result, Ok(iterations));
1769	}
1770
1771	// MonadRec tests for ResultOkAppliedBrand
1772
1773	/// Tests the MonadRec identity law for `ResultOkAppliedBrand`:
1774	/// `tail_rec_m(|a| Err(Done(a)), x) == Err(x)`.
1775	#[quickcheck]
1776	fn monad_rec_ok_applied_identity(x: i32) -> bool {
1777		use {
1778			crate::classes::monad_rec::tail_rec_m,
1779			core::ops::ControlFlow,
1780		};
1781		tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(|a| Err(ControlFlow::Break(a)), x) == Err(x)
1782	}
1783
1784	/// Tests a recursive computation that sums a range via `tail_rec_m` on the error channel.
1785	#[test]
1786	fn monad_rec_ok_applied_sum_range() {
1787		use {
1788			crate::classes::monad_rec::tail_rec_m,
1789			core::ops::ControlFlow,
1790		};
1791		let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1792			|(n, acc)| {
1793				if n == 0 {
1794					Err(ControlFlow::Break(acc))
1795				} else {
1796					Err(ControlFlow::Continue((n - 1, acc + n)))
1797				}
1798			},
1799			(100i64, 0i64),
1800		);
1801		assert_eq!(result, Err(5050));
1802	}
1803
1804	/// Tests that `tail_rec_m` on `ResultOkAppliedBrand` short-circuits on `Ok`.
1805	#[test]
1806	fn monad_rec_ok_applied_short_circuit() {
1807		use {
1808			crate::classes::monad_rec::tail_rec_m,
1809			core::ops::ControlFlow,
1810		};
1811		let result: Result<&str, i32> = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1812			|n| {
1813				if n == 5 { Ok("stopped") } else { Err(ControlFlow::Continue(n + 1)) }
1814			},
1815			0,
1816		);
1817		assert_eq!(result, Ok("stopped"));
1818	}
1819
1820	/// Tests stack safety: `tail_rec_m` on `ResultOkAppliedBrand` handles large iteration counts.
1821	#[test]
1822	fn monad_rec_ok_applied_stack_safety() {
1823		use {
1824			crate::classes::monad_rec::tail_rec_m,
1825			core::ops::ControlFlow,
1826		};
1827		let iterations: i64 = 200_000;
1828		let result = tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(
1829			|acc| {
1830				if acc < iterations {
1831					Err(ControlFlow::Continue(acc + 1))
1832				} else {
1833					Err(ControlFlow::Break(acc))
1834				}
1835			},
1836			0i64,
1837		);
1838		assert_eq!(result, Err(iterations));
1839	}
1840}