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 Applicative,
17 ApplyFirst,
18 ApplySecond,
19 Bifoldable,
20 Bifunctor,
21 Bitraversable,
22 CloneableFn,
23 Foldable,
24 Functor,
25 Lift,
26 Monoid,
27 ParFoldable,
28 Pointed,
29 Semiapplicative,
30 Semimonad,
31 SendCloneableFn,
32 Traversable,
33 },
34 impl_kind,
35 kinds::*,
36 },
37 fp_macros::*,
38 };
39
40 impl_kind! {
41 for ResultBrand {
48 type Of<A, B> = Result<B, A>;
49 }
50 }
51
52 impl_kind! {
53 for ResultBrand {
57 type Of<'a, A: 'a, B: 'a>: 'a = Result<B, A>;
58 }
59 }
60
61 impl Bifunctor for ResultBrand {
62 #[document_signature]
66 #[document_type_parameters(
68 "The lifetime of the values.",
69 "The type of the error value.",
70 "The type of the mapped error value.",
71 "The type of the success value.",
72 "The type of the mapped success value."
73 )]
74 #[document_parameters(
76 "The function to apply to the error.",
77 "The function to apply to the success.",
78 "The result to map over."
79 )]
80 #[document_returns("A new result containing the mapped values.")]
82 #[document_examples]
83 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
98 f: impl Fn(A) -> B + 'a,
99 g: impl Fn(C) -> D + 'a,
100 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
101 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
102 match p {
103 Ok(c) => Ok(g(c)),
104 Err(a) => Err(f(a)),
105 }
106 }
107 }
108
109 impl Bifoldable for ResultBrand {
110 #[document_signature]
114 #[document_type_parameters(
116 "The lifetime of the values.",
117 "The brand of the cloneable function to use.",
118 "The error type (first position).",
119 "The success type (second position).",
120 "The accumulator type."
121 )]
122 #[document_parameters(
124 "The step function applied to the error value.",
125 "The step function applied to the success value.",
126 "The initial accumulator.",
127 "The result to fold."
128 )]
129 #[document_returns("`f(a, z)` for `Err(a)`, or `g(b, z)` for `Ok(b)`.")]
131 #[document_examples]
132 fn bi_fold_right<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
159 f: impl Fn(A, C) -> C + 'a,
160 g: impl Fn(B, C) -> C + 'a,
161 z: C,
162 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
163 ) -> C {
164 match p {
165 Err(a) => f(a, z),
166 Ok(b) => g(b, z),
167 }
168 }
169
170 #[document_signature]
174 #[document_type_parameters(
176 "The lifetime of the values.",
177 "The brand of the cloneable function to use.",
178 "The error type (first position).",
179 "The success type (second position).",
180 "The accumulator type."
181 )]
182 #[document_parameters(
184 "The step function applied to the error value.",
185 "The step function applied to the success value.",
186 "The initial accumulator.",
187 "The result to fold."
188 )]
189 #[document_returns("`f(z, a)` for `Err(a)`, or `g(z, b)` for `Ok(b)`.")]
191 #[document_examples]
192 fn bi_fold_left<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
219 f: impl Fn(C, A) -> C + 'a,
220 g: impl Fn(C, B) -> C + 'a,
221 z: C,
222 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
223 ) -> C {
224 match p {
225 Err(a) => f(z, a),
226 Ok(b) => g(z, b),
227 }
228 }
229
230 #[document_signature]
234 #[document_type_parameters(
236 "The lifetime of the values.",
237 "The brand of the cloneable function to use.",
238 "The error type (first position).",
239 "The success type (second position).",
240 "The monoid type."
241 )]
242 #[document_parameters(
244 "The function mapping the error to the monoid.",
245 "The function mapping the success to the monoid.",
246 "The result to fold."
247 )]
248 #[document_returns("`f(a)` for `Err(a)`, or `g(b)` for `Ok(b)`.")]
250 #[document_examples]
251 fn bi_fold_map<'a, FnBrand: CloneableFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
276 f: impl Fn(A) -> M + 'a,
277 g: impl Fn(B) -> M + 'a,
278 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
279 ) -> M
280 where
281 M: Monoid + 'a, {
282 match p {
283 Err(a) => f(a),
284 Ok(b) => g(b),
285 }
286 }
287 }
288
289 impl Bitraversable for ResultBrand {
290 #[document_signature]
295 #[document_type_parameters(
297 "The lifetime of the values.",
298 "The error type (first position).",
299 "The success type (second position).",
300 "The output error type.",
301 "The output success type.",
302 "The applicative context."
303 )]
304 #[document_parameters(
306 "The function applied to the error value.",
307 "The function applied to the success value.",
308 "The result to traverse."
309 )]
310 #[document_returns(
312 "`f(a)` wrapped in context for `Err(a)`, or `g(b)` wrapped in context for `Ok(b)`."
313 )]
314 #[document_examples]
315 fn bi_traverse<
340 'a,
341 A: 'a + Clone,
342 B: 'a + Clone,
343 C: 'a + Clone,
344 D: 'a + Clone,
345 F: Applicative,
346 >(
347 f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
348 g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
349 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
350 ) -> 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>)>)
351 {
352 match p {
353 Err(a) => F::map(|c| Err(c), f(a)),
354 Ok(b) => F::map(|d| Ok(d), g(b)),
355 }
356 }
357 }
358
359 impl_kind! {
362 #[document_type_parameters("The error type.")]
363 impl<E: 'static> for ResultErrAppliedBrand<E> {
364 type Of<'a, A: 'a>: 'a = Result<A, E>;
365 }
366 }
367
368 #[document_type_parameters("The error type.")]
369 impl<E: 'static> Functor for ResultErrAppliedBrand<E> {
370 #[document_signature]
374 #[document_type_parameters(
376 "The lifetime of the values.",
377 "The type of the value inside the result.",
378 "The type of the result of applying the function."
379 )]
380 #[document_parameters("The function to apply.", "The result to map over.")]
382 #[document_returns(
384 "A new result containing the result of applying the function, or the original error."
385 )]
386 #[document_examples]
388 fn map<'a, A: 'a, B: 'a>(
399 func: impl Fn(A) -> B + 'a,
400 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
401 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
402 fa.map(func)
403 }
404 }
405
406 #[document_type_parameters("The error type.")]
407 impl<E: Clone + 'static> Lift for ResultErrAppliedBrand<E> {
408 #[document_signature]
412 #[document_type_parameters(
414 "The lifetime of the values.",
415 "The type of the first value.",
416 "The type of the second value.",
417 "The type of the result."
418 )]
419 #[document_parameters(
421 "The binary function to apply.",
422 "The first result.",
423 "The second result."
424 )]
425 #[document_returns(
427 "`Ok(f(a, b))` if both results are `Ok`, otherwise the first error encountered."
428 )]
429 #[document_examples]
430 fn lift2<'a, A, B, C>(
455 func: impl Fn(A, B) -> C + 'a,
456 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
457 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
458 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
459 where
460 A: Clone + 'a,
461 B: Clone + 'a,
462 C: 'a, {
463 match (fa, fb) {
464 (Ok(a), Ok(b)) => Ok(func(a, b)),
465 (Err(e), _) => Err(e),
466 (_, Err(e)) => Err(e),
467 }
468 }
469 }
470
471 #[document_type_parameters("The error type.")]
472 impl<E: 'static> Pointed for ResultErrAppliedBrand<E> {
473 #[document_signature]
477 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
479 #[document_parameters("The value to wrap.")]
481 #[document_returns("`Ok(a)`.")]
483 #[document_examples]
485 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
495 Ok(a)
496 }
497 }
498
499 #[document_type_parameters("The error type.")]
500 impl<E: Clone + 'static> ApplyFirst for ResultErrAppliedBrand<E> {}
501
502 #[document_type_parameters("The error type.")]
503 impl<E: Clone + 'static> ApplySecond for ResultErrAppliedBrand<E> {}
504
505 #[document_type_parameters("The error type.")]
506 impl<E: Clone + 'static> Semiapplicative for ResultErrAppliedBrand<E> {
507 #[document_signature]
511 #[document_type_parameters(
513 "The lifetime of the values.",
514 "The brand of the cloneable function wrapper.",
515 "The type of the input value.",
516 "The type of the output value."
517 )]
518 #[document_parameters(
520 "The result containing the function.",
521 "The result containing the value."
522 )]
523 #[document_returns("`Ok(f(a))` if both are `Ok`, otherwise the first error encountered.")]
525 #[document_examples]
526 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
545 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
546 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
547 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
548 match (ff, fa) {
549 (Ok(f), Ok(a)) => Ok(f(a)),
550 (Err(e), _) => Err(e),
551 (_, Err(e)) => Err(e),
552 }
553 }
554 }
555
556 #[document_type_parameters("The error type.")]
557 impl<E: Clone + 'static> Semimonad for ResultErrAppliedBrand<E> {
558 #[document_signature]
562 #[document_type_parameters(
564 "The lifetime of the values.",
565 "The type of the result of the first computation.",
566 "The type of the result of the second computation."
567 )]
568 #[document_parameters(
570 "The first result.",
571 "The function to apply to the value inside the result."
572 )]
573 #[document_returns(
575 "The result of applying `f` to the value if `ma` is `Ok`, otherwise the original error."
576 )]
577 #[document_examples]
578 fn bind<'a, A: 'a, B: 'a>(
590 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
591 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
592 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
593 ma.and_then(func)
594 }
595 }
596
597 #[document_type_parameters("The error type.")]
598 impl<E: 'static> Foldable for ResultErrAppliedBrand<E> {
599 #[document_signature]
603 #[document_type_parameters(
605 "The lifetime of the values.",
606 "The brand of the cloneable function to use.",
607 "The type of the elements in the structure.",
608 "The type of the accumulator."
609 )]
610 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
612 #[document_returns("`func(a, initial)` if `fa` is `Ok(a)`, otherwise `initial`.")]
614 #[document_examples]
616 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
633 func: impl Fn(A, B) -> B + 'a,
634 initial: B,
635 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
636 ) -> B
637 where
638 FnBrand: CloneableFn + 'a, {
639 match fa {
640 Ok(a) => func(a, initial),
641 Err(_) => initial,
642 }
643 }
644
645 #[document_signature]
649 #[document_type_parameters(
651 "The lifetime of the values.",
652 "The brand of the cloneable function to use.",
653 "The type of the elements in the structure.",
654 "The type of the accumulator."
655 )]
656 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
658 #[document_returns("`func(initial, a)` if `fa` is `Ok(a)`, otherwise `initial`.")]
660 #[document_examples]
662 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
679 func: impl Fn(B, A) -> B + 'a,
680 initial: B,
681 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
682 ) -> B
683 where
684 FnBrand: CloneableFn + 'a, {
685 match fa {
686 Ok(a) => func(initial, a),
687 Err(_) => initial,
688 }
689 }
690
691 #[document_signature]
695 #[document_type_parameters(
697 "The lifetime of the values.",
698 "The brand of the cloneable function to use.",
699 "The type of the elements in the structure.",
700 "The type of the monoid."
701 )]
702 #[document_parameters("The mapping function.", "The result to fold.")]
704 #[document_returns("`func(a)` if `fa` is `Ok(a)`, otherwise `M::empty()`.")]
706 #[document_examples]
708 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
725 func: impl Fn(A) -> M + 'a,
726 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
727 ) -> M
728 where
729 M: Monoid + 'a,
730 FnBrand: CloneableFn + 'a, {
731 match fa {
732 Ok(a) => func(a),
733 Err(_) => M::empty(),
734 }
735 }
736 }
737
738 #[document_type_parameters("The error type.")]
739 impl<E: Clone + 'static> Traversable for ResultErrAppliedBrand<E> {
740 #[document_signature]
744 #[document_type_parameters(
746 "The lifetime of the values.",
747 "The type of the elements in the traversable structure.",
748 "The type of the elements in the resulting traversable structure.",
749 "The applicative context."
750 )]
751 #[document_parameters("The function to apply.", "The result to traverse.")]
753 #[document_returns("The result wrapped in the applicative context.")]
755 #[document_examples]
757 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
781 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
782 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
783 ) -> 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>)>)
784 where
785 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
786 match ta {
787 Ok(a) => F::map(|b| Ok(b), func(a)),
788 Err(e) => F::pure(Err(e)),
789 }
790 }
791
792 #[document_signature]
796 #[document_type_parameters(
798 "The lifetime of the values.",
799 "The type of the elements in the traversable structure.",
800 "The applicative context."
801 )]
802 #[document_parameters("The result containing the applicative value.")]
804 #[document_returns("The result wrapped in the applicative context.")]
806 #[document_examples]
808 fn sequence<'a, A: 'a + Clone, F: Applicative>(
826 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>)>)
827 ) -> 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>)>)
828 where
829 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
830 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
831 match ta {
832 Ok(fa) => F::map(|a| Ok(a), fa),
833 Err(e) => F::pure(Err(e)),
834 }
835 }
836 }
837
838 impl_kind! {
841 #[document_type_parameters("The success type.")]
842 impl<T: 'static> for ResultOkAppliedBrand<T> {
843 type Of<'a, A: 'a>: 'a = Result<T, A>;
844 }
845 }
846
847 #[document_type_parameters("The success type.")]
848 impl<T: 'static> Functor for ResultOkAppliedBrand<T> {
849 #[document_signature]
853 #[document_type_parameters(
855 "The lifetime of the values.",
856 "The type of the error value inside the result.",
857 "The type of the result of applying the function."
858 )]
859 #[document_parameters("The function to apply to the error.", "The result to map over.")]
861 #[document_returns(
863 "A new result containing the mapped error, or the original success value."
864 )]
865 #[document_examples]
867 fn map<'a, A: 'a, B: 'a>(
878 func: impl Fn(A) -> B + 'a,
879 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
880 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
881 match fa {
882 Ok(t) => Ok(t),
883 Err(e) => Err(func(e)),
884 }
885 }
886 }
887
888 #[document_type_parameters("The success type.")]
889 impl<T: Clone + 'static> Lift for ResultOkAppliedBrand<T> {
890 #[document_signature]
894 #[document_type_parameters(
896 "The lifetime of the values.",
897 "The type of the first error value.",
898 "The type of the second error value.",
899 "The type of the result error value."
900 )]
901 #[document_parameters(
903 "The binary function to apply to the errors.",
904 "The first result.",
905 "The second result."
906 )]
907 #[document_returns(
909 "`Err(f(a, b))` if both results are `Err`, otherwise the first success encountered."
910 )]
911 #[document_examples]
912 fn lift2<'a, A, B, C>(
937 func: impl Fn(A, B) -> C + 'a,
938 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
939 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
940 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
941 where
942 A: Clone + 'a,
943 B: Clone + 'a,
944 C: 'a, {
945 match (fa, fb) {
946 (Err(a), Err(b)) => Err(func(a, b)),
947 (Ok(t), _) => Ok(t),
948 (_, Ok(t)) => Ok(t),
949 }
950 }
951 }
952
953 #[document_type_parameters("The success type.")]
954 impl<T: 'static> Pointed for ResultOkAppliedBrand<T> {
955 #[document_signature]
959 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
961 #[document_parameters("The value to wrap.")]
963 #[document_returns("`Err(a)`.")]
965 #[document_examples]
967 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
977 Err(a)
978 }
979 }
980
981 #[document_type_parameters("The success type.")]
982 impl<T: Clone + 'static> ApplyFirst for ResultOkAppliedBrand<T> {}
983
984 #[document_type_parameters("The success type.")]
985 impl<T: Clone + 'static> ApplySecond for ResultOkAppliedBrand<T> {}
986
987 #[document_type_parameters("The success type.")]
988 impl<T: Clone + 'static> Semiapplicative for ResultOkAppliedBrand<T> {
989 #[document_signature]
993 #[document_type_parameters(
995 "The lifetime of the values.",
996 "The brand of the cloneable function wrapper.",
997 "The type of the input value.",
998 "The type of the output value."
999 )]
1000 #[document_parameters(
1002 "The result containing the function (in Err).",
1003 "The result containing the value (in Err)."
1004 )]
1005 #[document_returns(
1007 "`Err(f(a))` if both are `Err`, otherwise the first success encountered."
1008 )]
1009 #[document_examples]
1010 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
1029 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
1030 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1031 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1032 match (ff, fa) {
1033 (Err(f), Err(a)) => Err(f(a)),
1034 (Ok(t), _) => Ok(t),
1035 (_, Ok(t)) => Ok(t),
1036 }
1037 }
1038 }
1039
1040 #[document_type_parameters("The success type.")]
1041 impl<T: Clone + 'static> Semimonad for ResultOkAppliedBrand<T> {
1042 #[document_signature]
1046 #[document_type_parameters(
1048 "The lifetime of the values.",
1049 "The type of the result of the first computation.",
1050 "The type of the result of the second computation."
1051 )]
1052 #[document_parameters("The first result.", "The function to apply to the error value.")]
1054 #[document_returns(
1056 "The result of applying `f` to the error if `ma` is `Err`, otherwise the original success."
1057 )]
1058 #[document_examples]
1060 fn bind<'a, A: 'a, B: 'a>(
1072 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1073 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1074 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1075 match ma {
1076 Ok(t) => Ok(t),
1077 Err(e) => func(e),
1078 }
1079 }
1080 }
1081
1082 #[document_type_parameters("The success type.")]
1083 impl<T: 'static> Foldable for ResultOkAppliedBrand<T> {
1084 #[document_signature]
1088 #[document_type_parameters(
1090 "The lifetime of the values.",
1091 "The brand of the cloneable function to use.",
1092 "The type of the elements in the structure.",
1093 "The type of the accumulator."
1094 )]
1095 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1097 #[document_returns("`func(a, initial)` if `fa` is `Err(a)`, otherwise `initial`.")]
1099 #[document_examples]
1101 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1118 func: impl Fn(A, B) -> B + 'a,
1119 initial: B,
1120 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1121 ) -> B
1122 where
1123 FnBrand: CloneableFn + 'a, {
1124 match fa {
1125 Err(e) => func(e, initial),
1126 Ok(_) => initial,
1127 }
1128 }
1129
1130 #[document_signature]
1134 #[document_type_parameters(
1136 "The lifetime of the values.",
1137 "The brand of the cloneable function to use.",
1138 "The type of the elements in the structure.",
1139 "The type of the accumulator."
1140 )]
1141 #[document_parameters("The folding function.", "The initial value.", "The result to fold.")]
1143 #[document_returns("`func(initial, a)` if `fa` is `Err(a)`, otherwise `initial`.")]
1145 #[document_examples]
1147 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1164 func: impl Fn(B, A) -> B + 'a,
1165 initial: B,
1166 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1167 ) -> B
1168 where
1169 FnBrand: CloneableFn + 'a, {
1170 match fa {
1171 Err(e) => func(initial, e),
1172 Ok(_) => initial,
1173 }
1174 }
1175
1176 #[document_signature]
1180 #[document_type_parameters(
1182 "The lifetime of the values.",
1183 "The brand of the cloneable function to use.",
1184 "The type of the elements in the structure.",
1185 "The type of the monoid."
1186 )]
1187 #[document_parameters("The mapping function.", "The result to fold.")]
1189 #[document_returns("`func(a)` if `fa` is `Err(a)`, otherwise `M::empty()`.")]
1191 #[document_examples]
1193 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1210 func: impl Fn(A) -> M + 'a,
1211 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1212 ) -> M
1213 where
1214 M: Monoid + 'a,
1215 FnBrand: CloneableFn + 'a, {
1216 match fa {
1217 Err(e) => func(e),
1218 Ok(_) => M::empty(),
1219 }
1220 }
1221 }
1222
1223 #[document_type_parameters("The success type.")]
1224 impl<T: Clone + 'static> Traversable for ResultOkAppliedBrand<T> {
1225 #[document_signature]
1229 #[document_type_parameters(
1231 "The lifetime of the values.",
1232 "The type of the elements in the traversable structure.",
1233 "The type of the elements in the resulting traversable structure.",
1234 "The applicative context."
1235 )]
1236 #[document_parameters("The function to apply.", "The result to traverse.")]
1238 #[document_returns("The result wrapped in the applicative context.")]
1240 #[document_examples]
1242 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1266 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1267 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1268 ) -> 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>)>)
1269 where
1270 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1271 match ta {
1272 Err(e) => F::map(|b| Err(b), func(e)),
1273 Ok(t) => F::pure(Ok(t)),
1274 }
1275 }
1276
1277 #[document_signature]
1281 #[document_type_parameters(
1283 "The lifetime of the values.",
1284 "The type of the elements in the traversable structure.",
1285 "The applicative context."
1286 )]
1287 #[document_parameters("The result containing the applicative value.")]
1289 #[document_returns("The result wrapped in the applicative context.")]
1291 #[document_examples]
1293 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1311 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>)>)
1312 ) -> 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>)>)
1313 where
1314 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1315 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1316 match ta {
1317 Err(fe) => F::map(|e| Err(e), fe),
1318 Ok(t) => F::pure(Ok(t)),
1319 }
1320 }
1321 }
1322
1323 #[document_type_parameters("The error type.")]
1324 impl<E: 'static> ParFoldable for ResultErrAppliedBrand<E> {
1325 #[document_signature]
1329 #[document_type_parameters(
1331 "The lifetime of the values.",
1332 "The brand of the cloneable function wrapper.",
1333 "The element type.",
1334 "The monoid type."
1335 )]
1336 #[document_parameters(
1338 "The thread-safe function to map each element to a monoid.",
1339 "The result to fold."
1340 )]
1341 #[document_returns("The combined monoid value.")]
1343 #[document_examples]
1344 fn par_fold_map<'a, FnBrand, A, M>(
1367 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1368 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1369 ) -> M
1370 where
1371 FnBrand: 'a + SendCloneableFn,
1372 A: 'a + Clone + Send + Sync,
1373 M: Monoid + Send + Sync + 'a, {
1374 match fa {
1375 Ok(a) => func(a),
1376 Err(_) => M::empty(),
1377 }
1378 }
1379
1380 #[document_signature]
1384 #[document_type_parameters(
1386 "The lifetime of the values.",
1387 "The brand of the cloneable function wrapper.",
1388 "The element type.",
1389 "The accumulator type."
1390 )]
1391 #[document_parameters(
1393 "The thread-safe function to apply to each element and the accumulator.",
1394 "The initial value.",
1395 "The result to fold."
1396 )]
1397 #[document_returns("The final accumulator value.")]
1399 #[document_examples]
1400 fn par_fold_right<'a, FnBrand, A, B>(
1416 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1417 initial: B,
1418 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1419 ) -> B
1420 where
1421 FnBrand: 'a + SendCloneableFn,
1422 A: 'a + Clone + Send + Sync,
1423 B: Send + Sync + 'a, {
1424 match fa {
1425 Ok(a) => func((a, initial)),
1426 Err(_) => initial,
1427 }
1428 }
1429 }
1430
1431 #[document_type_parameters("The success type.")]
1432 impl<T: 'static> ParFoldable for ResultOkAppliedBrand<T> {
1433 #[document_signature]
1437 #[document_type_parameters(
1439 "The lifetime of the values.",
1440 "The brand of the cloneable function wrapper.",
1441 "The element type.",
1442 "The monoid type."
1443 )]
1444 #[document_parameters(
1446 "The thread-safe function to map each element to a monoid.",
1447 "The result to fold."
1448 )]
1449 #[document_returns("The combined monoid value.")]
1451 #[document_examples]
1452 fn par_fold_map<'a, FnBrand, A, M>(
1475 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1476 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1477 ) -> M
1478 where
1479 FnBrand: 'a + SendCloneableFn,
1480 A: 'a + Clone + Send + Sync,
1481 M: Monoid + Send + Sync + 'a, {
1482 match fa {
1483 Err(e) => func(e),
1484 Ok(_) => M::empty(),
1485 }
1486 }
1487
1488 #[document_signature]
1492 #[document_type_parameters(
1494 "The lifetime of the values.",
1495 "The brand of the cloneable function wrapper.",
1496 "The element type.",
1497 "The accumulator type."
1498 )]
1499 #[document_parameters(
1501 "The thread-safe function to apply to each element and the accumulator.",
1502 "The initial value.",
1503 "The result to fold."
1504 )]
1505 #[document_returns("The final accumulator value.")]
1507 #[document_examples]
1508 fn par_fold_right<'a, FnBrand, A, B>(
1524 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1525 initial: B,
1526 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1527 ) -> B
1528 where
1529 FnBrand: 'a + SendCloneableFn,
1530 A: 'a + Clone + Send + Sync,
1531 B: Send + Sync + 'a, {
1532 match fa {
1533 Err(e) => func((e, initial)),
1534 Ok(_) => initial,
1535 }
1536 }
1537 }
1538}
1539
1540#[cfg(test)]
1541mod tests {
1542
1543 use {
1544 crate::{
1545 brands::*,
1546 classes::{
1547 CloneableFn,
1548 bifunctor::*,
1549 },
1550 functions::*,
1551 },
1552 quickcheck_macros::quickcheck,
1553 };
1554
1555 #[test]
1559 fn test_bimap() {
1560 let x: Result<i32, i32> = Ok(5);
1561 assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x), Ok(10));
1562
1563 let y: Result<i32, i32> = Err(5);
1564 assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, y), Err(6));
1565 }
1566
1567 #[quickcheck]
1571 fn bifunctor_identity(x: Result<i32, i32>) -> bool {
1572 bimap::<ResultBrand, _, _, _, _>(identity, identity, x) == x
1573 }
1574
1575 #[quickcheck]
1577 fn bifunctor_composition(x: Result<i32, i32>) -> bool {
1578 let f = |x: i32| x.wrapping_add(1);
1579 let g = |x: i32| x.wrapping_mul(2);
1580 let h = |x: i32| x.wrapping_sub(1);
1581 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1582
1583 bimap::<ResultBrand, _, _, _, _>(compose(f, g), compose(h, i), x)
1584 == bimap::<ResultBrand, _, _, _, _>(f, h, bimap::<ResultBrand, _, _, _, _>(g, i, x))
1585 }
1586
1587 #[quickcheck]
1591 fn functor_identity(x: Result<i32, i32>) -> bool {
1592 map::<ResultErrAppliedBrand<i32>, _, _>(identity, x) == x
1593 }
1594
1595 #[quickcheck]
1597 fn functor_composition(x: Result<i32, i32>) -> bool {
1598 let f = |x: i32| x.wrapping_add(1);
1599 let g = |x: i32| x.wrapping_mul(2);
1600 map::<ResultErrAppliedBrand<i32>, _, _>(compose(f, g), x)
1601 == map::<ResultErrAppliedBrand<i32>, _, _>(
1602 f,
1603 map::<ResultErrAppliedBrand<i32>, _, _>(g, x),
1604 )
1605 }
1606
1607 #[quickcheck]
1611 fn applicative_identity(v: Result<i32, i32>) -> bool {
1612 apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1613 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(identity)),
1614 v,
1615 ) == v
1616 }
1617
1618 #[quickcheck]
1620 fn applicative_homomorphism(x: i32) -> bool {
1621 let f = |x: i32| x.wrapping_mul(2);
1622 apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1623 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f)),
1624 pure::<ResultErrAppliedBrand<i32>, _>(x),
1625 ) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
1626 }
1627
1628 #[quickcheck]
1630 fn applicative_composition(
1631 w: Result<i32, i32>,
1632 u_is_ok: bool,
1633 v_is_ok: bool,
1634 ) -> bool {
1635 let v_fn = |x: i32| x.wrapping_mul(2);
1636 let u_fn = |x: i32| x.wrapping_add(1);
1637
1638 let v = if v_is_ok {
1639 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(v_fn))
1640 } else {
1641 Err(100)
1642 };
1643 let u = if u_is_ok {
1644 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(u_fn))
1645 } else {
1646 Err(200)
1647 };
1648
1649 let vw = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
1651 let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
1652
1653 let uv = match (u, v) {
1656 (Ok(uf), Ok(vf)) => {
1657 let composed = move |x| uf(vf(x));
1658 Ok(<RcFnBrand as CloneableFn>::new(composed))
1659 }
1660 (Err(e), _) => Err(e),
1661 (_, Err(e)) => Err(e),
1662 };
1663
1664 let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
1665
1666 lhs == rhs
1667 }
1668
1669 #[quickcheck]
1671 fn applicative_interchange(y: i32) -> bool {
1672 let f = |x: i32| x.wrapping_mul(2);
1674 let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f));
1675
1676 let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1677 u.clone(),
1678 pure::<ResultErrAppliedBrand<i32>, _>(y),
1679 );
1680
1681 let rhs_fn =
1682 <RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1683 let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1684 pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
1685 u,
1686 );
1687
1688 lhs == rhs
1689 }
1690
1691 #[quickcheck]
1695 fn monad_left_identity(a: i32) -> bool {
1696 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1697 bind::<ResultErrAppliedBrand<i32>, _, _>(pure::<ResultErrAppliedBrand<i32>, _>(a), f)
1698 == f(a)
1699 }
1700
1701 #[quickcheck]
1703 fn monad_right_identity(m: Result<i32, i32>) -> bool {
1704 bind::<ResultErrAppliedBrand<i32>, _, _>(m, pure::<ResultErrAppliedBrand<i32>, _>) == m
1705 }
1706
1707 #[quickcheck]
1709 fn monad_associativity(m: Result<i32, i32>) -> bool {
1710 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1711 let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
1712 bind::<ResultErrAppliedBrand<i32>, _, _>(bind::<ResultErrAppliedBrand<i32>, _, _>(m, f), g)
1713 == bind::<ResultErrAppliedBrand<i32>, _, _>(m, |x| {
1714 bind::<ResultErrAppliedBrand<i32>, _, _>(f(x), g)
1715 })
1716 }
1717
1718 #[test]
1722 fn map_err() {
1723 assert_eq!(
1724 map::<ResultErrAppliedBrand<i32>, _, _>(|x: i32| x + 1, Err::<i32, i32>(1)),
1725 Err(1)
1726 );
1727 }
1728
1729 #[test]
1731 fn bind_err() {
1732 assert_eq!(
1733 bind::<ResultErrAppliedBrand<i32>, _, _>(Err::<i32, i32>(1), |x: i32| Ok(x + 1)),
1734 Err(1)
1735 );
1736 }
1737
1738 #[test]
1740 fn bind_returning_err() {
1741 assert_eq!(bind::<ResultErrAppliedBrand<i32>, _, _>(Ok(1), |_| Err::<i32, i32>(2)), Err(2));
1742 }
1743
1744 #[test]
1746 fn fold_right_err() {
1747 assert_eq!(
1748 crate::classes::foldable::fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1749 |x: i32, acc| x + acc,
1750 0,
1751 Err(1)
1752 ),
1753 0
1754 );
1755 }
1756
1757 #[test]
1759 fn fold_left_err() {
1760 assert_eq!(
1761 crate::classes::foldable::fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1762 |acc, x: i32| acc + x,
1763 0,
1764 Err(1)
1765 ),
1766 0
1767 );
1768 }
1769
1770 #[test]
1772 fn traverse_err() {
1773 assert_eq!(
1774 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1775 |x: i32| Some(x + 1),
1776 Err(1)
1777 ),
1778 Some(Err(1))
1779 );
1780 }
1781
1782 #[test]
1784 fn traverse_returning_err() {
1785 assert_eq!(
1786 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1787 |_: i32| None::<i32>,
1788 Ok(1)
1789 ),
1790 None
1791 );
1792 }
1793
1794 #[test]
1798 fn par_fold_map_ok() {
1799 let x: Result<i32, ()> = Ok(5);
1800 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1801 assert_eq!(
1802 par_fold_map::<ArcFnBrand, ResultErrAppliedBrand<()>, _, _>(f, x),
1803 "5".to_string()
1804 );
1805 }
1806
1807 #[test]
1809 fn par_fold_map_err_val() {
1810 let x: Result<i32, i32> = Err(5);
1811 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1812 assert_eq!(
1813 par_fold_map::<ArcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, x),
1814 "".to_string()
1815 );
1816 }
1817
1818 #[test]
1820 fn par_fold_right_ok() {
1821 let x: Result<i32, ()> = Ok(5);
1822 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1823 assert_eq!(par_fold_right::<ArcFnBrand, ResultErrAppliedBrand<()>, _, _>(f, 10, x), 15);
1824 }
1825
1826 #[test]
1828 fn par_fold_right_err_val() {
1829 let x: Result<i32, i32> = Err(5);
1830 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1831 assert_eq!(par_fold_right::<ArcFnBrand, ResultErrAppliedBrand<i32>, _, _>(f, 10, x), 10);
1832 }
1833
1834 #[test]
1838 fn par_fold_map_err_ok_brand() {
1839 let x: Result<(), i32> = Err(5);
1840 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1841 assert_eq!(
1842 par_fold_map::<ArcFnBrand, ResultOkAppliedBrand<()>, _, _>(f.clone(), x),
1843 "5".to_string()
1844 );
1845 }
1846
1847 #[test]
1849 fn par_fold_map_ok_ok_brand() {
1850 let x: Result<i32, i32> = Ok(5);
1851 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1852 assert_eq!(
1853 par_fold_map::<ArcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, x),
1854 "".to_string()
1855 );
1856 }
1857
1858 #[test]
1860 fn par_fold_right_err_ok_brand() {
1861 let x: Result<(), i32> = Err(5);
1862 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1863 assert_eq!(
1864 par_fold_right::<ArcFnBrand, ResultOkAppliedBrand<()>, _, _>(f.clone(), 10, x),
1865 15
1866 );
1867 }
1868
1869 #[test]
1871 fn par_fold_right_ok_ok_brand() {
1872 let x: Result<i32, i32> = Ok(5);
1873 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1874 assert_eq!(par_fold_right::<ArcFnBrand, ResultOkAppliedBrand<i32>, _, _>(f, 10, x), 10);
1875 }
1876}