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 #[multi_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>(
739 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
740 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
741 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
742 match (ff, fa) {
743 (Ok(f), Ok(a)) => Ok(f(a)),
744 (Err(e), _) => Err(e),
745 (_, Err(e)) => Err(e),
746 }
747 }
748 }
749
750 #[document_type_parameters("The error type.")]
751 impl<E: Clone + 'static> Semimonad for ResultErrAppliedBrand<E> {
752 #[document_signature]
756 #[document_type_parameters(
758 "The lifetime of the values.",
759 "The type of the result of the first computation.",
760 "The type of the result of the second computation."
761 )]
762 #[document_parameters(
764 "The first result.",
765 "The function to apply to the value inside the result."
766 )]
767 #[document_returns(
769 "The result of applying `f` to the value if `ma` is `Ok`, otherwise the original error."
770 )]
771 #[document_examples]
772 fn bind<'a, A: 'a, B: 'a>(
793 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
794 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
795 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
796 ma.and_then(func)
797 }
798 }
799
800 #[document_type_parameters("The error type.")]
801 impl<E: 'static> Foldable for ResultErrAppliedBrand<E> {
802 #[document_signature]
806 #[document_type_parameters(
808 "The lifetime of the values.",
809 "The brand of the cloneable function to use.",
810 "The type of the elements in the structure.",
811 "The type of the accumulator."
812 )]
813 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
815 #[document_returns("`func(a, initial)` if `fa` is `Ok(a)`, otherwise `initial`.")]
817 #[document_examples]
819 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
844 func: impl Fn(A, B) -> B + 'a,
845 initial: B,
846 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
847 ) -> B
848 where
849 FnBrand: CloneFn + 'a, {
850 match fa {
851 Ok(a) => func(a, initial),
852 Err(_) => initial,
853 }
854 }
855
856 #[document_signature]
860 #[document_type_parameters(
862 "The lifetime of the values.",
863 "The brand of the cloneable function to use.",
864 "The type of the elements in the structure.",
865 "The type of the accumulator."
866 )]
867 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
869 #[document_returns("`func(initial, a)` if `fa` is `Ok(a)`, otherwise `initial`.")]
871 #[document_examples]
873 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
898 func: impl Fn(B, A) -> B + 'a,
899 initial: B,
900 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
901 ) -> B
902 where
903 FnBrand: CloneFn + 'a, {
904 match fa {
905 Ok(a) => func(initial, a),
906 Err(_) => initial,
907 }
908 }
909
910 #[document_signature]
914 #[document_type_parameters(
916 "The lifetime of the values.",
917 "The brand of the cloneable function to use.",
918 "The type of the elements in the structure.",
919 "The type of the monoid."
920 )]
921 #[document_parameters("The mapping function.", "The result to fold.")]
923 #[document_returns("`func(a)` if `fa` is `Ok(a)`, otherwise `M::empty()`.")]
925 #[document_examples]
927 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
950 func: impl Fn(A) -> M + 'a,
951 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
952 ) -> M
953 where
954 M: Monoid + 'a,
955 FnBrand: CloneFn + 'a, {
956 match fa {
957 Ok(a) => func(a),
958 Err(_) => M::empty(),
959 }
960 }
961 }
962
963 #[document_type_parameters("The error type.")]
964 impl<E: Clone + 'static> Traversable for ResultErrAppliedBrand<E> {
965 #[document_signature]
969 #[document_type_parameters(
971 "The lifetime of the values.",
972 "The type of the elements in the traversable structure.",
973 "The type of the elements in the resulting traversable structure.",
974 "The applicative context."
975 )]
976 #[document_parameters("The function to apply.", "The result to traverse.")]
978 #[document_returns("The result wrapped in the applicative context.")]
980 #[document_examples]
982 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1012 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1013 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1014 ) -> 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>)>)
1015 where
1016 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1017 match ta {
1018 Ok(a) => F::map(|b| Ok(b), func(a)),
1019 Err(e) => F::pure(Err(e)),
1020 }
1021 }
1022
1023 #[document_signature]
1027 #[document_type_parameters(
1029 "The lifetime of the values.",
1030 "The type of the elements in the traversable structure.",
1031 "The applicative context."
1032 )]
1033 #[document_parameters("The result containing the applicative value.")]
1035 #[document_returns("The result wrapped in the applicative context.")]
1037 #[document_examples]
1039 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1057 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>)>)
1058 ) -> 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>)>)
1059 where
1060 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1061 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1062 match ta {
1063 Ok(fa) => F::map(|a| Ok(a), fa),
1064 Err(e) => F::pure(Err(e)),
1065 }
1066 }
1067 }
1068
1069 impl_kind! {
1072 #[multi_brand]
1073 #[document_type_parameters("The success type.")]
1074 impl<T: 'static> for ResultOkAppliedBrand<T> {
1075 type Of<'a, A: 'a>: 'a = Result<T, A>;
1076 }
1077 }
1078
1079 #[document_type_parameters("The success type.")]
1080 impl<T: 'static> Functor for ResultOkAppliedBrand<T> {
1081 #[document_signature]
1085 #[document_type_parameters(
1087 "The lifetime of the values.",
1088 "The type of the error value inside the result.",
1089 "The type of the result of applying the function."
1090 )]
1091 #[document_parameters("The function to apply to the error.", "The result to map over.")]
1093 #[document_returns(
1095 "A new result containing the mapped error, or the original success value."
1096 )]
1097 #[document_examples]
1099 fn map<'a, A: 'a, B: 'a>(
1116 func: impl Fn(A) -> B + 'a,
1117 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1118 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1119 match fa {
1120 Ok(t) => Ok(t),
1121 Err(e) => Err(func(e)),
1122 }
1123 }
1124 }
1125
1126 #[document_type_parameters("The success type.")]
1127 impl<T: Clone + 'static> Lift for ResultOkAppliedBrand<T> {
1128 #[document_signature]
1132 #[document_type_parameters(
1134 "The lifetime of the values.",
1135 "The type of the first error value.",
1136 "The type of the second error value.",
1137 "The type of the result error value."
1138 )]
1139 #[document_parameters(
1141 "The binary function to apply to the errors.",
1142 "The first result.",
1143 "The second result."
1144 )]
1145 #[document_returns(
1147 "`Err(f(a, b))` if both results are `Err`, otherwise the first success encountered."
1148 )]
1149 #[document_examples]
1150 fn lift2<'a, A, B, C>(
1191 func: impl Fn(A, B) -> C + 'a,
1192 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1193 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1194 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1195 where
1196 A: Clone + 'a,
1197 B: Clone + 'a,
1198 C: 'a, {
1199 match (fa, fb) {
1200 (Err(a), Err(b)) => Err(func(a, b)),
1201 (Ok(t), _) => Ok(t),
1202 (_, Ok(t)) => Ok(t),
1203 }
1204 }
1205 }
1206
1207 #[document_type_parameters("The success type.")]
1208 impl<T: 'static> Pointed for ResultOkAppliedBrand<T> {
1209 #[document_signature]
1213 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1215 #[document_parameters("The value to wrap.")]
1217 #[document_returns("`Err(a)`.")]
1219 #[document_examples]
1221 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1231 Err(a)
1232 }
1233 }
1234
1235 #[document_type_parameters("The success type.")]
1236 impl<T: Clone + 'static> ApplyFirst for ResultOkAppliedBrand<T> {}
1237
1238 #[document_type_parameters("The success type.")]
1239 impl<T: Clone + 'static> ApplySecond for ResultOkAppliedBrand<T> {}
1240
1241 #[document_type_parameters("The success type.")]
1242 impl<T: Clone + 'static> Semiapplicative for ResultOkAppliedBrand<T> {
1243 #[document_signature]
1247 #[document_type_parameters(
1249 "The lifetime of the values.",
1250 "The brand of the cloneable function wrapper.",
1251 "The type of the input value.",
1252 "The type of the output value."
1253 )]
1254 #[document_parameters(
1256 "The result containing the function (in Err).",
1257 "The result containing the value (in Err)."
1258 )]
1259 #[document_returns(
1261 "`Err(f(a))` if both are `Err`, otherwise the first success encountered."
1262 )]
1263 #[document_examples]
1264 fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1286 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1287 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1288 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1289 match (ff, fa) {
1290 (Err(f), Err(a)) => Err(f(a)),
1291 (Ok(t), _) => Ok(t),
1292 (_, Ok(t)) => Ok(t),
1293 }
1294 }
1295 }
1296
1297 #[document_type_parameters("The success type.")]
1298 impl<T: Clone + 'static> Semimonad for ResultOkAppliedBrand<T> {
1299 #[document_signature]
1303 #[document_type_parameters(
1305 "The lifetime of the values.",
1306 "The type of the result of the first computation.",
1307 "The type of the result of the second computation."
1308 )]
1309 #[document_parameters("The first result.", "The function to apply to the error value.")]
1311 #[document_returns(
1313 "The result of applying `f` to the error if `ma` is `Err`, otherwise the original success."
1314 )]
1315 #[document_examples]
1317 fn bind<'a, A: 'a, B: 'a>(
1338 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1339 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1340 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1341 match ma {
1342 Ok(t) => Ok(t),
1343 Err(e) => func(e),
1344 }
1345 }
1346 }
1347
1348 #[document_type_parameters("The success type.")]
1349 impl<T: 'static> Foldable for ResultOkAppliedBrand<T> {
1350 #[document_signature]
1354 #[document_type_parameters(
1356 "The lifetime of the values.",
1357 "The brand of the cloneable function to use.",
1358 "The type of the elements in the structure.",
1359 "The type of the accumulator."
1360 )]
1361 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1363 #[document_returns("`func(a, initial)` if `fa` is `Err(a)`, otherwise `initial`.")]
1365 #[document_examples]
1367 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1392 func: impl Fn(A, B) -> B + 'a,
1393 initial: B,
1394 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1395 ) -> B
1396 where
1397 FnBrand: CloneFn + 'a, {
1398 match fa {
1399 Err(e) => func(e, initial),
1400 Ok(_) => initial,
1401 }
1402 }
1403
1404 #[document_signature]
1408 #[document_type_parameters(
1410 "The lifetime of the values.",
1411 "The brand of the cloneable function to use.",
1412 "The type of the elements in the structure.",
1413 "The type of the accumulator."
1414 )]
1415 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1417 #[document_returns("`func(initial, a)` if `fa` is `Err(a)`, otherwise `initial`.")]
1419 #[document_examples]
1421 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1446 func: impl Fn(B, A) -> B + 'a,
1447 initial: B,
1448 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1449 ) -> B
1450 where
1451 FnBrand: CloneFn + 'a, {
1452 match fa {
1453 Err(e) => func(initial, e),
1454 Ok(_) => initial,
1455 }
1456 }
1457
1458 #[document_signature]
1462 #[document_type_parameters(
1464 "The lifetime of the values.",
1465 "The brand of the cloneable function to use.",
1466 "The type of the elements in the structure.",
1467 "The type of the monoid."
1468 )]
1469 #[document_parameters("The mapping function.", "The result to fold.")]
1471 #[document_returns("`func(a)` if `fa` is `Err(a)`, otherwise `M::empty()`.")]
1473 #[document_examples]
1475 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1498 func: impl Fn(A) -> M + 'a,
1499 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1500 ) -> M
1501 where
1502 M: Monoid + 'a,
1503 FnBrand: CloneFn + 'a, {
1504 match fa {
1505 Err(e) => func(e),
1506 Ok(_) => M::empty(),
1507 }
1508 }
1509 }
1510
1511 #[document_type_parameters("The success type.")]
1512 impl<T: Clone + 'static> Traversable for ResultOkAppliedBrand<T> {
1513 #[document_signature]
1517 #[document_type_parameters(
1519 "The lifetime of the values.",
1520 "The type of the elements in the traversable structure.",
1521 "The type of the elements in the resulting traversable structure.",
1522 "The applicative context."
1523 )]
1524 #[document_parameters("The function to apply.", "The result to traverse.")]
1526 #[document_returns("The result wrapped in the applicative context.")]
1528 #[document_examples]
1530 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1560 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1561 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1562 ) -> 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>)>)
1563 where
1564 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1565 match ta {
1566 Err(e) => F::map(|b| Err(b), func(e)),
1567 Ok(t) => F::pure(Ok(t)),
1568 }
1569 }
1570
1571 #[document_signature]
1575 #[document_type_parameters(
1577 "The lifetime of the values.",
1578 "The type of the elements in the traversable structure.",
1579 "The applicative context."
1580 )]
1581 #[document_parameters("The result containing the applicative value.")]
1583 #[document_returns("The result wrapped in the applicative context.")]
1585 #[document_examples]
1587 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1605 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>)>)
1606 ) -> 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>)>)
1607 where
1608 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1609 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1610 match ta {
1611 Err(fe) => F::map(|e| Err(e), fe),
1612 Ok(t) => F::pure(Ok(t)),
1613 }
1614 }
1615 }
1616
1617 #[document_type_parameters("The success type.")]
1619 impl<T: Clone + 'static> MonadRec for ResultOkAppliedBrand<T> {
1620 #[document_signature]
1627 #[document_type_parameters(
1629 "The lifetime of the computation.",
1630 "The type of the initial value and loop state.",
1631 "The type of the result."
1632 )]
1633 #[document_parameters("The step function.", "The initial value.")]
1635 #[document_returns(
1637 "The result of the computation, or a success if the step function returned `Ok`."
1638 )]
1639 #[document_examples]
1641 fn tail_rec_m<'a, A: 'a, B: 'a>(
1661 func: impl Fn(
1662 A,
1663 )
1664 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1665 + 'a,
1666 initial: A,
1667 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1668 let mut current = initial;
1669 loop {
1670 match func(current) {
1671 Ok(t) => return Ok(t),
1672 Err(ControlFlow::Continue(next)) => current = next,
1673 Err(ControlFlow::Break(b)) => return Err(b),
1674 }
1675 }
1676 }
1677 }
1678
1679 #[document_type_parameters("The error type.")]
1682 impl<E: Clone + 'static> RefFunctor for ResultErrAppliedBrand<E> {
1683 #[document_signature]
1685 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1686 #[document_parameters("The function.", "The result.")]
1687 #[document_returns("The mapped result.")]
1688 #[document_examples]
1689 fn ref_map<'a, A: 'a, B: 'a>(
1708 func: impl Fn(&A) -> B + 'a,
1709 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1710 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1711 match fa {
1712 Ok(a) => Ok(func(a)),
1713 Err(e) => Err(e.clone()),
1714 }
1715 }
1716 }
1717
1718 #[document_type_parameters("The error type.")]
1719 impl<E: Clone + 'static> RefFoldable for ResultErrAppliedBrand<E> {
1720 #[document_signature]
1722 #[document_type_parameters(
1723 "The lifetime.",
1724 "The brand.",
1725 "The element type.",
1726 "The monoid type."
1727 )]
1728 #[document_parameters("The mapping function.", "The result.")]
1729 #[document_returns("The monoid value.")]
1730 #[document_examples]
1731 fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1744 func: impl Fn(&A) -> M + 'a,
1745 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1746 ) -> M
1747 where
1748 FnBrand: LiftFn + 'a,
1749 M: Monoid + 'a, {
1750 match fa {
1751 Ok(a) => func(a),
1752 Err(_) => Monoid::empty(),
1753 }
1754 }
1755 }
1756
1757 #[document_type_parameters("The error type.")]
1758 impl<E: Clone + 'static> RefTraversable for ResultErrAppliedBrand<E> {
1759 #[document_signature]
1761 #[document_type_parameters(
1762 "The lifetime.",
1763 "The brand.",
1764 "The input type.",
1765 "The output type.",
1766 "The applicative."
1767 )]
1768 #[document_parameters("The function.", "The result.")]
1769 #[document_returns("The traversed result.")]
1770 #[document_examples]
1771 fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1785 func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1786 ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1787 ) -> 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>)>)
1788 where
1789 FnBrand: LiftFn + 'a,
1790 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1791 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1792 match ta {
1793 Ok(a) => F::map(Ok, func(a)),
1794 Err(e) => F::pure(Err(e.clone())),
1795 }
1796 }
1797 }
1798
1799 #[document_type_parameters("The error type.")]
1800 impl<E: 'static> RefPointed for ResultErrAppliedBrand<E> {
1801 #[document_signature]
1803 #[document_type_parameters("The lifetime of the value.", "The type of the value.")]
1804 #[document_parameters("The reference to the value to wrap.")]
1805 #[document_returns("An `Ok` containing a clone of the value.")]
1806 #[document_examples]
1807 fn ref_pure<'a, A: Clone + 'a>(
1819 a: &A
1820 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1821 Ok(a.clone())
1822 }
1823 }
1824
1825 #[document_type_parameters("The error type.")]
1826 impl<E: Clone + 'static> RefLift for ResultErrAppliedBrand<E> {
1827 #[document_signature]
1829 #[document_type_parameters(
1830 "The lifetime.",
1831 "First input type.",
1832 "Second input type.",
1833 "Output type."
1834 )]
1835 #[document_parameters("The binary function.", "The first result.", "The second result.")]
1836 #[document_returns("The combined result, or the first error encountered.")]
1837 #[document_examples]
1838 fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1853 func: impl Fn(&A, &B) -> C + 'a,
1854 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1855 fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1856 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1857 match (fa, fb) {
1858 (Ok(a), Ok(b)) => Ok(func(a, b)),
1859 (Err(e), _) => Err(e.clone()),
1860 (_, Err(e)) => Err(e.clone()),
1861 }
1862 }
1863 }
1864
1865 #[document_type_parameters("The error type.")]
1866 impl<E: Clone + 'static> RefSemiapplicative for ResultErrAppliedBrand<E> {
1867 #[document_signature]
1869 #[document_type_parameters(
1870 "The lifetime.",
1871 "The function brand.",
1872 "The input type.",
1873 "The output type."
1874 )]
1875 #[document_parameters(
1876 "The result containing the by-ref function.",
1877 "The result containing the value."
1878 )]
1879 #[document_returns("The result of applying the function, or the first error.")]
1880 #[document_examples]
1881 fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1894 ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1895 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1896 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1897 match (ff, fa) {
1898 (Ok(f), Ok(a)) => Ok((**f)(a)),
1899 (Err(e), _) => Err(e.clone()),
1900 (_, Err(e)) => Err(e.clone()),
1901 }
1902 }
1903 }
1904
1905 #[document_type_parameters("The error type.")]
1906 impl<E: Clone + 'static> RefSemimonad for ResultErrAppliedBrand<E> {
1907 #[document_signature]
1909 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1910 #[document_parameters("The input result.", "The function to apply by reference.")]
1911 #[document_returns("The result of applying the function, or the original error.")]
1912 #[document_examples]
1913 fn ref_bind<'a, A: 'a, B: 'a>(
1927 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1928 f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1929 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1930 match fa {
1931 Ok(a) => f(a),
1932 Err(e) => Err(e.clone()),
1933 }
1934 }
1935 }
1936
1937 #[document_type_parameters("The success type.")]
1940 impl<T: Clone + 'static> RefFunctor for ResultOkAppliedBrand<T> {
1941 #[document_signature]
1943 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1944 #[document_parameters("The function.", "The result.")]
1945 #[document_returns("The mapped result.")]
1946 #[document_examples]
1947 fn ref_map<'a, A: 'a, B: 'a>(
1963 func: impl Fn(&A) -> B + 'a,
1964 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1965 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1966 match fa {
1967 Err(e) => Err(func(e)),
1968 Ok(t) => Ok(t.clone()),
1969 }
1970 }
1971 }
1972
1973 #[document_type_parameters("The success type.")]
1974 impl<T: Clone + 'static> RefFoldable for ResultOkAppliedBrand<T> {
1975 #[document_signature]
1977 #[document_type_parameters(
1978 "The lifetime.",
1979 "The brand.",
1980 "The element type.",
1981 "The monoid type."
1982 )]
1983 #[document_parameters("The mapping function.", "The result.")]
1984 #[document_returns("The monoid value.")]
1985 #[document_examples]
1986 fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1999 func: impl Fn(&A) -> M + 'a,
2000 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2001 ) -> M
2002 where
2003 FnBrand: LiftFn + 'a,
2004 M: Monoid + 'a, {
2005 match fa {
2006 Err(e) => func(e),
2007 Ok(_) => Monoid::empty(),
2008 }
2009 }
2010 }
2011
2012 #[document_type_parameters("The success type.")]
2013 impl<T: Clone + 'static> RefTraversable for ResultOkAppliedBrand<T> {
2014 #[document_signature]
2016 #[document_type_parameters(
2017 "The lifetime.",
2018 "The brand.",
2019 "The input type.",
2020 "The output type.",
2021 "The applicative."
2022 )]
2023 #[document_parameters("The function.", "The result.")]
2024 #[document_returns("The traversed result.")]
2025 #[document_examples]
2026 fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
2040 func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2041 ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2042 ) -> 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>)>)
2043 where
2044 FnBrand: LiftFn + 'a,
2045 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
2046 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
2047 match ta {
2048 Err(e) => F::map(Err, func(e)),
2049 Ok(t) => F::pure(Ok(t.clone())),
2050 }
2051 }
2052 }
2053
2054 #[document_type_parameters("The success type.")]
2055 impl<T: 'static> RefPointed for ResultOkAppliedBrand<T> {
2056 #[document_signature]
2058 #[document_type_parameters("The lifetime of the value.", "The type of the value.")]
2059 #[document_parameters("The reference to the value to wrap.")]
2060 #[document_returns("An `Err` containing a clone of the value.")]
2061 #[document_examples]
2062 fn ref_pure<'a, A: Clone + 'a>(
2074 a: &A
2075 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
2076 Err(a.clone())
2077 }
2078 }
2079
2080 #[document_type_parameters("The success type.")]
2081 impl<T: Clone + 'static> RefLift for ResultOkAppliedBrand<T> {
2082 #[document_signature]
2084 #[document_type_parameters(
2085 "The lifetime.",
2086 "First input type.",
2087 "Second input type.",
2088 "Output type."
2089 )]
2090 #[document_parameters("The binary function.", "The first result.", "The second result.")]
2091 #[document_returns("The combined result, or the first success encountered.")]
2092 #[document_examples]
2093 fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
2108 func: impl Fn(&A, &B) -> C + 'a,
2109 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2110 fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
2111 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
2112 match (fa, fb) {
2113 (Err(a), Err(b)) => Err(func(a, b)),
2114 (Ok(t), _) => Ok(t.clone()),
2115 (_, Ok(t)) => Ok(t.clone()),
2116 }
2117 }
2118 }
2119
2120 #[document_type_parameters("The success type.")]
2121 impl<T: Clone + 'static> RefSemiapplicative for ResultOkAppliedBrand<T> {
2122 #[document_signature]
2124 #[document_type_parameters(
2125 "The lifetime.",
2126 "The function brand.",
2127 "The input type.",
2128 "The output type."
2129 )]
2130 #[document_parameters(
2131 "The result containing the by-ref function (in Err).",
2132 "The result containing the value (in Err)."
2133 )]
2134 #[document_returns("The result of applying the function, or the first success.")]
2135 #[document_examples]
2136 fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
2149 ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
2150 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2151 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2152 match (ff, fa) {
2153 (Err(f), Err(a)) => Err((**f)(a)),
2154 (Ok(t), _) => Ok(t.clone()),
2155 (_, Ok(t)) => Ok(t.clone()),
2156 }
2157 }
2158 }
2159
2160 #[document_type_parameters("The success type.")]
2161 impl<T: Clone + 'static> RefSemimonad for ResultOkAppliedBrand<T> {
2162 #[document_signature]
2164 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
2165 #[document_parameters("The input result.", "The function to apply by reference.")]
2166 #[document_returns("The result of applying the function, or the original success.")]
2167 #[document_examples]
2168 fn ref_bind<'a, A: 'a, B: 'a>(
2182 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
2183 f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
2184 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2185 match fa {
2186 Err(e) => f(e),
2187 Ok(t) => Ok(t.clone()),
2188 }
2189 }
2190 }
2191
2192 #[document_type_parameters("The error type.")]
2194 impl<E: Clone + 'static> MonadRec for ResultErrAppliedBrand<E> {
2195 #[document_signature]
2202 #[document_type_parameters(
2204 "The lifetime of the computation.",
2205 "The type of the initial value and loop state.",
2206 "The type of the result."
2207 )]
2208 #[document_parameters("The step function.", "The initial value.")]
2210 #[document_returns(
2212 "The result of the computation, or an error if the step function returned `Err`."
2213 )]
2214 #[document_examples]
2216 fn tail_rec_m<'a, A: 'a, B: 'a>(
2236 func: impl Fn(
2237 A,
2238 )
2239 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
2240 + 'a,
2241 initial: A,
2242 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
2243 let mut current = initial;
2244 loop {
2245 match func(current) {
2246 Err(e) => return Err(e),
2247 Ok(ControlFlow::Continue(next)) => current = next,
2248 Ok(ControlFlow::Break(b)) => return Ok(b),
2249 }
2250 }
2251 }
2252 }
2253}
2254
2255#[cfg(test)]
2256mod tests {
2257
2258 use {
2259 crate::{
2260 brands::*,
2261 classes::{
2262 semiapplicative::apply as explicit_apply,
2263 *,
2264 },
2265 functions::*,
2266 },
2267 quickcheck_macros::quickcheck,
2268 };
2269
2270 #[test]
2274 fn test_bimap() {
2275 let x: Result<i32, i32> = Ok(5);
2276 assert_eq!(
2277 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), x),
2278 Ok(10)
2279 );
2280
2281 let y: Result<i32, i32> = Err(5);
2282 assert_eq!(
2283 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|e| e + 1, |s| s * 2), y),
2284 Err(6)
2285 );
2286 }
2287
2288 #[quickcheck]
2292 fn bifunctor_identity(x: Result<i32, i32>) -> bool {
2293 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((identity, identity), x) == x
2294 }
2295
2296 #[quickcheck]
2298 fn bifunctor_composition(x: Result<i32, i32>) -> bool {
2299 let f = |x: i32| x.wrapping_add(1);
2300 let g = |x: i32| x.wrapping_mul(2);
2301 let h = |x: i32| x.wrapping_sub(1);
2302 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2303
2304 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2305 == explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2306 (f, h),
2307 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((g, i), x),
2308 )
2309 }
2310
2311 #[quickcheck]
2315 fn functor_identity(x: Result<i32, i32>) -> bool {
2316 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(identity, x) == x
2317 }
2318
2319 #[quickcheck]
2321 fn functor_composition(x: Result<i32, i32>) -> bool {
2322 let f = |x: i32| x.wrapping_add(1);
2323 let g = |x: i32| x.wrapping_mul(2);
2324 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(compose(f, g), x)
2325 == explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2326 f,
2327 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(g, x),
2328 )
2329 }
2330
2331 #[quickcheck]
2335 fn applicative_identity(v: Result<i32, i32>) -> bool {
2336 explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2337 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(identity)),
2338 v,
2339 ) == v
2340 }
2341
2342 #[quickcheck]
2344 fn applicative_homomorphism(x: i32) -> bool {
2345 let f = |x: i32| x.wrapping_mul(2);
2346 explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2347 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f)),
2348 pure::<ResultErrAppliedBrand<i32>, _>(x),
2349 ) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
2350 }
2351
2352 #[quickcheck]
2354 fn applicative_composition(
2355 w: Result<i32, i32>,
2356 u_is_ok: bool,
2357 v_is_ok: bool,
2358 ) -> bool {
2359 let v_fn = |x: i32| x.wrapping_mul(2);
2360 let u_fn = |x: i32| x.wrapping_add(1);
2361
2362 let v = if v_is_ok {
2363 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(v_fn))
2364 } else {
2365 Err(100)
2366 };
2367 let u = if u_is_ok {
2368 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(u_fn))
2369 } else {
2370 Err(200)
2371 };
2372
2373 let vw = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
2375 let rhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
2376
2377 let uv = match (u, v) {
2380 (Ok(uf), Ok(vf)) => {
2381 let composed = move |x| uf(vf(x));
2382 Ok(<RcFnBrand as LiftFn>::new(composed))
2383 }
2384 (Err(e), _) => Err(e),
2385 (_, Err(e)) => Err(e),
2386 };
2387
2388 let lhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
2389
2390 lhs == rhs
2391 }
2392
2393 #[quickcheck]
2395 fn applicative_interchange(y: i32) -> bool {
2396 let f = |x: i32| x.wrapping_mul(2);
2398 let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as LiftFn>::new(f));
2399
2400 let lhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2401 u.clone(),
2402 pure::<ResultErrAppliedBrand<i32>, _>(y),
2403 );
2404
2405 let rhs_fn = <RcFnBrand as LiftFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
2406 let rhs = explicit_apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
2407 pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
2408 u,
2409 );
2410
2411 lhs == rhs
2412 }
2413
2414 #[quickcheck]
2418 fn monad_left_identity(a: i32) -> bool {
2419 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2420 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2421 pure::<ResultErrAppliedBrand<i32>, _>(a),
2422 f,
2423 ) == f(a)
2424 }
2425
2426 #[quickcheck]
2428 fn monad_right_identity(m: Result<i32, i32>) -> bool {
2429 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2430 m,
2431 pure::<ResultErrAppliedBrand<i32>, _>,
2432 ) == m
2433 }
2434
2435 #[quickcheck]
2437 fn monad_associativity(m: Result<i32, i32>) -> bool {
2438 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
2439 let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
2440 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2441 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, f),
2442 g,
2443 ) == explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(m, |x| {
2444 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(f(x), g)
2445 })
2446 }
2447
2448 #[test]
2452 fn map_err() {
2453 assert_eq!(
2454 explicit::map::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2455 |x: i32| x + 1,
2456 Err::<i32, i32>(1)
2457 ),
2458 Err(1)
2459 );
2460 }
2461
2462 #[test]
2464 fn bind_err() {
2465 assert_eq!(
2466 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(
2467 Err::<i32, i32>(1),
2468 |x: i32| Ok(x + 1)
2469 ),
2470 Err(1)
2471 );
2472 }
2473
2474 #[test]
2476 fn bind_returning_err() {
2477 assert_eq!(
2478 explicit::bind::<ResultErrAppliedBrand<i32>, _, _, _, _>(Ok(1), |_| Err::<i32, i32>(2)),
2479 Err(2)
2480 );
2481 }
2482
2483 #[test]
2485 fn fold_right_err() {
2486 assert_eq!(
2487 crate::functions::explicit::fold_right::<
2488 RcFnBrand,
2489 ResultErrAppliedBrand<i32>,
2490 _,
2491 _,
2492 _,
2493 _,
2494 >(|x: i32, acc| x + acc, 0, Err(1)),
2495 0
2496 );
2497 }
2498
2499 #[test]
2501 fn fold_left_err() {
2502 assert_eq!(
2503 crate::functions::explicit::fold_left::<
2504 RcFnBrand,
2505 ResultErrAppliedBrand<i32>,
2506 _,
2507 _,
2508 _,
2509 _,
2510 >(|acc, x: i32| acc + x, 0, Err(1)),
2511 0
2512 );
2513 }
2514
2515 #[test]
2517 fn traverse_err() {
2518 assert_eq!(
2519 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2520 |x: i32| Some(x + 1),
2521 Err(1)
2522 ),
2523 Some(Err(1))
2524 );
2525 }
2526
2527 #[test]
2529 fn traverse_returning_err() {
2530 assert_eq!(
2531 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
2532 |_: i32| None::<i32>,
2533 Ok(1)
2534 ),
2535 None
2536 );
2537 }
2538
2539 #[quickcheck]
2543 fn monad_rec_identity(x: i32) -> bool {
2544 use {
2545 crate::classes::monad_rec::tail_rec_m,
2546 core::ops::ControlFlow,
2547 };
2548 tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(|a| Ok(ControlFlow::Break(a)), x) == Ok(x)
2549 }
2550
2551 #[test]
2553 fn monad_rec_sum_range() {
2554 use {
2555 crate::classes::monad_rec::tail_rec_m,
2556 core::ops::ControlFlow,
2557 };
2558 let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2559 |(n, acc)| {
2560 if n == 0 {
2561 Ok(ControlFlow::Break(acc))
2562 } else {
2563 Ok(ControlFlow::Continue((n - 1, acc + n)))
2564 }
2565 },
2566 (100i64, 0i64),
2567 );
2568 assert_eq!(result, Ok(5050));
2569 }
2570
2571 #[test]
2573 fn monad_rec_short_circuit() {
2574 use {
2575 crate::classes::monad_rec::tail_rec_m,
2576 core::ops::ControlFlow,
2577 };
2578 let result: Result<i32, &str> = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
2579 |n| {
2580 if n == 5 { Err("stopped") } else { Ok(ControlFlow::Continue(n + 1)) }
2581 },
2582 0,
2583 );
2584 assert_eq!(result, Err("stopped"));
2585 }
2586
2587 #[test]
2589 fn monad_rec_stack_safety() {
2590 use {
2591 crate::classes::monad_rec::tail_rec_m,
2592 core::ops::ControlFlow,
2593 };
2594 let iterations: i64 = 200_000;
2595 let result = tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(
2596 |acc| {
2597 if acc < iterations {
2598 Ok(ControlFlow::Continue(acc + 1))
2599 } else {
2600 Ok(ControlFlow::Break(acc))
2601 }
2602 },
2603 0i64,
2604 );
2605 assert_eq!(result, Ok(iterations));
2606 }
2607
2608 #[quickcheck]
2613 fn monad_rec_ok_applied_identity(x: i32) -> bool {
2614 use {
2615 crate::classes::monad_rec::tail_rec_m,
2616 core::ops::ControlFlow,
2617 };
2618 tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(|a| Err(ControlFlow::Break(a)), x) == Err(x)
2619 }
2620
2621 #[test]
2623 fn monad_rec_ok_applied_sum_range() {
2624 use {
2625 crate::classes::monad_rec::tail_rec_m,
2626 core::ops::ControlFlow,
2627 };
2628 let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2629 |(n, acc)| {
2630 if n == 0 {
2631 Err(ControlFlow::Break(acc))
2632 } else {
2633 Err(ControlFlow::Continue((n - 1, acc + n)))
2634 }
2635 },
2636 (100i64, 0i64),
2637 );
2638 assert_eq!(result, Err(5050));
2639 }
2640
2641 #[test]
2643 fn monad_rec_ok_applied_short_circuit() {
2644 use {
2645 crate::classes::monad_rec::tail_rec_m,
2646 core::ops::ControlFlow,
2647 };
2648 let result: Result<&str, i32> = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
2649 |n| {
2650 if n == 5 { Ok("stopped") } else { Err(ControlFlow::Continue(n + 1)) }
2651 },
2652 0,
2653 );
2654 assert_eq!(result, Ok("stopped"));
2655 }
2656
2657 #[test]
2659 fn monad_rec_ok_applied_stack_safety() {
2660 use {
2661 crate::classes::monad_rec::tail_rec_m,
2662 core::ops::ControlFlow,
2663 };
2664 let iterations: i64 = 200_000;
2665 let result = tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(
2666 |acc| {
2667 if acc < iterations {
2668 Err(ControlFlow::Continue(acc + 1))
2669 } else {
2670 Err(ControlFlow::Break(acc))
2671 }
2672 },
2673 0i64,
2674 );
2675 assert_eq!(result, Err(iterations));
2676 }
2677
2678 #[quickcheck]
2682 fn ref_bifunctor_identity(x: Result<i32, i32>) -> bool {
2683 explicit::bimap::<ResultBrand, _, _, _, _, _, _>((|a: &i32| *a, |c: &i32| *c), &x) == x
2684 }
2685
2686 #[quickcheck]
2688 fn ref_bifunctor_composition(x: Result<i32, i32>) -> bool {
2689 let f1 = |a: &i32| a.wrapping_add(1);
2690 let f2 = |a: &i32| a.wrapping_mul(2);
2691 let g1 = |c: &i32| c.wrapping_add(10);
2692 let g2 = |c: &i32| c.wrapping_mul(3);
2693 explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2694 (|a: &i32| f2(&f1(a)), |c: &i32| g2(&g1(c))),
2695 &x,
2696 ) == explicit::bimap::<ResultBrand, _, _, _, _, _, _>(
2697 (f2, g2),
2698 &explicit::bimap::<ResultBrand, _, _, _, _, _, _>((f1, g1), &x),
2699 )
2700 }
2701
2702 #[quickcheck]
2706 fn ref_bifoldable_fold_map(x: Result<i32, i32>) -> bool {
2707 let result = explicit::bi_fold_map::<RcFnBrand, ResultBrand, _, _, _, _, _>(
2708 (|a: &i32| a.to_string(), |b: &i32| b.to_string()),
2709 &x,
2710 );
2711 let expected = match &x {
2712 Err(a) => a.to_string(),
2713 Ok(b) => b.to_string(),
2714 };
2715 result == expected
2716 }
2717
2718 #[quickcheck]
2722 fn ref_bitraversable_consistency(x: Result<i32, i32>) -> bool {
2723 let f = |a: &i32| Some(a.wrapping_add(1));
2724 let g = |c: &i32| Some(c.wrapping_mul(2));
2725 let traversed =
2726 explicit::bi_traverse::<RcFnBrand, ResultBrand, _, _, _, _, OptionBrand, _, _>(
2727 (f, g),
2728 &x,
2729 );
2730 let mapped_then_sequenced =
2731 ref_bi_sequence::<ResultBrand, RcFnBrand, _, _, OptionBrand>(&explicit::bimap::<
2732 ResultBrand,
2733 _,
2734 _,
2735 _,
2736 _,
2737 _,
2738 _,
2739 >((f, g), &x));
2740 traversed == mapped_then_sequenced
2741 }
2742}