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