1#[fp_macros::document_module]
6mod inner {
7 use crate::{
8 Apply,
9 brands::{ResultBrand, ResultWithErrBrand, ResultWithOkBrand},
10 classes::{
11 Applicative, ApplyFirst, ApplySecond, Bifunctor, CloneableFn, Foldable, Functor, Lift,
12 Monoid, ParFoldable, Pointed, Semiapplicative, Semimonad, SendCloneableFn, Traversable,
13 },
14 impl_kind,
15 kinds::*,
16 };
17 use fp_macros::{document_parameters, document_type_parameters};
18
19 impl_kind! {
20 for ResultBrand {
27 type Of<A, B> = Result<B, A>;
28 }
29 }
30
31 impl_kind! {
32 for ResultBrand {
36 type Of<'a, A: 'a, B: 'a>: 'a = Result<B, A>;
37 }
38 }
39
40 impl Bifunctor for ResultBrand {
41 #[document_signature]
48 #[document_type_parameters(
52 "The lifetime of the values.",
53 "The type of the error value.",
54 "The type of the mapped error value.",
55 "The type of the success value.",
56 "The type of the mapped success value.",
57 "The type of the function to apply to the error.",
58 "The type of the function to apply to the success."
59 )]
60 #[document_parameters(
64 "The function to apply to the error.",
65 "The function to apply to the success.",
66 "The result to map over."
67 )]
68 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a, F, G>(
85 f: F,
86 g: G,
87 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
88 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>)
89 where
90 F: Fn(A) -> B + 'a,
91 G: Fn(C) -> D + 'a,
92 {
93 match p {
94 Ok(c) => Ok(g(c)),
95 Err(a) => Err(f(a)),
96 }
97 }
98 }
99
100 impl_kind! {
103 #[document_type_parameters("The error type.")]
106 impl<E: 'static> for ResultWithErrBrand<E> {
107 type Of<'a, A: 'a>: 'a = Result<A, E>;
108 }
109 }
110
111 #[document_type_parameters("The error type.")]
114 impl<E: 'static> Functor for ResultWithErrBrand<E> {
115 #[document_signature]
122 #[document_type_parameters(
126 "The lifetime of the values.",
127 "The type of the value inside the result.",
128 "The type of the result of applying the function.",
129 "The type of the function to apply."
130 )]
131 #[document_parameters("The function to apply.", "The result to map over.")]
135 fn map<'a, A: 'a, B: 'a, Func>(
149 func: Func,
150 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
151 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
152 where
153 Func: Fn(A) -> B + 'a,
154 {
155 fa.map(func)
156 }
157 }
158
159 #[document_type_parameters("The error type.")]
162 impl<E: Clone + 'static> Lift for ResultWithErrBrand<E> {
163 #[document_signature]
170 #[document_type_parameters(
174 "The lifetime of the values.",
175 "The type of the first value.",
176 "The type of the second value.",
177 "The type of the result.",
178 "The type of the binary function."
179 )]
180 #[document_parameters(
184 "The binary function to apply.",
185 "The first result.",
186 "The second result."
187 )]
188 fn lift2<'a, A, B, C, Func>(
216 func: Func,
217 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
218 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
219 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
220 where
221 Func: Fn(A, B) -> C + 'a,
222 A: Clone + 'a,
223 B: Clone + 'a,
224 C: 'a,
225 {
226 match (fa, fb) {
227 (Ok(a), Ok(b)) => Ok(func(a, b)),
228 (Err(e), _) => Err(e),
229 (_, Err(e)) => Err(e),
230 }
231 }
232 }
233
234 #[document_type_parameters("The error type.")]
237 impl<E: 'static> Pointed for ResultWithErrBrand<E> {
238 #[document_signature]
245 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
249 #[document_parameters("The value to wrap.")]
253 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
267 Ok(a)
268 }
269 }
270
271 #[document_type_parameters("The error type.")]
274 impl<E: Clone + 'static> ApplyFirst for ResultWithErrBrand<E> {}
275
276 #[document_type_parameters("The error type.")]
279 impl<E: Clone + 'static> ApplySecond for ResultWithErrBrand<E> {}
280
281 #[document_type_parameters("The error type.")]
284 impl<E: Clone + 'static> Semiapplicative for ResultWithErrBrand<E> {
285 #[document_signature]
292 #[document_type_parameters(
296 "The lifetime of the values.",
297 "The brand of the cloneable function wrapper.",
298 "The type of the input value.",
299 "The type of the output value."
300 )]
301 #[document_parameters(
305 "The result containing the function.",
306 "The result containing the value."
307 )]
308 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
327 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
328 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
329 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
330 match (ff, fa) {
331 (Ok(f), Ok(a)) => Ok(f(a)),
332 (Err(e), _) => Err(e),
333 (_, Err(e)) => Err(e),
334 }
335 }
336 }
337
338 #[document_type_parameters("The error type.")]
341 impl<E: Clone + 'static> Semimonad for ResultWithErrBrand<E> {
342 #[document_signature]
349 #[document_type_parameters(
353 "The lifetime of the values.",
354 "The type of the result of the first computation.",
355 "The type of the result of the second computation.",
356 "The type of the function to apply."
357 )]
358 #[document_parameters(
362 "The first result.",
363 "The function to apply to the value inside the result."
364 )]
365 fn bind<'a, A: 'a, B: 'a, Func>(
390 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
391 func: Func,
392 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
393 where
394 Func: Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
395 {
396 ma.and_then(func)
397 }
398 }
399
400 #[document_type_parameters("The error type.")]
403 impl<E: 'static> Foldable for ResultWithErrBrand<E> {
404 #[document_signature]
411 #[document_type_parameters(
415 "The lifetime of the values.",
416 "The brand of the cloneable function to use.",
417 "The type of the elements in the structure.",
418 "The type of the accumulator.",
419 "The type of the folding function."
420 )]
421 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
425 fn fold_right<'a, FnBrand, A: 'a, B: 'a, F>(
439 func: F,
440 initial: B,
441 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
442 ) -> B
443 where
444 F: Fn(A, B) -> B + 'a,
445 FnBrand: CloneableFn + 'a,
446 {
447 match fa {
448 Ok(a) => func(a, initial),
449 Err(_) => initial,
450 }
451 }
452
453 #[document_signature]
460 #[document_type_parameters(
464 "The lifetime of the values.",
465 "The brand of the cloneable function to use.",
466 "The type of the elements in the structure.",
467 "The type of the accumulator.",
468 "The type of the folding function."
469 )]
470 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
474 fn fold_left<'a, FnBrand, A: 'a, B: 'a, F>(
488 func: F,
489 initial: B,
490 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
491 ) -> B
492 where
493 F: Fn(B, A) -> B + 'a,
494 FnBrand: CloneableFn + 'a,
495 {
496 match fa {
497 Ok(a) => func(initial, a),
498 Err(_) => initial,
499 }
500 }
501
502 #[document_signature]
509 #[document_type_parameters(
513 "The lifetime of the values.",
514 "The brand of the cloneable function to use.",
515 "The type of the elements in the structure.",
516 "The type of the monoid.",
517 "The type of the mapping function."
518 )]
519 #[document_parameters("The mapping function.", "The result to fold.")]
523 fn fold_map<'a, FnBrand, A: 'a, M, F>(
543 func: F,
544 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
545 ) -> M
546 where
547 M: Monoid + 'a,
548 F: Fn(A) -> M + 'a,
549 FnBrand: CloneableFn + 'a,
550 {
551 match fa {
552 Ok(a) => func(a),
553 Err(_) => M::empty(),
554 }
555 }
556 }
557
558 #[document_type_parameters("The error type.")]
561 impl<E: Clone + 'static> Traversable for ResultWithErrBrand<E> {
562 #[document_signature]
569 #[document_type_parameters(
573 "The lifetime of the values.",
574 "The type of the elements in the traversable structure.",
575 "The type of the elements in the resulting traversable structure.",
576 "The applicative context.",
577 "The type of the function to apply."
578 )]
579 #[document_parameters("The function to apply.", "The result to traverse.")]
583 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
608 func: Func,
609 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
610 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
611 where
612 Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
613 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
614 {
615 match ta {
616 Ok(a) => F::map(|b| Ok(b), func(a)),
617 Err(e) => F::pure(Err(e)),
618 }
619 }
620
621 #[document_signature]
628 #[document_type_parameters(
632 "The lifetime of the values.",
633 "The type of the elements in the traversable structure.",
634 "The applicative context."
635 )]
636 #[document_parameters("The result containing the applicative value.")]
640 fn sequence<'a, A: 'a + Clone, F: Applicative>(
665 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
666 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
667 where
668 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
669 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
670 {
671 match ta {
672 Ok(fa) => F::map(|a| Ok(a), fa),
673 Err(e) => F::pure(Err(e)),
674 }
675 }
676 }
677
678 impl_kind! {
681 #[document_type_parameters("The success type.")]
684 impl<T: 'static> for ResultWithOkBrand<T> {
685 type Of<'a, A: 'a>: 'a = Result<T, A>;
686 }
687 }
688
689 #[document_type_parameters("The success type.")]
692 impl<T: 'static> Functor for ResultWithOkBrand<T> {
693 #[document_signature]
700 #[document_type_parameters(
704 "The lifetime of the values.",
705 "The type of the error value inside the result.",
706 "The type of the result of applying the function.",
707 "The type of the function to apply."
708 )]
709 #[document_parameters("The function to apply to the error.", "The result to map over.")]
713 fn map<'a, A: 'a, B: 'a, Func>(
727 func: Func,
728 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
729 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
730 where
731 Func: Fn(A) -> B + 'a,
732 {
733 match fa {
734 Ok(t) => Ok(t),
735 Err(e) => Err(func(e)),
736 }
737 }
738 }
739
740 #[document_type_parameters("The success type.")]
743 impl<T: Clone + 'static> Lift for ResultWithOkBrand<T> {
744 #[document_signature]
751 #[document_type_parameters(
755 "The lifetime of the values.",
756 "The type of the first error value.",
757 "The type of the second error value.",
758 "The type of the result error value.",
759 "The type of the binary function."
760 )]
761 #[document_parameters(
765 "The binary function to apply to the errors.",
766 "The first result.",
767 "The second result."
768 )]
769 fn lift2<'a, A, B, C, Func>(
797 func: Func,
798 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
799 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
800 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
801 where
802 Func: Fn(A, B) -> C + 'a,
803 A: Clone + 'a,
804 B: Clone + 'a,
805 C: 'a,
806 {
807 match (fa, fb) {
808 (Err(a), Err(b)) => Err(func(a, b)),
809 (Ok(t), _) => Ok(t),
810 (_, Ok(t)) => Ok(t),
811 }
812 }
813 }
814
815 #[document_type_parameters("The success type.")]
818 impl<T: 'static> Pointed for ResultWithOkBrand<T> {
819 #[document_signature]
826 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
830 #[document_parameters("The value to wrap.")]
834 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
848 Err(a)
849 }
850 }
851
852 #[document_type_parameters("The success type.")]
855 impl<T: Clone + 'static> ApplyFirst for ResultWithOkBrand<T> {}
856
857 #[document_type_parameters("The success type.")]
860 impl<T: Clone + 'static> ApplySecond for ResultWithOkBrand<T> {}
861
862 #[document_type_parameters("The success type.")]
865 impl<T: Clone + 'static> Semiapplicative for ResultWithOkBrand<T> {
866 #[document_signature]
873 #[document_type_parameters(
877 "The lifetime of the values.",
878 "The brand of the cloneable function wrapper.",
879 "The type of the input value.",
880 "The type of the output value."
881 )]
882 #[document_parameters(
886 "The result containing the function (in Err).",
887 "The result containing the value (in Err)."
888 )]
889 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
908 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
909 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
910 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
911 match (ff, fa) {
912 (Err(f), Err(a)) => Err(f(a)),
913 (Ok(t), _) => Ok(t),
914 (_, Ok(t)) => Ok(t),
915 }
916 }
917 }
918
919 #[document_type_parameters("The success type.")]
922 impl<T: Clone + 'static> Semimonad for ResultWithOkBrand<T> {
923 #[document_signature]
930 #[document_type_parameters(
934 "The lifetime of the values.",
935 "The type of the result of the first computation.",
936 "The type of the result of the second computation.",
937 "The type of the function to apply."
938 )]
939 #[document_parameters("The first result.", "The function to apply to the error value.")]
943 fn bind<'a, A: 'a, B: 'a, Func>(
968 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
969 func: Func,
970 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
971 where
972 Func: Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
973 {
974 match ma {
975 Ok(t) => Ok(t),
976 Err(e) => func(e),
977 }
978 }
979 }
980
981 #[document_type_parameters("The success type.")]
984 impl<T: 'static> Foldable for ResultWithOkBrand<T> {
985 #[document_signature]
992 #[document_type_parameters(
996 "The lifetime of the values.",
997 "The brand of the cloneable function to use.",
998 "The type of the elements in the structure.",
999 "The type of the accumulator.",
1000 "The type of the folding function."
1001 )]
1002 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1006 fn fold_right<'a, FnBrand, A: 'a, B: 'a, F>(
1020 func: F,
1021 initial: B,
1022 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1023 ) -> B
1024 where
1025 F: Fn(A, B) -> B + 'a,
1026 FnBrand: CloneableFn + 'a,
1027 {
1028 match fa {
1029 Err(e) => func(e, initial),
1030 Ok(_) => initial,
1031 }
1032 }
1033
1034 #[document_signature]
1041 #[document_type_parameters(
1045 "The lifetime of the values.",
1046 "The brand of the cloneable function to use.",
1047 "The type of the elements in the structure.",
1048 "The type of the accumulator.",
1049 "The type of the folding function."
1050 )]
1051 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1055 fn fold_left<'a, FnBrand, A: 'a, B: 'a, F>(
1069 func: F,
1070 initial: B,
1071 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1072 ) -> B
1073 where
1074 F: Fn(B, A) -> B + 'a,
1075 FnBrand: CloneableFn + 'a,
1076 {
1077 match fa {
1078 Err(e) => func(initial, e),
1079 Ok(_) => initial,
1080 }
1081 }
1082
1083 #[document_signature]
1090 #[document_type_parameters(
1094 "The lifetime of the values.",
1095 "The brand of the cloneable function to use.",
1096 "The type of the elements in the structure.",
1097 "The type of the monoid.",
1098 "The type of the mapping function."
1099 )]
1100 #[document_parameters("The mapping function.", "The result to fold.")]
1104 fn fold_map<'a, FnBrand, A: 'a, M, Func>(
1124 func: Func,
1125 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1126 ) -> M
1127 where
1128 M: Monoid + 'a,
1129 Func: Fn(A) -> M + 'a,
1130 FnBrand: CloneableFn + 'a,
1131 {
1132 match fa {
1133 Err(e) => func(e),
1134 Ok(_) => M::empty(),
1135 }
1136 }
1137 }
1138
1139 #[document_type_parameters("The success type.")]
1142 impl<T: Clone + 'static> Traversable for ResultWithOkBrand<T> {
1143 #[document_signature]
1150 #[document_type_parameters(
1154 "The lifetime of the values.",
1155 "The type of the elements in the traversable structure.",
1156 "The type of the elements in the resulting traversable structure.",
1157 "The applicative context.",
1158 "The type of the function to apply."
1159 )]
1160 #[document_parameters("The function to apply.", "The result to traverse.")]
1164 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
1189 func: Func,
1190 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1191 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
1192 where
1193 Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1194 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1195 {
1196 match ta {
1197 Err(e) => F::map(|b| Err(b), func(e)),
1198 Ok(t) => F::pure(Ok(t)),
1199 }
1200 }
1201
1202 #[document_signature]
1209 #[document_type_parameters(
1213 "The lifetime of the values.",
1214 "The type of the elements in the traversable structure.",
1215 "The applicative context."
1216 )]
1217 #[document_parameters("The result containing the applicative value.")]
1221 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1246 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1247 ) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
1248 where
1249 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1250 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1251 {
1252 match ta {
1253 Err(fe) => F::map(|e| Err(e), fe),
1254 Ok(t) => F::pure(Ok(t)),
1255 }
1256 }
1257 }
1258
1259 #[document_type_parameters("The error type.")]
1262 impl<E: 'static> ParFoldable for ResultWithErrBrand<E> {
1263 #[document_signature]
1270 #[document_type_parameters(
1274 "The lifetime of the values.",
1275 "The brand of the cloneable function wrapper.",
1276 "The element type.",
1277 "The monoid type."
1278 )]
1279 #[document_parameters(
1283 "The thread-safe function to map each element to a monoid.",
1284 "The result to fold."
1285 )]
1286 fn par_fold_map<'a, FnBrand, A, M>(
1304 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1305 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1306 ) -> M
1307 where
1308 FnBrand: 'a + SendCloneableFn,
1309 A: 'a + Clone + Send + Sync,
1310 M: Monoid + Send + Sync + 'a,
1311 {
1312 match fa {
1313 Ok(a) => func(a),
1314 Err(_) => M::empty(),
1315 }
1316 }
1317
1318 #[document_signature]
1325 #[document_type_parameters(
1329 "The lifetime of the values.",
1330 "The brand of the cloneable function wrapper.",
1331 "The element type.",
1332 "The accumulator type."
1333 )]
1334 #[document_parameters(
1338 "The thread-safe function to apply to each element and the accumulator.",
1339 "The initial value.",
1340 "The result to fold."
1341 )]
1342 fn par_fold_right<'a, FnBrand, A, B>(
1360 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1361 initial: B,
1362 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1363 ) -> B
1364 where
1365 FnBrand: 'a + SendCloneableFn,
1366 A: 'a + Clone + Send + Sync,
1367 B: Send + Sync + 'a,
1368 {
1369 match fa {
1370 Ok(a) => func((a, initial)),
1371 Err(_) => initial,
1372 }
1373 }
1374 }
1375
1376 #[document_type_parameters("The success type.")]
1379 impl<T: 'static> ParFoldable for ResultWithOkBrand<T> {
1380 #[document_signature]
1387 #[document_type_parameters(
1391 "The lifetime of the values.",
1392 "The brand of the cloneable function wrapper.",
1393 "The element type.",
1394 "The monoid type."
1395 )]
1396 #[document_parameters(
1400 "The thread-safe function to map each element to a monoid.",
1401 "The result to fold."
1402 )]
1403 fn par_fold_map<'a, FnBrand, A, M>(
1421 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1422 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1423 ) -> M
1424 where
1425 FnBrand: 'a + SendCloneableFn,
1426 A: 'a + Clone + Send + Sync,
1427 M: Monoid + Send + Sync + 'a,
1428 {
1429 match fa {
1430 Err(e) => func(e),
1431 Ok(_) => M::empty(),
1432 }
1433 }
1434
1435 #[document_signature]
1442 #[document_type_parameters(
1446 "The lifetime of the values.",
1447 "The brand of the cloneable function wrapper.",
1448 "The element type.",
1449 "The accumulator type."
1450 )]
1451 #[document_parameters(
1455 "The thread-safe function to apply to each element and the accumulator.",
1456 "The initial value.",
1457 "The result to fold."
1458 )]
1459 fn par_fold_right<'a, FnBrand, A, B>(
1477 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1478 initial: B,
1479 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1480 ) -> B
1481 where
1482 FnBrand: 'a + SendCloneableFn,
1483 A: 'a + Clone + Send + Sync,
1484 B: Send + Sync + 'a,
1485 {
1486 match fa {
1487 Err(e) => func((e, initial)),
1488 Ok(_) => initial,
1489 }
1490 }
1491 }
1492}
1493
1494#[cfg(test)]
1495mod tests {
1496
1497 use crate::{
1498 brands::*,
1499 classes::{CloneableFn, bifunctor::*},
1500 functions::*,
1501 };
1502 use quickcheck_macros::quickcheck;
1503
1504 #[test]
1508 fn test_bimap() {
1509 let x: Result<i32, i32> = Ok(5);
1510 assert_eq!(bimap::<ResultBrand, _, _, _, _, _, _>(|e| e + 1, |s| s * 2, x), Ok(10));
1511
1512 let y: Result<i32, i32> = Err(5);
1513 assert_eq!(bimap::<ResultBrand, _, _, _, _, _, _>(|e| e + 1, |s| s * 2, y), Err(6));
1514 }
1515
1516 #[quickcheck]
1520 fn bifunctor_identity(x: Result<i32, i32>) -> bool {
1521 bimap::<ResultBrand, _, _, _, _, _, _>(identity, identity, x) == x
1522 }
1523
1524 #[quickcheck]
1526 fn bifunctor_composition(x: Result<i32, i32>) -> bool {
1527 let f = |x: i32| x.wrapping_add(1);
1528 let g = |x: i32| x.wrapping_mul(2);
1529 let h = |x: i32| x.wrapping_sub(1);
1530 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1531
1532 bimap::<ResultBrand, _, _, _, _, _, _>(compose(f, g), compose(h, i), x)
1533 == bimap::<ResultBrand, _, _, _, _, _, _>(
1534 f,
1535 h,
1536 bimap::<ResultBrand, _, _, _, _, _, _>(g, i, x),
1537 )
1538 }
1539
1540 #[quickcheck]
1544 fn functor_identity(x: Result<i32, i32>) -> bool {
1545 map::<ResultWithErrBrand<i32>, _, _, _>(identity, x) == x
1546 }
1547
1548 #[quickcheck]
1550 fn functor_composition(x: Result<i32, i32>) -> bool {
1551 let f = |x: i32| x.wrapping_add(1);
1552 let g = |x: i32| x.wrapping_mul(2);
1553 map::<ResultWithErrBrand<i32>, _, _, _>(compose(f, g), x)
1554 == map::<ResultWithErrBrand<i32>, _, _, _>(
1555 f,
1556 map::<ResultWithErrBrand<i32>, _, _, _>(g, x),
1557 )
1558 }
1559
1560 #[quickcheck]
1564 fn applicative_identity(v: Result<i32, i32>) -> bool {
1565 apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(
1566 pure::<ResultWithErrBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(identity)),
1567 v,
1568 ) == v
1569 }
1570
1571 #[quickcheck]
1573 fn applicative_homomorphism(x: i32) -> bool {
1574 let f = |x: i32| x.wrapping_mul(2);
1575 apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(
1576 pure::<ResultWithErrBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f)),
1577 pure::<ResultWithErrBrand<i32>, _>(x),
1578 ) == pure::<ResultWithErrBrand<i32>, _>(f(x))
1579 }
1580
1581 #[quickcheck]
1583 fn applicative_composition(
1584 w: Result<i32, i32>,
1585 u_is_ok: bool,
1586 v_is_ok: bool,
1587 ) -> bool {
1588 let v_fn = |x: i32| x.wrapping_mul(2);
1589 let u_fn = |x: i32| x.wrapping_add(1);
1590
1591 let v = if v_is_ok {
1592 pure::<ResultWithErrBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(v_fn))
1593 } else {
1594 Err(100)
1595 };
1596 let u = if u_is_ok {
1597 pure::<ResultWithErrBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(u_fn))
1598 } else {
1599 Err(200)
1600 };
1601
1602 let vw = apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(v.clone(), w.clone());
1604 let rhs = apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(u.clone(), vw);
1605
1606 let uv = match (u, v) {
1609 (Ok(uf), Ok(vf)) => {
1610 let composed = move |x| uf(vf(x));
1611 Ok(<RcFnBrand as CloneableFn>::new(composed))
1612 }
1613 (Err(e), _) => Err(e),
1614 (_, Err(e)) => Err(e),
1615 };
1616
1617 let lhs = apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(uv, w);
1618
1619 lhs == rhs
1620 }
1621
1622 #[quickcheck]
1624 fn applicative_interchange(y: i32) -> bool {
1625 let f = |x: i32| x.wrapping_mul(2);
1627 let u = pure::<ResultWithErrBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f));
1628
1629 let lhs = apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(
1630 u.clone(),
1631 pure::<ResultWithErrBrand<i32>, _>(y),
1632 );
1633
1634 let rhs_fn =
1635 <RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1636 let rhs = apply::<RcFnBrand, ResultWithErrBrand<i32>, _, _>(
1637 pure::<ResultWithErrBrand<i32>, _>(rhs_fn),
1638 u,
1639 );
1640
1641 lhs == rhs
1642 }
1643
1644 #[quickcheck]
1648 fn monad_left_identity(a: i32) -> bool {
1649 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1650 bind::<ResultWithErrBrand<i32>, _, _, _>(pure::<ResultWithErrBrand<i32>, _>(a), f) == f(a)
1651 }
1652
1653 #[quickcheck]
1655 fn monad_right_identity(m: Result<i32, i32>) -> bool {
1656 bind::<ResultWithErrBrand<i32>, _, _, _>(m, pure::<ResultWithErrBrand<i32>, _>) == m
1657 }
1658
1659 #[quickcheck]
1661 fn monad_associativity(m: Result<i32, i32>) -> bool {
1662 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1663 let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
1664 bind::<ResultWithErrBrand<i32>, _, _, _>(bind::<ResultWithErrBrand<i32>, _, _, _>(m, f), g)
1665 == bind::<ResultWithErrBrand<i32>, _, _, _>(m, |x| {
1666 bind::<ResultWithErrBrand<i32>, _, _, _>(f(x), g)
1667 })
1668 }
1669
1670 #[test]
1674 fn map_err() {
1675 assert_eq!(
1676 map::<ResultWithErrBrand<i32>, _, _, _>(|x: i32| x + 1, Err::<i32, i32>(1)),
1677 Err(1)
1678 );
1679 }
1680
1681 #[test]
1683 fn bind_err() {
1684 assert_eq!(
1685 bind::<ResultWithErrBrand<i32>, _, _, _>(Err::<i32, i32>(1), |x: i32| Ok(x + 1)),
1686 Err(1)
1687 );
1688 }
1689
1690 #[test]
1692 fn bind_returning_err() {
1693 assert_eq!(bind::<ResultWithErrBrand<i32>, _, _, _>(Ok(1), |_| Err::<i32, i32>(2)), Err(2));
1694 }
1695
1696 #[test]
1698 fn fold_right_err() {
1699 assert_eq!(
1700 crate::classes::foldable::fold_right::<RcFnBrand, ResultWithErrBrand<i32>, _, _, _>(
1701 |x: i32, acc| x + acc,
1702 0,
1703 Err(1)
1704 ),
1705 0
1706 );
1707 }
1708
1709 #[test]
1711 fn fold_left_err() {
1712 assert_eq!(
1713 crate::classes::foldable::fold_left::<RcFnBrand, ResultWithErrBrand<i32>, _, _, _>(
1714 |acc, x: i32| acc + x,
1715 0,
1716 Err(1)
1717 ),
1718 0
1719 );
1720 }
1721
1722 #[test]
1724 fn traverse_err() {
1725 assert_eq!(
1726 crate::classes::traversable::traverse::<ResultWithErrBrand<i32>, _, _, OptionBrand, _>(
1727 |x: i32| Some(x + 1),
1728 Err(1)
1729 ),
1730 Some(Err(1))
1731 );
1732 }
1733
1734 #[test]
1736 fn traverse_returning_err() {
1737 assert_eq!(
1738 crate::classes::traversable::traverse::<ResultWithErrBrand<i32>, _, _, OptionBrand, _>(
1739 |_: i32| None::<i32>,
1740 Ok(1)
1741 ),
1742 None
1743 );
1744 }
1745
1746 #[test]
1750 fn par_fold_map_ok() {
1751 let x: Result<i32, ()> = Ok(5);
1752 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1753 assert_eq!(par_fold_map::<ArcFnBrand, ResultWithErrBrand<()>, _, _>(f, x), "5".to_string());
1754 }
1755
1756 #[test]
1758 fn par_fold_map_err_val() {
1759 let x: Result<i32, i32> = Err(5);
1760 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1761 assert_eq!(par_fold_map::<ArcFnBrand, ResultWithErrBrand<i32>, _, _>(f, x), "".to_string());
1762 }
1763
1764 #[test]
1766 fn par_fold_right_ok() {
1767 let x: Result<i32, ()> = Ok(5);
1768 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1769 assert_eq!(par_fold_right::<ArcFnBrand, ResultWithErrBrand<()>, _, _>(f, 10, x), 15);
1770 }
1771
1772 #[test]
1774 fn par_fold_right_err_val() {
1775 let x: Result<i32, i32> = Err(5);
1776 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1777 assert_eq!(par_fold_right::<ArcFnBrand, ResultWithErrBrand<i32>, _, _>(f, 10, x), 10);
1778 }
1779
1780 #[test]
1784 fn par_fold_map_err_ok_brand() {
1785 let x: Result<(), i32> = Err(5);
1786 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1787 assert_eq!(
1788 par_fold_map::<ArcFnBrand, ResultWithOkBrand<()>, _, _>(f.clone(), x),
1789 "5".to_string()
1790 );
1791 }
1792
1793 #[test]
1795 fn par_fold_map_ok_ok_brand() {
1796 let x: Result<i32, i32> = Ok(5);
1797 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1798 assert_eq!(par_fold_map::<ArcFnBrand, ResultWithOkBrand<i32>, _, _>(f, x), "".to_string());
1799 }
1800
1801 #[test]
1803 fn par_fold_right_err_ok_brand() {
1804 let x: Result<(), i32> = Err(5);
1805 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1806 assert_eq!(par_fold_right::<ArcFnBrand, ResultWithOkBrand<()>, _, _>(f.clone(), 10, x), 15);
1807 }
1808
1809 #[test]
1811 fn par_fold_right_ok_ok_brand() {
1812 let x: Result<i32, i32> = Ok(5);
1813 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1814 assert_eq!(par_fold_right::<ArcFnBrand, ResultWithOkBrand<i32>, _, _>(f, 10, x), 10);
1815 }
1816}