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				Monoid,
27				ParFoldable,
28				Pointed,
29				Semiapplicative,
30				Semimonad,
31				SendCloneableFn,
32				Traversable,
33			},
34			impl_kind,
35			kinds::*,
36		},
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	#[document_type_parameters("The error type.")]
1324	impl<E: 'static> ParFoldable for ResultErrAppliedBrand<E> {
1325		/// Maps the value to a monoid and returns it, or returns empty, in parallel.
1326		///
1327		/// This method maps the element of the result to a monoid and then returns it. The mapping operation may be executed in parallel.
1328		#[document_signature]
1329		///
1330		#[document_type_parameters(
1331			"The lifetime of the values.",
1332			"The brand of the cloneable function wrapper.",
1333			"The element type.",
1334			"The monoid type."
1335		)]
1336		///
1337		#[document_parameters(
1338			"The thread-safe function to map each element to a monoid.",
1339			"The result to fold."
1340		)]
1341		///
1342		#[document_returns("The combined monoid value.")]
1343		#[document_examples]
1344		///
1345		/// ```
1346		/// use fp_library::{
1347		/// 	brands::*,
1348		/// 	classes::*,
1349		/// 	functions::*,
1350		/// 	types::*,
1351		/// };
1352		///
1353		/// let x: Result<i32, ()> = Ok(5);
1354		/// let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1355		/// assert_eq!(
1356		/// 	par_fold_map::<ArcFnBrand, ResultErrAppliedBrand<()>, _, _>(f.clone(), x),
1357		/// 	"5".to_string()
1358		/// );
1359		///
1360		/// let x_err: Result<i32, i32> = Err(1);
1361		/// assert_eq!(
1362		/// 	par_fold_map::<ArcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, x_err),
1363		/// 	"".to_string()
1364		/// );
1365		/// ```
1366		fn par_fold_map<'a, FnBrand, A, M>(
1367			func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1368			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1369		) -> M
1370		where
1371			FnBrand: 'a + SendCloneableFn,
1372			A: 'a + Clone + Send + Sync,
1373			M: Monoid + Send + Sync + 'a, {
1374			match fa {
1375				Ok(a) => func(a),
1376				Err(_) => M::empty(),
1377			}
1378		}
1379
1380		/// Folds the result from the right in parallel.
1381		///
1382		/// This method folds the result by applying a function from right to left, potentially in parallel.
1383		#[document_signature]
1384		///
1385		#[document_type_parameters(
1386			"The lifetime of the values.",
1387			"The brand of the cloneable function wrapper.",
1388			"The element type.",
1389			"The accumulator type."
1390		)]
1391		///
1392		#[document_parameters(
1393			"The thread-safe function to apply to each element and the accumulator.",
1394			"The initial value.",
1395			"The result to fold."
1396		)]
1397		///
1398		#[document_returns("The final accumulator value.")]
1399		#[document_examples]
1400		///
1401		/// ```
1402		/// use fp_library::{
1403		/// 	brands::*,
1404		/// 	classes::*,
1405		/// 	functions::*,
1406		/// };
1407		///
1408		/// let x: Result<i32, ()> = Ok(5);
1409		/// let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1410		/// assert_eq!(par_fold_right::<ArcFnBrand, ResultErrAppliedBrand<()>, _, _>(f.clone(), 10, x), 15);
1411		///
1412		/// let x_err: Result<i32, i32> = Err(1);
1413		/// assert_eq!(par_fold_right::<ArcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, 10, x_err), 10);
1414		/// ```
1415		fn par_fold_right<'a, FnBrand, A, B>(
1416			func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1417			initial: B,
1418			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1419		) -> B
1420		where
1421			FnBrand: 'a + SendCloneableFn,
1422			A: 'a + Clone + Send + Sync,
1423			B: Send + Sync + 'a, {
1424			match fa {
1425				Ok(a) => func((a, initial)),
1426				Err(_) => initial,
1427			}
1428		}
1429	}
1430
1431	#[document_type_parameters("The success type.")]
1432	impl<T: 'static> ParFoldable for ResultOkAppliedBrand<T> {
1433		/// Maps the value to a monoid and returns it, or returns empty, in parallel (over error).
1434		///
1435		/// This method maps the element of the result to a monoid and then returns it (over error). The mapping operation may be executed in parallel.
1436		#[document_signature]
1437		///
1438		#[document_type_parameters(
1439			"The lifetime of the values.",
1440			"The brand of the cloneable function wrapper.",
1441			"The element type.",
1442			"The monoid type."
1443		)]
1444		///
1445		#[document_parameters(
1446			"The thread-safe function to map each element to a monoid.",
1447			"The result to fold."
1448		)]
1449		///
1450		#[document_returns("The combined monoid value.")]
1451		#[document_examples]
1452		///
1453		/// ```
1454		/// use fp_library::{
1455		/// 	brands::*,
1456		/// 	classes::*,
1457		/// 	functions::*,
1458		/// 	types::*,
1459		/// };
1460		///
1461		/// let x: Result<(), i32> = Err(5);
1462		/// let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1463		/// assert_eq!(
1464		/// 	par_fold_map::<ArcFnBrand, ResultOkAppliedBrand<()>, _, _>(f.clone(), x),
1465		/// 	"5".to_string()
1466		/// );
1467		///
1468		/// let x_ok: Result<i32, i32> = Ok(1);
1469		/// assert_eq!(
1470		/// 	par_fold_map::<ArcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, x_ok),
1471		/// 	"".to_string()
1472		/// );
1473		/// ```
1474		fn par_fold_map<'a, FnBrand, A, M>(
1475			func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1476			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1477		) -> M
1478		where
1479			FnBrand: 'a + SendCloneableFn,
1480			A: 'a + Clone + Send + Sync,
1481			M: Monoid + Send + Sync + 'a, {
1482			match fa {
1483				Err(e) => func(e),
1484				Ok(_) => M::empty(),
1485			}
1486		}
1487
1488		/// Folds the result from the right in parallel (over error).
1489		///
1490		/// This method folds the result by applying a function from right to left, potentially in parallel (over error).
1491		#[document_signature]
1492		///
1493		#[document_type_parameters(
1494			"The lifetime of the values.",
1495			"The brand of the cloneable function wrapper.",
1496			"The element type.",
1497			"The accumulator type."
1498		)]
1499		///
1500		#[document_parameters(
1501			"The thread-safe function to apply to each element and the accumulator.",
1502			"The initial value.",
1503			"The result to fold."
1504		)]
1505		///
1506		#[document_returns("The final accumulator value.")]
1507		#[document_examples]
1508		///
1509		/// ```
1510		/// use fp_library::{
1511		/// 	brands::*,
1512		/// 	classes::*,
1513		/// 	functions::*,
1514		/// };
1515		///
1516		/// let x: Result<(), i32> = Err(5);
1517		/// let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1518		/// assert_eq!(par_fold_right::<ArcFnBrand, ResultOkAppliedBrand<()>, _, _>(f.clone(), 10, x), 15);
1519		///
1520		/// let x_ok: Result<i32, i32> = Ok(1);
1521		/// assert_eq!(par_fold_right::<ArcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, 10, x_ok), 10);
1522		/// ```
1523		fn par_fold_right<'a, FnBrand, A, B>(
1524			func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1525			initial: B,
1526			fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1527		) -> B
1528		where
1529			FnBrand: 'a + SendCloneableFn,
1530			A: 'a + Clone + Send + Sync,
1531			B: Send + Sync + 'a, {
1532			match fa {
1533				Err(e) => func((e, initial)),
1534				Ok(_) => initial,
1535			}
1536		}
1537	}
1538}
1539
1540#[cfg(test)]
1541mod tests {
1542
1543	use {
1544		crate::{
1545			brands::*,
1546			classes::{
1547				CloneableFn,
1548				bifunctor::*,
1549			},
1550			functions::*,
1551		},
1552		quickcheck_macros::quickcheck,
1553	};
1554
1555	// Bifunctor Tests
1556
1557	/// Tests `bimap` on `Ok` and `Err`.
1558	#[test]
1559	fn test_bimap() {
1560		let x: Result<i32, i32> = Ok(5);
1561		assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x), Ok(10));
1562
1563		let y: Result<i32, i32> = Err(5);
1564		assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, y), Err(6));
1565	}
1566
1567	// Bifunctor Laws
1568
1569	/// Tests the identity law for Bifunctor.
1570	#[quickcheck]
1571	fn bifunctor_identity(x: Result<i32, i32>) -> bool {
1572		bimap::<ResultBrand, _, _, _, _>(identity, identity, x) == x
1573	}
1574
1575	/// Tests the composition law for Bifunctor.
1576	#[quickcheck]
1577	fn bifunctor_composition(x: Result<i32, i32>) -> bool {
1578		let f = |x: i32| x.wrapping_add(1);
1579		let g = |x: i32| x.wrapping_mul(2);
1580		let h = |x: i32| x.wrapping_sub(1);
1581		let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1582
1583		bimap::<ResultBrand, _, _, _, _>(compose(f, g), compose(h, i), x)
1584			== bimap::<ResultBrand, _, _, _, _>(f, h, bimap::<ResultBrand, _, _, _, _>(g, i, x))
1585	}
1586
1587	// Functor Laws
1588
1589	/// Tests the identity law for Functor.
1590	#[quickcheck]
1591	fn functor_identity(x: Result<i32, i32>) -> bool {
1592		map::<ResultErrAppliedBrand<i32>, _, _>(identity, x) == x
1593	}
1594
1595	/// Tests the composition law for Functor.
1596	#[quickcheck]
1597	fn functor_composition(x: Result<i32, i32>) -> bool {
1598		let f = |x: i32| x.wrapping_add(1);
1599		let g = |x: i32| x.wrapping_mul(2);
1600		map::<ResultErrAppliedBrand<i32>, _, _>(compose(f, g), x)
1601			== map::<ResultErrAppliedBrand<i32>, _, _>(
1602				f,
1603				map::<ResultErrAppliedBrand<i32>, _, _>(g, x),
1604			)
1605	}
1606
1607	// Applicative Laws
1608
1609	/// Tests the identity law for Applicative.
1610	#[quickcheck]
1611	fn applicative_identity(v: Result<i32, i32>) -> bool {
1612		apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1613			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(identity)),
1614			v,
1615		) == v
1616	}
1617
1618	/// Tests the homomorphism law for Applicative.
1619	#[quickcheck]
1620	fn applicative_homomorphism(x: i32) -> bool {
1621		let f = |x: i32| x.wrapping_mul(2);
1622		apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1623			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f)),
1624			pure::<ResultErrAppliedBrand<i32>, _>(x),
1625		) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
1626	}
1627
1628	/// Tests the composition law for Applicative.
1629	#[quickcheck]
1630	fn applicative_composition(
1631		w: Result<i32, i32>,
1632		u_is_ok: bool,
1633		v_is_ok: bool,
1634	) -> bool {
1635		let v_fn = |x: i32| x.wrapping_mul(2);
1636		let u_fn = |x: i32| x.wrapping_add(1);
1637
1638		let v = if v_is_ok {
1639			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(v_fn))
1640		} else {
1641			Err(100)
1642		};
1643		let u = if u_is_ok {
1644			pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(u_fn))
1645		} else {
1646			Err(200)
1647		};
1648
1649		// RHS: u <*> (v <*> w)
1650		let vw = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
1651		let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
1652
1653		// LHS: pure(compose) <*> u <*> v <*> w
1654		// equivalent to (u . v) <*> w
1655		let uv = match (u, v) {
1656			(Ok(uf), Ok(vf)) => {
1657				let composed = move |x| uf(vf(x));
1658				Ok(<RcFnBrand as CloneableFn>::new(composed))
1659			}
1660			(Err(e), _) => Err(e),
1661			(_, Err(e)) => Err(e),
1662		};
1663
1664		let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
1665
1666		lhs == rhs
1667	}
1668
1669	/// Tests the interchange law for Applicative.
1670	#[quickcheck]
1671	fn applicative_interchange(y: i32) -> bool {
1672		// u <*> pure y = pure ($ y) <*> u
1673		let f = |x: i32| x.wrapping_mul(2);
1674		let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f));
1675
1676		let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1677			u.clone(),
1678			pure::<ResultErrAppliedBrand<i32>, _>(y),
1679		);
1680
1681		let rhs_fn =
1682			<RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1683		let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1684			pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
1685			u,
1686		);
1687
1688		lhs == rhs
1689	}
1690
1691	// Monad Laws
1692
1693	/// Tests the left identity law for Monad.
1694	#[quickcheck]
1695	fn monad_left_identity(a: i32) -> bool {
1696		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1697		bind::<ResultErrAppliedBrand<i32>, _, _>(pure::<ResultErrAppliedBrand<i32>, _>(a), f)
1698			== f(a)
1699	}
1700
1701	/// Tests the right identity law for Monad.
1702	#[quickcheck]
1703	fn monad_right_identity(m: Result<i32, i32>) -> bool {
1704		bind::<ResultErrAppliedBrand<i32>, _, _>(m, pure::<ResultErrAppliedBrand<i32>, _>) == m
1705	}
1706
1707	/// Tests the associativity law for Monad.
1708	#[quickcheck]
1709	fn monad_associativity(m: Result<i32, i32>) -> bool {
1710		let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1711		let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
1712		bind::<ResultErrAppliedBrand<i32>, _, _>(bind::<ResultErrAppliedBrand<i32>, _, _>(m, f), g)
1713			== bind::<ResultErrAppliedBrand<i32>, _, _>(m, |x| {
1714				bind::<ResultErrAppliedBrand<i32>, _, _>(f(x), g)
1715			})
1716	}
1717
1718	// Edge Cases
1719
1720	/// Tests `map` on `Err`.
1721	#[test]
1722	fn map_err() {
1723		assert_eq!(
1724			map::<ResultErrAppliedBrand<i32>, _, _>(|x: i32| x + 1, Err::<i32, i32>(1)),
1725			Err(1)
1726		);
1727	}
1728
1729	/// Tests `bind` on `Err`.
1730	#[test]
1731	fn bind_err() {
1732		assert_eq!(
1733			bind::<ResultErrAppliedBrand<i32>, _, _>(Err::<i32, i32>(1), |x: i32| Ok(x + 1)),
1734			Err(1)
1735		);
1736	}
1737
1738	/// Tests `bind` returning `Err`.
1739	#[test]
1740	fn bind_returning_err() {
1741		assert_eq!(bind::<ResultErrAppliedBrand<i32>, _, _>(Ok(1), |_| Err::<i32, i32>(2)), Err(2));
1742	}
1743
1744	/// Tests `fold_right` on `Err`.
1745	#[test]
1746	fn fold_right_err() {
1747		assert_eq!(
1748			crate::classes::foldable::fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1749				|x: i32, acc| x + acc,
1750				0,
1751				Err(1)
1752			),
1753			0
1754		);
1755	}
1756
1757	/// Tests `fold_left` on `Err`.
1758	#[test]
1759	fn fold_left_err() {
1760		assert_eq!(
1761			crate::classes::foldable::fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1762				|acc, x: i32| acc + x,
1763				0,
1764				Err(1)
1765			),
1766			0
1767		);
1768	}
1769
1770	/// Tests `traverse` on `Err`.
1771	#[test]
1772	fn traverse_err() {
1773		assert_eq!(
1774			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1775				|x: i32| Some(x + 1),
1776				Err(1)
1777			),
1778			Some(Err(1))
1779		);
1780	}
1781
1782	/// Tests `traverse` returning `Err`.
1783	#[test]
1784	fn traverse_returning_err() {
1785		assert_eq!(
1786			crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1787				|_: i32| None::<i32>,
1788				Ok(1)
1789			),
1790			None
1791		);
1792	}
1793
1794	// ParFoldable Tests for ResultErrAppliedBrand
1795
1796	/// Tests `par_fold_map` on `Ok`.
1797	#[test]
1798	fn par_fold_map_ok() {
1799		let x: Result<i32, ()> = Ok(5);
1800		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1801		assert_eq!(
1802			par_fold_map::<ArcFnBrand, ResultErrAppliedBrand<()>, _, _>(f, x),
1803			"5".to_string()
1804		);
1805	}
1806
1807	/// Tests `par_fold_map` on `Err`.
1808	#[test]
1809	fn par_fold_map_err_val() {
1810		let x: Result<i32, i32> = Err(5);
1811		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1812		assert_eq!(
1813			par_fold_map::<ArcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, x),
1814			"".to_string()
1815		);
1816	}
1817
1818	/// Tests `par_fold_right` on `Ok`.
1819	#[test]
1820	fn par_fold_right_ok() {
1821		let x: Result<i32, ()> = Ok(5);
1822		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1823		assert_eq!(par_fold_right::<ArcFnBrand, ResultErrAppliedBrand<()>, _, _>(f, 10, x), 15);
1824	}
1825
1826	/// Tests `par_fold_right` on `Err`.
1827	#[test]
1828	fn par_fold_right_err_val() {
1829		let x: Result<i32, i32> = Err(5);
1830		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1831		assert_eq!(par_fold_right::<ArcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, 10, x), 10);
1832	}
1833
1834	// ParFoldable Tests for ResultOkAppliedBrand
1835
1836	/// Tests `par_fold_map` on `Err` (which holds the value for ResultOkAppliedBrand).
1837	#[test]
1838	fn par_fold_map_err_ok_brand() {
1839		let x: Result<(), i32> = Err(5);
1840		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1841		assert_eq!(
1842			par_fold_map::<ArcFnBrand, ResultOkAppliedBrand<()>, _, _>(f.clone(), x),
1843			"5".to_string()
1844		);
1845	}
1846
1847	/// Tests `par_fold_map` on `Ok` (which is empty for ResultOkAppliedBrand).
1848	#[test]
1849	fn par_fold_map_ok_ok_brand() {
1850		let x: Result<i32, i32> = Ok(5);
1851		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1852		assert_eq!(
1853			par_fold_map::<ArcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, x),
1854			"".to_string()
1855		);
1856	}
1857
1858	/// Tests `par_fold_right` on `Err` (which holds the value for ResultOkAppliedBrand).
1859	#[test]
1860	fn par_fold_right_err_ok_brand() {
1861		let x: Result<(), i32> = Err(5);
1862		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1863		assert_eq!(
1864			par_fold_right::<ArcFnBrand, ResultOkAppliedBrand<()>, _, _>(f.clone(), 10, x),
1865			15
1866		);
1867	}
1868
1869	/// Tests `par_fold_right` on `Ok` (which is empty for ResultOkAppliedBrand).
1870	#[test]
1871	fn par_fold_right_ok_ok_brand() {
1872		let x: Result<i32, i32> = Ok(5);
1873		let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1874		assert_eq!(par_fold_right::<ArcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, 10, x), 10);
1875	}
1876}