1#[fp_macros::document_module]
6mod inner {
7 use {
8 crate::{
9 Apply,
10 brands::{
11 ResultBrand,
12 ResultErrAppliedBrand,
13 ResultOkAppliedBrand,
14 },
15 classes::*,
16 dispatch::Ref,
17 impl_kind,
18 kinds::*,
19 },
20 core::ops::ControlFlow,
21 fp_macros::*,
22 };
23
24 impl_kind! {
25 for ResultBrand {
32 type Of<A, B> = Result<B, A>;
33 }
34 }
35
36 impl_kind! {
37 for ResultBrand {
41 type Of<'a, A: 'a, B: 'a>: 'a = Result<B, A>;
42 }
43 }
44
45 impl Bifunctor for ResultBrand {
46 #[document_signature]
50 #[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 )]
58 #[document_parameters(
60 "The function to apply to the error.",
61 "The function to apply to the success.",
62 "The result to map over."
63 )]
64 #[document_returns("A new result containing the mapped values.")]
66 #[document_examples]
67 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
81 f: impl Fn(A) -> B + 'a,
82 g: impl Fn(C) -> D + 'a,
83 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
84 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
85 match p {
86 Ok(c) => Ok(g(c)),
87 Err(a) => Err(f(a)),
88 }
89 }
90 }
91
92 impl RefBifunctor for ResultBrand {
93 #[document_signature]
99 #[document_type_parameters(
101 "The lifetime of the values.",
102 "The type of the error value.",
103 "The type of the mapped error value.",
104 "The type of the success value.",
105 "The type of the mapped success value."
106 )]
107 #[document_parameters(
109 "The function to apply to a reference of the error.",
110 "The function to apply to a reference of the success.",
111 "The result to map over by reference."
112 )]
113 #[document_returns("A new result containing the mapped values.")]
115 #[document_examples]
116 fn ref_bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
131 f: impl Fn(&A) -> B + 'a,
132 g: impl Fn(&C) -> D + 'a,
133 p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
134 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
135 match p {
136 Ok(c) => Ok(g(c)),
137 Err(a) => Err(f(a)),
138 }
139 }
140 }
141
142 impl RefBifoldable for ResultBrand {
143 #[document_signature]
149 #[document_type_parameters(
151 "The lifetime of the values.",
152 "The brand of the cloneable function to use.",
153 "The error type (first position).",
154 "The success type (second position).",
155 "The accumulator type."
156 )]
157 #[document_parameters(
159 "The step function applied to a reference of the error value.",
160 "The step function applied to a reference of the success value.",
161 "The initial accumulator.",
162 "The result to fold by reference."
163 )]
164 #[document_returns("`f(&a, z)` for `Err(a)`, or `g(&b, z)` for `Ok(b)`.")]
166 #[document_examples]
167 fn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
195 f: impl Fn(&A, C) -> C + 'a,
196 g: impl Fn(&B, C) -> C + 'a,
197 z: C,
198 p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
199 ) -> C {
200 match p {
201 Err(a) => f(a, z),
202 Ok(b) => g(b, z),
203 }
204 }
205 }
206
207 impl RefBitraversable for ResultBrand {
208 #[document_signature]
214 #[document_type_parameters(
216 "The lifetime of the values.",
217 "The brand of the cloneable function wrapper.",
218 "The error type (first position).",
219 "The success type (second position).",
220 "The output error type.",
221 "The output success type.",
222 "The applicative context."
223 )]
224 #[document_parameters(
226 "The function applied to a reference of the error value.",
227 "The function applied to a reference of the success value.",
228 "The result to traverse by reference."
229 )]
230 #[document_returns(
232 "`f(&a)` wrapped in context for `Err(a)`, or `g(&b)` wrapped in context for `Ok(b)`."
233 )]
234 #[document_examples]
235 fn ref_bi_traverse<
261 'a,
262 FnBrand,
263 A: 'a + Clone,
264 B: 'a + Clone,
265 C: 'a + Clone,
266 D: 'a + Clone,
267 F: Applicative,
268 >(
269 f: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
270 g: impl Fn(&B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
271 p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
272 ) -> 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>)>)
273 where
274 FnBrand: LiftFn + 'a,
275 Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>): Clone,
276 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>): Clone,
277 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>): Clone, {
278 match p {
279 Err(a) => F::map(|c| Err(c), f(a)),
280 Ok(b) => F::map(|d| Ok(d), g(b)),
281 }
282 }
283 }
284
285 impl Bifoldable for ResultBrand {
286 #[document_signature]
290 #[document_type_parameters(
292 "The lifetime of the values.",
293 "The brand of the cloneable function to use.",
294 "The error type (first position).",
295 "The success type (second position).",
296 "The accumulator type."
297 )]
298 #[document_parameters(
300 "The step function applied to the error value.",
301 "The step function applied to the success value.",
302 "The initial accumulator.",
303 "The result to fold."
304 )]
305 #[document_returns("`f(a, z)` for `Err(a)`, or `g(b, z)` for `Ok(b)`.")]
307 #[document_examples]
308 fn bi_fold_right<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
333 f: impl Fn(A, C) -> C + 'a,
334 g: impl Fn(B, C) -> C + 'a,
335 z: C,
336 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
337 ) -> C {
338 match p {
339 Err(a) => f(a, z),
340 Ok(b) => g(b, z),
341 }
342 }
343
344 #[document_signature]
348 #[document_type_parameters(
350 "The lifetime of the values.",
351 "The brand of the cloneable function to use.",
352 "The error type (first position).",
353 "The success type (second position).",
354 "The accumulator type."
355 )]
356 #[document_parameters(
358 "The step function applied to the error value.",
359 "The step function applied to the success value.",
360 "The initial accumulator.",
361 "The result to fold."
362 )]
363 #[document_returns("`f(z, a)` for `Err(a)`, or `g(z, b)` for `Ok(b)`.")]
365 #[document_examples]
366 fn bi_fold_left<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
391 f: impl Fn(C, A) -> C + 'a,
392 g: impl Fn(C, B) -> C + 'a,
393 z: C,
394 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
395 ) -> C {
396 match p {
397 Err(a) => f(z, a),
398 Ok(b) => g(z, b),
399 }
400 }
401
402 #[document_signature]
406 #[document_type_parameters(
408 "The lifetime of the values.",
409 "The brand of the cloneable function to use.",
410 "The error type (first position).",
411 "The success type (second position).",
412 "The monoid type."
413 )]
414 #[document_parameters(
416 "The function mapping the error to the monoid.",
417 "The function mapping the success to the monoid.",
418 "The result to fold."
419 )]
420 #[document_returns("`f(a)` for `Err(a)`, or `g(b)` for `Ok(b)`.")]
422 #[document_examples]
423 fn bi_fold_map<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
446 f: impl Fn(A) -> M + 'a,
447 g: impl Fn(B) -> M + 'a,
448 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
449 ) -> M
450 where
451 M: Monoid + 'a, {
452 match p {
453 Err(a) => f(a),
454 Ok(b) => g(b),
455 }
456 }
457 }
458
459 impl Bitraversable for ResultBrand {
460 #[document_signature]
465 #[document_type_parameters(
467 "The lifetime of the values.",
468 "The error type (first position).",
469 "The success type (second position).",
470 "The output error type.",
471 "The output success type.",
472 "The applicative context."
473 )]
474 #[document_parameters(
476 "The function applied to the error value.",
477 "The function applied to the success value.",
478 "The result to traverse."
479 )]
480 #[document_returns(
482 "`f(a)` wrapped in context for `Err(a)`, or `g(b)` wrapped in context for `Ok(b)`."
483 )]
484 #[document_examples]
485 fn bi_traverse<
508 'a,
509 A: 'a + Clone,
510 B: 'a + Clone,
511 C: 'a + Clone,
512 D: 'a + Clone,
513 F: Applicative,
514 >(
515 f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
516 g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
517 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
518 ) -> 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>)>)
519 {
520 match p {
521 Err(a) => F::map(|c| Err(c), f(a)),
522 Ok(b) => F::map(|d| Ok(d), g(b)),
523 }
524 }
525 }
526
527 impl_kind! {
530 #[no_inferable_brand]
531 #[document_type_parameters("The error type.")]
532 impl<E: 'static> for ResultErrAppliedBrand<E> {
533 type Of<'a, A: 'a>: 'a = Result<A, E>;
534 }
535 }
536
537 #[document_type_parameters("The error type.")]
538 impl<E: 'static> Functor for ResultErrAppliedBrand<E> {
539 #[document_signature]
543 #[document_type_parameters(
545 "The lifetime of the values.",
546 "The type of the value inside the result.",
547 "The type of the result of applying the function."
548 )]
549 #[document_parameters("The function to apply.", "The result to map over.")]
551 #[document_returns(
553 "A new result containing the result of applying the function, or the original error."
554 )]
555 #[document_examples]
557 fn map<'a, A: 'a, B: 'a>(
574 func: impl Fn(A) -> B + 'a,
575 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
576 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
577 fa.map(func)
578 }
579 }
580
581 #[document_type_parameters("The error type.")]
582 impl<E: Clone + 'static> Lift for ResultErrAppliedBrand<E> {
583 #[document_signature]
587 #[document_type_parameters(
589 "The lifetime of the values.",
590 "The type of the first value.",
591 "The type of the second value.",
592 "The type of the result."
593 )]
594 #[document_parameters(
596 "The binary function to apply.",
597 "The first result.",
598 "The second result."
599 )]
600 #[document_returns(
602 "`Ok(f(a, b))` if both results are `Ok`, otherwise the first error encountered."
603 )]
604 #[document_examples]
605 fn lift2<'a, A, B, C>(
646 func: impl Fn(A, B) -> C + 'a,
647 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
648 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
649 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
650 where
651 A: Clone + 'a,
652 B: Clone + 'a,
653 C: 'a, {
654 match (fa, fb) {
655 (Ok(a), Ok(b)) => Ok(func(a, b)),
656 (Err(e), _) => Err(e),
657 (_, Err(e)) => Err(e),
658 }
659 }
660 }
661
662 #[document_type_parameters("The error type.")]
663 impl<E: 'static> Pointed for ResultErrAppliedBrand<E> {
664 #[document_signature]
668 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
670 #[document_parameters("The value to wrap.")]
672 #[document_returns("`Ok(a)`.")]
674 #[document_examples]
676 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
686 Ok(a)
687 }
688 }
689
690 #[document_type_parameters("The error type.")]
691 impl<E: Clone + 'static> ApplyFirst for ResultErrAppliedBrand<E> {}
692
693 #[document_type_parameters("The error type.")]
694 impl<E: Clone + 'static> ApplySecond for ResultErrAppliedBrand<E> {}
695
696 #[document_type_parameters("The error type.")]
697 impl<E: Clone + 'static> Semiapplicative for ResultErrAppliedBrand<E> {
698 #[document_signature]
702 #[document_type_parameters(
704 "The lifetime of the values.",
705 "The brand of the cloneable function wrapper.",
706 "The type of the input value.",
707 "The type of the output value."
708 )]
709 #[document_parameters(
711 "The result containing the function.",
712 "The result containing the value."
713 )]
714 #[document_returns("`Ok(f(a))` if both are `Ok`, otherwise the first error encountered.")]
716 #[document_examples]
717 fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
736 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
737 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
738 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
739 match (ff, fa) {
740 (Ok(f), Ok(a)) => Ok(f(a)),
741 (Err(e), _) => Err(e),
742 (_, Err(e)) => Err(e),
743 }
744 }
745 }
746
747 #[document_type_parameters("The error type.")]
748 impl<E: Clone + 'static> Semimonad for ResultErrAppliedBrand<E> {
749 #[document_signature]
753 #[document_type_parameters(
755 "The lifetime of the values.",
756 "The type of the result of the first computation.",
757 "The type of the result of the second computation."
758 )]
759 #[document_parameters(
761 "The first result.",
762 "The function to apply to the value inside the result."
763 )]
764 #[document_returns(
766 "The result of applying `f` to the value if `ma` is `Ok`, otherwise the original error."
767 )]
768 #[document_examples]
769 fn bind<'a, A: 'a, B: 'a>(
790 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
791 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
792 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
793 ma.and_then(func)
794 }
795 }
796
797 #[document_type_parameters("The error type.")]
798 impl<E: 'static> Foldable for ResultErrAppliedBrand<E> {
799 #[document_signature]
803 #[document_type_parameters(
805 "The lifetime of the values.",
806 "The brand of the cloneable function to use.",
807 "The type of the elements in the structure.",
808 "The type of the accumulator."
809 )]
810 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
812 #[document_returns("`func(a, initial)` if `fa` is `Ok(a)`, otherwise `initial`.")]
814 #[document_examples]
816 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
841 func: impl Fn(A, B) -> B + 'a,
842 initial: B,
843 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
844 ) -> B
845 where
846 FnBrand: CloneFn + 'a, {
847 match fa {
848 Ok(a) => func(a, initial),
849 Err(_) => initial,
850 }
851 }
852
853 #[document_signature]
857 #[document_type_parameters(
859 "The lifetime of the values.",
860 "The brand of the cloneable function to use.",
861 "The type of the elements in the structure.",
862 "The type of the accumulator."
863 )]
864 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
866 #[document_returns("`func(initial, a)` if `fa` is `Ok(a)`, otherwise `initial`.")]
868 #[document_examples]
870 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
895 func: impl Fn(B, A) -> B + 'a,
896 initial: B,
897 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
898 ) -> B
899 where
900 FnBrand: CloneFn + 'a, {
901 match fa {
902 Ok(a) => func(initial, a),
903 Err(_) => initial,
904 }
905 }
906
907 #[document_signature]
911 #[document_type_parameters(
913 "The lifetime of the values.",
914 "The brand of the cloneable function to use.",
915 "The type of the elements in the structure.",
916 "The type of the monoid."
917 )]
918 #[document_parameters("The mapping function.", "The result to fold.")]
920 #[document_returns("`func(a)` if `fa` is `Ok(a)`, otherwise `M::empty()`.")]
922 #[document_examples]
924 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
947 func: impl Fn(A) -> M + 'a,
948 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
949 ) -> M
950 where
951 M: Monoid + 'a,
952 FnBrand: CloneFn + 'a, {
953 match fa {
954 Ok(a) => func(a),
955 Err(_) => M::empty(),
956 }
957 }
958 }
959
960 #[document_type_parameters("The error type.")]
961 impl<E: Clone + 'static> Traversable for ResultErrAppliedBrand<E> {
962 #[document_signature]
966 #[document_type_parameters(
968 "The lifetime of the values.",
969 "The type of the elements in the traversable structure.",
970 "The type of the elements in the resulting traversable structure.",
971 "The applicative context."
972 )]
973 #[document_parameters("The function to apply.", "The result to traverse.")]
975 #[document_returns("The result wrapped in the applicative context.")]
977 #[document_examples]
979 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1009 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1010 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1011 ) -> 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>)>)
1012 where
1013 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1014 match ta {
1015 Ok(a) => F::map(|b| Ok(b), func(a)),
1016 Err(e) => F::pure(Err(e)),
1017 }
1018 }
1019
1020 #[document_signature]
1024 #[document_type_parameters(
1026 "The lifetime of the values.",
1027 "The type of the elements in the traversable structure.",
1028 "The applicative context."
1029 )]
1030 #[document_parameters("The result containing the applicative value.")]
1032 #[document_returns("The result wrapped in the applicative context.")]
1034 #[document_examples]
1036 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1054 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>)>)
1055 ) -> 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>)>)
1056 where
1057 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1058 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1059 match ta {
1060 Ok(fa) => F::map(|a| Ok(a), fa),
1061 Err(e) => F::pure(Err(e)),
1062 }
1063 }
1064 }
1065
1066 impl_kind! {
1069 #[no_inferable_brand]
1070 #[document_type_parameters("The success type.")]
1071 impl<T: 'static> for ResultOkAppliedBrand<T> {
1072 type Of<'a, A: 'a>: 'a = Result<T, A>;
1073 }
1074 }
1075
1076 #[document_type_parameters("The success type.")]
1077 impl<T: 'static> Functor for ResultOkAppliedBrand<T> {
1078 #[document_signature]
1082 #[document_type_parameters(
1084 "The lifetime of the values.",
1085 "The type of the error value inside the result.",
1086 "The type of the result of applying the function."
1087 )]
1088 #[document_parameters("The function to apply to the error.", "The result to map over.")]
1090 #[document_returns(
1092 "A new result containing the mapped error, or the original success value."
1093 )]
1094 #[document_examples]
1096 fn map<'a, A: 'a, B: 'a>(
1113 func: impl Fn(A) -> B + 'a,
1114 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1115 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1116 match fa {
1117 Ok(t) => Ok(t),
1118 Err(e) => Err(func(e)),
1119 }
1120 }
1121 }
1122
1123 #[document_type_parameters("The success type.")]
1124 impl<T: Clone + 'static> Lift for ResultOkAppliedBrand<T> {
1125 #[document_signature]
1129 #[document_type_parameters(
1131 "The lifetime of the values.",
1132 "The type of the first error value.",
1133 "The type of the second error value.",
1134 "The type of the result error value."
1135 )]
1136 #[document_parameters(
1138 "The binary function to apply to the errors.",
1139 "The first result.",
1140 "The second result."
1141 )]
1142 #[document_returns(
1144 "`Err(f(a, b))` if both results are `Err`, otherwise the first success encountered."
1145 )]
1146 #[document_examples]
1147 fn lift2<'a, A, B, C>(
1188 func: impl Fn(A, B) -> C + 'a,
1189 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1190 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1191 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1192 where
1193 A: Clone + 'a,
1194 B: Clone + 'a,
1195 C: 'a, {
1196 match (fa, fb) {
1197 (Err(a), Err(b)) => Err(func(a, b)),
1198 (Ok(t), _) => Ok(t),
1199 (_, Ok(t)) => Ok(t),
1200 }
1201 }
1202 }
1203
1204 #[document_type_parameters("The success type.")]
1205 impl<T: 'static> Pointed for ResultOkAppliedBrand<T> {
1206 #[document_signature]
1210 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1212 #[document_parameters("The value to wrap.")]
1214 #[document_returns("`Err(a)`.")]
1216 #[document_examples]
1218 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1228 Err(a)
1229 }
1230 }
1231
1232 #[document_type_parameters("The success type.")]
1233 impl<T: Clone + 'static> ApplyFirst for ResultOkAppliedBrand<T> {}
1234
1235 #[document_type_parameters("The success type.")]
1236 impl<T: Clone + 'static> ApplySecond for ResultOkAppliedBrand<T> {}
1237
1238 #[document_type_parameters("The success type.")]
1239 impl<T: Clone + 'static> Semiapplicative for ResultOkAppliedBrand<T> {
1240 #[document_signature]
1244 #[document_type_parameters(
1246 "The lifetime of the values.",
1247 "The brand of the cloneable function wrapper.",
1248 "The type of the input value.",
1249 "The type of the output value."
1250 )]
1251 #[document_parameters(
1253 "The result containing the function (in Err).",
1254 "The result containing the value (in Err)."
1255 )]
1256 #[document_returns(
1258 "`Err(f(a))` if both are `Err`, otherwise the first success encountered."
1259 )]
1260 #[document_examples]
1261 fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1280 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1281 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1282 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1283 match (ff, fa) {
1284 (Err(f), Err(a)) => Err(f(a)),
1285 (Ok(t), _) => Ok(t),
1286 (_, Ok(t)) => Ok(t),
1287 }
1288 }
1289 }
1290
1291 #[document_type_parameters("The success type.")]
1292 impl<T: Clone + 'static> Semimonad for ResultOkAppliedBrand<T> {
1293 #[document_signature]
1297 #[document_type_parameters(
1299 "The lifetime of the values.",
1300 "The type of the result of the first computation.",
1301 "The type of the result of the second computation."
1302 )]
1303 #[document_parameters("The first result.", "The function to apply to the error value.")]
1305 #[document_returns(
1307 "The result of applying `f` to the error if `ma` is `Err`, otherwise the original success."
1308 )]
1309 #[document_examples]
1311 fn bind<'a, A: 'a, B: 'a>(
1332 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1333 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1334 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1335 match ma {
1336 Ok(t) => Ok(t),
1337 Err(e) => func(e),
1338 }
1339 }
1340 }
1341
1342 #[document_type_parameters("The success type.")]
1343 impl<T: 'static> Foldable for ResultOkAppliedBrand<T> {
1344 #[document_signature]
1348 #[document_type_parameters(
1350 "The lifetime of the values.",
1351 "The brand of the cloneable function to use.",
1352 "The type of the elements in the structure.",
1353 "The type of the accumulator."
1354 )]
1355 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1357 #[document_returns("`func(a, initial)` if `fa` is `Err(a)`, otherwise `initial`.")]
1359 #[document_examples]
1361 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1386 func: impl Fn(A, B) -> B + 'a,
1387 initial: B,
1388 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1389 ) -> B
1390 where
1391 FnBrand: CloneFn + 'a, {
1392 match fa {
1393 Err(e) => func(e, initial),
1394 Ok(_) => initial,
1395 }
1396 }
1397
1398 #[document_signature]
1402 #[document_type_parameters(
1404 "The lifetime of the values.",
1405 "The brand of the cloneable function to use.",
1406 "The type of the elements in the structure.",
1407 "The type of the accumulator."
1408 )]
1409 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1411 #[document_returns("`func(initial, a)` if `fa` is `Err(a)`, otherwise `initial`.")]
1413 #[document_examples]
1415 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1440 func: impl Fn(B, A) -> B + 'a,
1441 initial: B,
1442 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1443 ) -> B
1444 where
1445 FnBrand: CloneFn + 'a, {
1446 match fa {
1447 Err(e) => func(initial, e),
1448 Ok(_) => initial,
1449 }
1450 }
1451
1452 #[document_signature]
1456 #[document_type_parameters(
1458 "The lifetime of the values.",
1459 "The brand of the cloneable function to use.",
1460 "The type of the elements in the structure.",
1461 "The type of the monoid."
1462 )]
1463 #[document_parameters("The mapping function.", "The result to fold.")]
1465 #[document_returns("`func(a)` if `fa` is `Err(a)`, otherwise `M::empty()`.")]
1467 #[document_examples]
1469 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1492 func: impl Fn(A) -> M + 'a,
1493 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1494 ) -> M
1495 where
1496 M: Monoid + 'a,
1497 FnBrand: CloneFn + 'a, {
1498 match fa {
1499 Err(e) => func(e),
1500 Ok(_) => M::empty(),
1501 }
1502 }
1503 }
1504
1505 #[document_type_parameters("The success type.")]
1506 impl<T: Clone + 'static> Traversable for ResultOkAppliedBrand<T> {
1507 #[document_signature]
1511 #[document_type_parameters(
1513 "The lifetime of the values.",
1514 "The type of the elements in the traversable structure.",
1515 "The type of the elements in the resulting traversable structure.",
1516 "The applicative context."
1517 )]
1518 #[document_parameters("The function to apply.", "The result to traverse.")]
1520 #[document_returns("The result wrapped in the applicative context.")]
1522 #[document_examples]
1524 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1554 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1555 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1556 ) -> 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>)>)
1557 where
1558 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1559 match ta {
1560 Err(e) => F::map(|b| Err(b), func(e)),
1561 Ok(t) => F::pure(Ok(t)),
1562 }
1563 }
1564
1565 #[document_signature]
1569 #[document_type_parameters(
1571 "The lifetime of the values.",
1572 "The type of the elements in the traversable structure.",
1573 "The applicative context."
1574 )]
1575 #[document_parameters("The result containing the applicative value.")]
1577 #[document_returns("The result wrapped in the applicative context.")]
1579 #[document_examples]
1581 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1599 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>)>)
1600 ) -> 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>)>)
1601 where
1602 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1603 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1604 match ta {
1605 Err(fe) => F::map(|e| Err(e), fe),
1606 Ok(t) => F::pure(Ok(t)),
1607 }
1608 }
1609 }
1610
1611 #[document_type_parameters("The success type.")]
1613 impl<T: Clone + 'static> MonadRec for ResultOkAppliedBrand<T> {
1614 #[document_signature]
1621 #[document_type_parameters(
1623 "The lifetime of the computation.",
1624 "The type of the initial value and loop state.",
1625 "The type of the result."
1626 )]
1627 #[document_parameters("The step function.", "The initial value.")]
1629 #[document_returns(
1631 "The result of the computation, or a success if the step function returned `Ok`."
1632 )]
1633 #[document_examples]
1635 fn tail_rec_m<'a, A: 'a, B: 'a>(
1655 func: impl Fn(
1656 A,
1657 )
1658 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1659 + 'a,
1660 initial: A,
1661 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1662 let mut current = initial;
1663 loop {
1664 match func(current) {
1665 Ok(t) => return Ok(t),
1666 Err(ControlFlow::Continue(next)) => current = next,
1667 Err(ControlFlow::Break(b)) => return Err(b),
1668 }
1669 }
1670 }
1671 }
1672
1673 #[document_type_parameters("The error type.")]
1676 impl<E: Clone + 'static> RefFunctor for ResultErrAppliedBrand<E> {
1677 #[document_signature]
1679 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1680 #[document_parameters("The function.", "The result.")]
1681 #[document_returns("The mapped result.")]
1682 #[document_examples]
1683 fn ref_map<'a, A: 'a, B: 'a>(
1702 func: impl Fn(&A) -> B + 'a,
1703 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1704 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1705 match fa {
1706 Ok(a) => Ok(func(a)),
1707 Err(e) => Err(e.clone()),
1708 }
1709 }
1710 }
1711
1712 #[document_type_parameters("The error type.")]
1713 impl<E: Clone + 'static> RefFoldable for ResultErrAppliedBrand<E> {
1714 #[document_signature]
1716 #[document_type_parameters(
1717 "The lifetime.",
1718 "The brand.",
1719 "The element type.",
1720 "The monoid type."
1721 )]
1722 #[document_parameters("The mapping function.", "The result.")]
1723 #[document_returns("The monoid value.")]
1724 #[document_examples]
1725 fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1738 func: impl Fn(&A) -> M + 'a,
1739 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1740 ) -> M
1741 where
1742 FnBrand: LiftFn + 'a,
1743 M: Monoid + 'a, {
1744 match fa {
1745 Ok(a) => func(a),
1746 Err(_) => Monoid::empty(),
1747 }
1748 }
1749 }
1750
1751 #[document_type_parameters("The error type.")]
1752 impl<E: Clone + 'static> RefTraversable for ResultErrAppliedBrand<E> {
1753 #[document_signature]
1755 #[document_type_parameters(
1756 "The lifetime.",
1757 "The brand.",
1758 "The input type.",
1759 "The output type.",
1760 "The applicative."
1761 )]
1762 #[document_parameters("The function.", "The result.")]
1763 #[document_returns("The traversed result.")]
1764 #[document_examples]
1765 fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1779 func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1780 ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1781 ) -> 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>)>)
1782 where
1783 FnBrand: LiftFn + 'a,
1784 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1785 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1786 match ta {
1787 Ok(a) => F::map(Ok, func(a)),
1788 Err(e) => F::pure(Err(e.clone())),
1789 }
1790 }
1791 }
1792
1793 #[document_type_parameters("The error type.")]
1794 impl<E: 'static> RefPointed for ResultErrAppliedBrand<E> {
1795 #[document_signature]
1797 #[document_type_parameters("The lifetime of the value.", "The type of the value.")]
1798 #[document_parameters("The reference to the value to wrap.")]
1799 #[document_returns("An `Ok` containing a clone of the value.")]
1800 #[document_examples]
1801 fn ref_pure<'a, A: Clone + 'a>(
1813 a: &A
1814 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1815 Ok(a.clone())
1816 }
1817 }
1818
1819 #[document_type_parameters("The error type.")]
1820 impl<E: Clone + 'static> RefLift for ResultErrAppliedBrand<E> {
1821 #[document_signature]
1823 #[document_type_parameters(
1824 "The lifetime.",
1825 "First input type.",
1826 "Second input type.",
1827 "Output type."
1828 )]
1829 #[document_parameters("The binary function.", "The first result.", "The second result.")]
1830 #[document_returns("The combined result, or the first error encountered.")]
1831 #[document_examples]
1832 fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1847 func: impl Fn(&A, &B) -> C + 'a,
1848 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1849 fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1850 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1851 match (fa, fb) {
1852 (Ok(a), Ok(b)) => Ok(func(a, b)),
1853 (Err(e), _) => Err(e.clone()),
1854 (_, Err(e)) => Err(e.clone()),
1855 }
1856 }
1857 }
1858
1859 #[document_type_parameters("The error type.")]
1860 impl<E: Clone + 'static> RefSemiapplicative for ResultErrAppliedBrand<E> {
1861 #[document_signature]
1863 #[document_type_parameters(
1864 "The lifetime.",
1865 "The function brand.",
1866 "The input type.",
1867 "The output type."
1868 )]
1869 #[document_parameters(
1870 "The result containing the by-ref function.",
1871 "The result containing the value."
1872 )]
1873 #[document_returns("The result of applying the function, or the first error.")]
1874 #[document_examples]
1875 fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1888 ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1889 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1890 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1891 match (ff, fa) {
1892 (Ok(f), Ok(a)) => Ok((**f)(a)),
1893 (Err(e), _) => Err(e.clone()),
1894 (_, Err(e)) => Err(e.clone()),
1895 }
1896 }
1897 }
1898
1899 #[document_type_parameters("The error type.")]
1900 impl<E: Clone + 'static> RefSemimonad for ResultErrAppliedBrand<E> {
1901 #[document_signature]
1903 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1904 #[document_parameters("The input result.", "The function to apply by reference.")]
1905 #[document_returns("The result of applying the function, or the original error.")]
1906 #[document_examples]
1907 fn ref_bind<'a, A: 'a, B: 'a>(
1921 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1922 f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1923 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1924 match fa {
1925 Ok(a) => f(a),
1926 Err(e) => Err(e.clone()),
1927 }
1928 }
1929 }
1930
1931 #[document_type_parameters("The success type.")]
1934 impl<T: Clone + 'static> RefFunctor for ResultOkAppliedBrand<T> {
1935 #[document_signature]
1937 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1938 #[document_parameters("The function.", "The result.")]
1939 #[document_returns("The mapped result.")]
1940 #[document_examples]
1941 fn ref_map<'a, A: 'a, B: 'a>(
1957 func: impl Fn(&A) -> B + 'a,
1958 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1959 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1960 match fa {
1961 Err(e) => Err(func(e)),
1962 Ok(t) => Ok(t.clone()),
1963 }
1964 }
1965 }
1966
1967 #[document_type_parameters("The success type.")]
1968 impl<T: Clone + 'static> RefFoldable for ResultOkAppliedBrand<T> {
1969 #[document_signature]
1971 #[document_type_parameters(
1972 "The lifetime.",
1973 "The brand.",
1974 "The element type.",
1975 "The monoid type."
1976 )]
1977 #[document_parameters("The mapping function.", "The result.")]
1978 #[document_returns("The monoid value.")]
1979 #[document_examples]
1980 fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1993 func: impl Fn(&A) -> M + 'a,
1994 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1995 ) -> M
1996 where
1997 FnBrand: LiftFn + 'a,
1998 M: Monoid + 'a, {
1999 match fa {
2000 Err(e) => func(e),
2001 Ok(_) => Monoid::empty(),
2002 }
2003 }
2004 }
2005
2006 #[document_type_parameters("The success type.")]
2007 impl<T: Clone + 'static> RefTraversable for ResultOkAppliedBrand<T> {
2008 #[document_signature]
2010 #[document_type_parameters(
2011 "The lifetime.",
2012 "The brand.",
2013 "The input type.",
2014 "The output type.",
2015 "The applicative."
2016 )]
2017 #[document_parameters("The function.", "The result.")]
2018 #[document_returns("The traversed result.")]
2019 #[document_examples]
2020 fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2034 func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2035 ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2036 ) -> 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>)>)
2037 where
2038 FnBrand: LiftFn + 'a,
2039 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
2040 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2041 match ta {
2042 Err(e) => F::map(Err, func(e)),
2043 Ok(t) => F::pure(Ok(t.clone())),
2044 }
2045 }
2046 }
2047
2048 #[document_type_parameters("The success type.")]
2049 impl<T: 'static> RefPointed for ResultOkAppliedBrand<T> {
2050 #[document_signature]
2052 #[document_type_parameters("The lifetime of the value.", "The type of the value.")]
2053 #[document_parameters("The reference to the value to wrap.")]
2054 #[document_returns("An `Err` containing a clone of the value.")]
2055 #[document_examples]
2056 fn ref_pure<'a, A: Clone + 'a>(
2068 a: &A
2069 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
2070 Err(a.clone())
2071 }
2072 }
2073
2074 #[document_type_parameters("The success type.")]
2075 impl<T: Clone + 'static> RefLift for ResultOkAppliedBrand<T> {
2076 #[document_signature]
2078 #[document_type_parameters(
2079 "The lifetime.",
2080 "First input type.",
2081 "Second input type.",
2082 "Output type."
2083 )]
2084 #[document_parameters("The binary function.", "The first result.", "The second result.")]
2085 #[document_returns("The combined result, or the first success encountered.")]
2086 #[document_examples]
2087 fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
2102 func: impl Fn(&A, &B) -> C + 'a,
2103 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2104 fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
2105 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
2106 match (fa, fb) {
2107 (Err(a), Err(b)) => Err(func(a, b)),
2108 (Ok(t), _) => Ok(t.clone()),
2109 (_, Ok(t)) => Ok(t.clone()),
2110 }
2111 }
2112 }
2113
2114 #[document_type_parameters("The success type.")]
2115 impl<T: Clone + 'static> RefSemiapplicative for ResultOkAppliedBrand<T> {
2116 #[document_signature]
2118 #[document_type_parameters(
2119 "The lifetime.",
2120 "The function brand.",
2121 "The input type.",
2122 "The output type."
2123 )]
2124 #[document_parameters(
2125 "The result containing the by-ref function (in Err).",
2126 "The result containing the value (in Err)."
2127 )]
2128 #[document_returns("The result of applying the function, or the first success.")]
2129 #[document_examples]
2130 fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
2143 ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
2144 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2145 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2146 match (ff, fa) {
2147 (Err(f), Err(a)) => Err((**f)(a)),
2148 (Ok(t), _) => Ok(t.clone()),
2149 (_, Ok(t)) => Ok(t.clone()),
2150 }
2151 }
2152 }
2153
2154 #[document_type_parameters("The success type.")]
2155 impl<T: Clone + 'static> RefSemimonad for ResultOkAppliedBrand<T> {
2156 #[document_signature]
2158 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
2159 #[document_parameters("The input result.", "The function to apply by reference.")]
2160 #[document_returns("The result of applying the function, or the original success.")]
2161 #[document_examples]
2162 fn ref_bind<'a, A: 'a, B: 'a>(
2176 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2177 f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2178 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2179 match fa {
2180 Err(e) => f(e),
2181 Ok(t) => Ok(t.clone()),
2182 }
2183 }
2184 }
2185
2186 #[document_type_parameters("The error type.")]
2188 impl<E: Clone + 'static> MonadRec for ResultErrAppliedBrand<E> {
2189 #[document_signature]
2196 #[document_type_parameters(
2198 "The lifetime of the computation.",
2199 "The type of the initial value and loop state.",
2200 "The type of the result."
2201 )]
2202 #[document_parameters("The step function.", "The initial value.")]
2204 #[document_returns(
2206 "The result of the computation, or an error if the step function returned `Err`."
2207 )]
2208 #[document_examples]
2210 fn tail_rec_m<'a, A: 'a, B: 'a>(
2230 func: impl Fn(
2231 A,
2232 )
2233 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2234 + 'a,
2235 initial: A,
2236 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2237 let mut current = initial;
2238 loop {
2239 match func(current) {
2240 Err(e) => return Err(e),
2241 Ok(ControlFlow::Continue(next)) => current = next,
2242 Ok(ControlFlow::Break(b)) => return Ok(b),
2243 }
2244 }
2245 }
2246 }
2247}
2248
2249#[cfg(test)]
2250mod tests {
2251
2252 use {
2253 crate::{
2254 brands::*,
2255 classes::*,
2256 functions::*,
2257 },
2258 quickcheck_macros::quickcheck,
2259 };
2260
2261 #[test]
2265 fn test_bimap() {
2266 let x: Result<i32, i32> = Ok(5);
2267 assert_eq!(
2268 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x),
2269 Ok(10)
2270 );
2271
2272 let y: Result<i32, i32> = Err(5);
2273 assert_eq!(
2274 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), y),
2275 Err(6)
2276 );
2277 }
2278
2279 #[quickcheck]
2283 fn bifunctor_identity(x: Result<i32, i32>) -> bool {
2284 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((identity, identity), x) == x
2285 }
2286
2287 #[quickcheck]
2289 fn bifunctor_composition(x: Result<i32, i32>) -> bool {
2290 let f = |x: i32| x.wrapping_add(1);
2291 let g = |x: i32| x.wrapping_mul(2);
2292 let h = |x: i32| x.wrapping_sub(1);
2293 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2294
2295 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2296 == explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2297 (f, h),
2298 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((g, i), x),
2299 )
2300 }
2301
2302 #[quickcheck]
2306 fn functor_identity(x: Result<i32, i32>) -> bool {
2307 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2308 }
2309
2310 #[quickcheck]
2312 fn functor_composition(x: Result<i32, i32>) -> bool {
2313 let f = |x: i32| x.wrapping_add(1);
2314 let g = |x: i32| x.wrapping_mul(2);
2315 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2316 == explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2317 f,
2318 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(g, x),
2319 )
2320 }
2321
2322 #[quickcheck]
2326 fn applicative_identity(v: Result<i32, i32>) -> bool {
2327 apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2328 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(identity)),
2329 v,
2330 ) == v
2331 }
2332
2333 #[quickcheck]
2335 fn applicative_homomorphism(x: i32) -> bool {
2336 let f = |x: i32| x.wrapping_mul(2);
2337 apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2338 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f)),
2339 pure::<ResultErrAppliedBrand<i32>, _>(x),
2340 ) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
2341 }
2342
2343 #[quickcheck]
2345 fn applicative_composition(
2346 w: Result<i32, i32>,
2347 u_is_ok: bool,
2348 v_is_ok: bool,
2349 ) -> bool {
2350 let v_fn = |x: i32| x.wrapping_mul(2);
2351 let u_fn = |x: i32| x.wrapping_add(1);
2352
2353 let v = if v_is_ok {
2354 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(v_fn))
2355 } else {
2356 Err(100)
2357 };
2358 let u = if u_is_ok {
2359 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(u_fn))
2360 } else {
2361 Err(200)
2362 };
2363
2364 let vw = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
2366 let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
2367
2368 let uv = match (u, v) {
2371 (Ok(uf), Ok(vf)) => {
2372 let composed = move |x| uf(vf(x));
2373 Ok(<RcFnBrand as LiftFn>::new(composed))
2374 }
2375 (Err(e), _) => Err(e),
2376 (_, Err(e)) => Err(e),
2377 };
2378
2379 let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
2380
2381 lhs == rhs
2382 }
2383
2384 #[quickcheck]
2386 fn applicative_interchange(y: i32) -> bool {
2387 let f = |x: i32| x.wrapping_mul(2);
2389 let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f));
2390
2391 let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2392 u.clone(),
2393 pure::<ResultErrAppliedBrand<i32>, _>(y),
2394 );
2395
2396 let rhs_fn = <RcFnBrand as LiftFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
2397 let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2398 pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
2399 u,
2400 );
2401
2402 lhs == rhs
2403 }
2404
2405 #[quickcheck]
2409 fn monad_left_identity(a: i32) -> bool {
2410 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2411 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2412 pure::<ResultErrAppliedBrand<i32>, _>(a),
2413 f,
2414 ) == f(a)
2415 }
2416
2417 #[quickcheck]
2419 fn monad_right_identity(m: Result<i32, i32>) -> bool {
2420 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2421 m,
2422 pure::<ResultErrAppliedBrand<i32>, _>,
2423 ) == m
2424 }
2425
2426 #[quickcheck]
2428 fn monad_associativity(m: Result<i32, i32>) -> bool {
2429 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2430 let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
2431 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2432 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, f),
2433 g,
2434 ) == explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, |x| {
2435 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(f(x), g)
2436 })
2437 }
2438
2439 #[test]
2443 fn map_err() {
2444 assert_eq!(
2445 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2446 |x: i32| x + 1,
2447 Err::<i32, i32>(1)
2448 ),
2449 Err(1)
2450 );
2451 }
2452
2453 #[test]
2455 fn bind_err() {
2456 assert_eq!(
2457 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2458 Err::<i32, i32>(1),
2459 |x: i32| Ok(x + 1)
2460 ),
2461 Err(1)
2462 );
2463 }
2464
2465 #[test]
2467 fn bind_returning_err() {
2468 assert_eq!(
2469 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Ok(1), |_| Err::<i32, i32>(2)),
2470 Err(2)
2471 );
2472 }
2473
2474 #[test]
2476 fn fold_right_err() {
2477 assert_eq!(
2478 crate::functions::explicit::fold_right::<
2479 RcFnBrand,
2480 ResultErrAppliedBrand<i32>,
2481 _,
2482 _,
2483 _,
2484 _,
2485 >(|x: i32, acc| x + acc, 0, Err(1)),
2486 0
2487 );
2488 }
2489
2490 #[test]
2492 fn fold_left_err() {
2493 assert_eq!(
2494 crate::functions::explicit::fold_left::<
2495 RcFnBrand,
2496 ResultErrAppliedBrand<i32>,
2497 _,
2498 _,
2499 _,
2500 _,
2501 >(|acc, x: i32| acc + x, 0, Err(1)),
2502 0
2503 );
2504 }
2505
2506 #[test]
2508 fn traverse_err() {
2509 assert_eq!(
2510 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2511 |x: i32| Some(x + 1),
2512 Err(1)
2513 ),
2514 Some(Err(1))
2515 );
2516 }
2517
2518 #[test]
2520 fn traverse_returning_err() {
2521 assert_eq!(
2522 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2523 |_: i32| None::<i32>,
2524 Ok(1)
2525 ),
2526 None
2527 );
2528 }
2529
2530 #[quickcheck]
2534 fn monad_rec_identity(x: i32) -> bool {
2535 use {
2536 crate::classes::monad_rec::tail_rec_m,
2537 core::ops::ControlFlow,
2538 };
2539 tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(|a| Ok(ControlFlow::Break(a)), x) == Ok(x)
2540 }
2541
2542 #[test]
2544 fn monad_rec_sum_range() {
2545 use {
2546 crate::classes::monad_rec::tail_rec_m,
2547 core::ops::ControlFlow,
2548 };
2549 let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2550 |(n, acc)| {
2551 if n == 0 {
2552 Ok(ControlFlow::Break(acc))
2553 } else {
2554 Ok(ControlFlow::Continue((n - 1, acc + n)))
2555 }
2556 },
2557 (100i64, 0i64),
2558 );
2559 assert_eq!(result, Ok(5050));
2560 }
2561
2562 #[test]
2564 fn monad_rec_short_circuit() {
2565 use {
2566 crate::classes::monad_rec::tail_rec_m,
2567 core::ops::ControlFlow,
2568 };
2569 let result: Result<i32, &str> = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2570 |n| {
2571 if n == 5 { Err("stopped") } else { Ok(ControlFlow::Continue(n + 1)) }
2572 },
2573 0,
2574 );
2575 assert_eq!(result, Err("stopped"));
2576 }
2577
2578 #[test]
2580 fn monad_rec_stack_safety() {
2581 use {
2582 crate::classes::monad_rec::tail_rec_m,
2583 core::ops::ControlFlow,
2584 };
2585 let iterations: i64 = 200_000;
2586 let result = tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(
2587 |acc| {
2588 if acc < iterations {
2589 Ok(ControlFlow::Continue(acc + 1))
2590 } else {
2591 Ok(ControlFlow::Break(acc))
2592 }
2593 },
2594 0i64,
2595 );
2596 assert_eq!(result, Ok(iterations));
2597 }
2598
2599 #[quickcheck]
2604 fn monad_rec_ok_applied_identity(x: i32) -> bool {
2605 use {
2606 crate::classes::monad_rec::tail_rec_m,
2607 core::ops::ControlFlow,
2608 };
2609 tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(|a| Err(ControlFlow::Break(a)), x) == Err(x)
2610 }
2611
2612 #[test]
2614 fn monad_rec_ok_applied_sum_range() {
2615 use {
2616 crate::classes::monad_rec::tail_rec_m,
2617 core::ops::ControlFlow,
2618 };
2619 let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2620 |(n, acc)| {
2621 if n == 0 {
2622 Err(ControlFlow::Break(acc))
2623 } else {
2624 Err(ControlFlow::Continue((n - 1, acc + n)))
2625 }
2626 },
2627 (100i64, 0i64),
2628 );
2629 assert_eq!(result, Err(5050));
2630 }
2631
2632 #[test]
2634 fn monad_rec_ok_applied_short_circuit() {
2635 use {
2636 crate::classes::monad_rec::tail_rec_m,
2637 core::ops::ControlFlow,
2638 };
2639 let result: Result<&str, i32> = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2640 |n| {
2641 if n == 5 { Ok("stopped") } else { Err(ControlFlow::Continue(n + 1)) }
2642 },
2643 0,
2644 );
2645 assert_eq!(result, Ok("stopped"));
2646 }
2647
2648 #[test]
2650 fn monad_rec_ok_applied_stack_safety() {
2651 use {
2652 crate::classes::monad_rec::tail_rec_m,
2653 core::ops::ControlFlow,
2654 };
2655 let iterations: i64 = 200_000;
2656 let result = tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(
2657 |acc| {
2658 if acc < iterations {
2659 Err(ControlFlow::Continue(acc + 1))
2660 } else {
2661 Err(ControlFlow::Break(acc))
2662 }
2663 },
2664 0i64,
2665 );
2666 assert_eq!(result, Err(iterations));
2667 }
2668
2669 #[quickcheck]
2673 fn ref_bifunctor_identity(x: Result<i32, i32>) -> bool {
2674 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|a: &i32| *a, |c: &i32| *c), &x) == x
2675 }
2676
2677 #[quickcheck]
2679 fn ref_bifunctor_composition(x: Result<i32, i32>) -> bool {
2680 let f1 = |a: &i32| a.wrapping_add(1);
2681 let f2 = |a: &i32| a.wrapping_mul(2);
2682 let g1 = |c: &i32| c.wrapping_add(10);
2683 let g2 = |c: &i32| c.wrapping_mul(3);
2684 explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2685 (|a: &i32| f2(&f1(a)), |c: &i32| g2(&g1(c))),
2686 &x,
2687 ) == explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2688 (f2, g2),
2689 &explicit::bimap::<ResultBrand, _, _, _, _, _, _>((f1, g1), &x),
2690 )
2691 }
2692
2693 #[quickcheck]
2697 fn ref_bifoldable_fold_map(x: Result<i32, i32>) -> bool {
2698 let result = explicit::bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
2699 (|a: &i32| a.to_string(), |b: &i32| b.to_string()),
2700 &x,
2701 );
2702 let expected = match &x {
2703 Err(a) => a.to_string(),
2704 Ok(b) => b.to_string(),
2705 };
2706 result == expected
2707 }
2708
2709 #[quickcheck]
2713 fn ref_bitraversable_consistency(x: Result<i32, i32>) -> bool {
2714 let f = |a: &i32| Some(a.wrapping_add(1));
2715 let g = |c: &i32| Some(c.wrapping_mul(2));
2716 let traversed =
2717 explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
2718 (f, g),
2719 &x,
2720 );
2721 let mapped_then_sequenced =
2722 ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&explicit::bimap::<
2723 ResultBrand,
2724 _,
2725 _,
2726 _,
2727 _,
2728 _,
2729 _,
2730 >((f, g), &x));
2731 traversed == mapped_then_sequenced
2732 }
2733}