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