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 MonadRec,
27 Monoid,
28 Pointed,
29 Semiapplicative,
30 Semimonad,
31 Traversable,
32 },
33 impl_kind,
34 kinds::*,
35 },
36 core::ops::ControlFlow,
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 success type.")]
1325 impl<T: Clone + 'static> MonadRec for ResultOkAppliedBrand<T> {
1326 #[document_signature]
1333 #[document_type_parameters(
1335 "The lifetime of the computation.",
1336 "The type of the initial value and loop state.",
1337 "The type of the result."
1338 )]
1339 #[document_parameters("The step function.", "The initial value.")]
1341 #[document_returns(
1343 "The result of the computation, or a success if the step function returned `Ok`."
1344 )]
1345 #[document_examples]
1347 fn tail_rec_m<'a, A: 'a, B: 'a>(
1367 func: impl Fn(
1368 A,
1369 )
1370 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1371 + 'a,
1372 initial: A,
1373 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1374 let mut current = initial;
1375 loop {
1376 match func(current) {
1377 Ok(t) => return Ok(t),
1378 Err(ControlFlow::Continue(next)) => current = next,
1379 Err(ControlFlow::Break(b)) => return Err(b),
1380 }
1381 }
1382 }
1383 }
1384
1385 #[document_type_parameters("The error type.")]
1387 impl<E: Clone + 'static> MonadRec for ResultErrAppliedBrand<E> {
1388 #[document_signature]
1395 #[document_type_parameters(
1397 "The lifetime of the computation.",
1398 "The type of the initial value and loop state.",
1399 "The type of the result."
1400 )]
1401 #[document_parameters("The step function.", "The initial value.")]
1403 #[document_returns(
1405 "The result of the computation, or an error if the step function returned `Err`."
1406 )]
1407 #[document_examples]
1409 fn tail_rec_m<'a, A: 'a, B: 'a>(
1429 func: impl Fn(
1430 A,
1431 )
1432 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1433 + 'a,
1434 initial: A,
1435 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1436 let mut current = initial;
1437 loop {
1438 match func(current) {
1439 Err(e) => return Err(e),
1440 Ok(ControlFlow::Continue(next)) => current = next,
1441 Ok(ControlFlow::Break(b)) => return Ok(b),
1442 }
1443 }
1444 }
1445 }
1446}
1447
1448#[cfg(test)]
1449mod tests {
1450
1451 use {
1452 crate::{
1453 brands::*,
1454 classes::{
1455 CloneableFn,
1456 bifunctor::*,
1457 },
1458 functions::*,
1459 },
1460 quickcheck_macros::quickcheck,
1461 };
1462
1463 #[test]
1467 fn test_bimap() {
1468 let x: Result<i32, i32> = Ok(5);
1469 assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, x), Ok(10));
1470
1471 let y: Result<i32, i32> = Err(5);
1472 assert_eq!(bimap::<ResultBrand, _, _, _, _>(|e| e + 1, |s| s * 2, y), Err(6));
1473 }
1474
1475 #[quickcheck]
1479 fn bifunctor_identity(x: Result<i32, i32>) -> bool {
1480 bimap::<ResultBrand, _, _, _, _>(identity, identity, x) == x
1481 }
1482
1483 #[quickcheck]
1485 fn bifunctor_composition(x: Result<i32, i32>) -> bool {
1486 let f = |x: i32| x.wrapping_add(1);
1487 let g = |x: i32| x.wrapping_mul(2);
1488 let h = |x: i32| x.wrapping_sub(1);
1489 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1490
1491 bimap::<ResultBrand, _, _, _, _>(compose(f, g), compose(h, i), x)
1492 == bimap::<ResultBrand, _, _, _, _>(f, h, bimap::<ResultBrand, _, _, _, _>(g, i, x))
1493 }
1494
1495 #[quickcheck]
1499 fn functor_identity(x: Result<i32, i32>) -> bool {
1500 map::<ResultErrAppliedBrand<i32>, _, _>(identity, x) == x
1501 }
1502
1503 #[quickcheck]
1505 fn functor_composition(x: Result<i32, i32>) -> bool {
1506 let f = |x: i32| x.wrapping_add(1);
1507 let g = |x: i32| x.wrapping_mul(2);
1508 map::<ResultErrAppliedBrand<i32>, _, _>(compose(f, g), x)
1509 == map::<ResultErrAppliedBrand<i32>, _, _>(
1510 f,
1511 map::<ResultErrAppliedBrand<i32>, _, _>(g, x),
1512 )
1513 }
1514
1515 #[quickcheck]
1519 fn applicative_identity(v: Result<i32, i32>) -> bool {
1520 apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1521 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(identity)),
1522 v,
1523 ) == v
1524 }
1525
1526 #[quickcheck]
1528 fn applicative_homomorphism(x: i32) -> bool {
1529 let f = |x: i32| x.wrapping_mul(2);
1530 apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1531 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f)),
1532 pure::<ResultErrAppliedBrand<i32>, _>(x),
1533 ) == pure::<ResultErrAppliedBrand<i32>, _>(f(x))
1534 }
1535
1536 #[quickcheck]
1538 fn applicative_composition(
1539 w: Result<i32, i32>,
1540 u_is_ok: bool,
1541 v_is_ok: bool,
1542 ) -> bool {
1543 let v_fn = |x: i32| x.wrapping_mul(2);
1544 let u_fn = |x: i32| x.wrapping_add(1);
1545
1546 let v = if v_is_ok {
1547 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(v_fn))
1548 } else {
1549 Err(100)
1550 };
1551 let u = if u_is_ok {
1552 pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(u_fn))
1553 } else {
1554 Err(200)
1555 };
1556
1557 let vw = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(v.clone(), w);
1559 let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(u.clone(), vw);
1560
1561 let uv = match (u, v) {
1564 (Ok(uf), Ok(vf)) => {
1565 let composed = move |x| uf(vf(x));
1566 Ok(<RcFnBrand as CloneableFn>::new(composed))
1567 }
1568 (Err(e), _) => Err(e),
1569 (_, Err(e)) => Err(e),
1570 };
1571
1572 let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(uv, w);
1573
1574 lhs == rhs
1575 }
1576
1577 #[quickcheck]
1579 fn applicative_interchange(y: i32) -> bool {
1580 let f = |x: i32| x.wrapping_mul(2);
1582 let u = pure::<ResultErrAppliedBrand<i32>, _>(<RcFnBrand as CloneableFn>::new(f));
1583
1584 let lhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1585 u.clone(),
1586 pure::<ResultErrAppliedBrand<i32>, _>(y),
1587 );
1588
1589 let rhs_fn =
1590 <RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1591 let rhs = apply::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1592 pure::<ResultErrAppliedBrand<i32>, _>(rhs_fn),
1593 u,
1594 );
1595
1596 lhs == rhs
1597 }
1598
1599 #[quickcheck]
1603 fn monad_left_identity(a: i32) -> bool {
1604 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1605 bind::<ResultErrAppliedBrand<i32>, _, _>(pure::<ResultErrAppliedBrand<i32>, _>(a), f)
1606 == f(a)
1607 }
1608
1609 #[quickcheck]
1611 fn monad_right_identity(m: Result<i32, i32>) -> bool {
1612 bind::<ResultErrAppliedBrand<i32>, _, _>(m, pure::<ResultErrAppliedBrand<i32>, _>) == m
1613 }
1614
1615 #[quickcheck]
1617 fn monad_associativity(m: Result<i32, i32>) -> bool {
1618 let f = |x: i32| -> Result<i32, i32> { Err(x.wrapping_mul(2)) };
1619 let g = |x: i32| -> Result<i32, i32> { Err(x.wrapping_add(1)) };
1620 bind::<ResultErrAppliedBrand<i32>, _, _>(bind::<ResultErrAppliedBrand<i32>, _, _>(m, f), g)
1621 == bind::<ResultErrAppliedBrand<i32>, _, _>(m, |x| {
1622 bind::<ResultErrAppliedBrand<i32>, _, _>(f(x), g)
1623 })
1624 }
1625
1626 #[test]
1630 fn map_err() {
1631 assert_eq!(
1632 map::<ResultErrAppliedBrand<i32>, _, _>(|x: i32| x + 1, Err::<i32, i32>(1)),
1633 Err(1)
1634 );
1635 }
1636
1637 #[test]
1639 fn bind_err() {
1640 assert_eq!(
1641 bind::<ResultErrAppliedBrand<i32>, _, _>(Err::<i32, i32>(1), |x: i32| Ok(x + 1)),
1642 Err(1)
1643 );
1644 }
1645
1646 #[test]
1648 fn bind_returning_err() {
1649 assert_eq!(bind::<ResultErrAppliedBrand<i32>, _, _>(Ok(1), |_| Err::<i32, i32>(2)), Err(2));
1650 }
1651
1652 #[test]
1654 fn fold_right_err() {
1655 assert_eq!(
1656 crate::classes::foldable::fold_right::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1657 |x: i32, acc| x + acc,
1658 0,
1659 Err(1)
1660 ),
1661 0
1662 );
1663 }
1664
1665 #[test]
1667 fn fold_left_err() {
1668 assert_eq!(
1669 crate::classes::foldable::fold_left::<RcFnBrand, ResultErrAppliedBrand<i32>, _, _>(
1670 |acc, x: i32| acc + x,
1671 0,
1672 Err(1)
1673 ),
1674 0
1675 );
1676 }
1677
1678 #[test]
1680 fn traverse_err() {
1681 assert_eq!(
1682 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1683 |x: i32| Some(x + 1),
1684 Err(1)
1685 ),
1686 Some(Err(1))
1687 );
1688 }
1689
1690 #[test]
1692 fn traverse_returning_err() {
1693 assert_eq!(
1694 crate::classes::traversable::traverse::<ResultErrAppliedBrand<i32>, _, _, OptionBrand>(
1695 |_: i32| None::<i32>,
1696 Ok(1)
1697 ),
1698 None
1699 );
1700 }
1701
1702 #[quickcheck]
1706 fn monad_rec_identity(x: i32) -> bool {
1707 use {
1708 crate::classes::monad_rec::tail_rec_m,
1709 core::ops::ControlFlow,
1710 };
1711 tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(|a| Ok(ControlFlow::Break(a)), x) == Ok(x)
1712 }
1713
1714 #[test]
1716 fn monad_rec_sum_range() {
1717 use {
1718 crate::classes::monad_rec::tail_rec_m,
1719 core::ops::ControlFlow,
1720 };
1721 let result = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
1722 |(n, acc)| {
1723 if n == 0 {
1724 Ok(ControlFlow::Break(acc))
1725 } else {
1726 Ok(ControlFlow::Continue((n - 1, acc + n)))
1727 }
1728 },
1729 (100i64, 0i64),
1730 );
1731 assert_eq!(result, Ok(5050));
1732 }
1733
1734 #[test]
1736 fn monad_rec_short_circuit() {
1737 use {
1738 crate::classes::monad_rec::tail_rec_m,
1739 core::ops::ControlFlow,
1740 };
1741 let result: Result<i32, &str> = tail_rec_m::<ResultErrAppliedBrand<&str>, _, _>(
1742 |n| {
1743 if n == 5 { Err("stopped") } else { Ok(ControlFlow::Continue(n + 1)) }
1744 },
1745 0,
1746 );
1747 assert_eq!(result, Err("stopped"));
1748 }
1749
1750 #[test]
1752 fn monad_rec_stack_safety() {
1753 use {
1754 crate::classes::monad_rec::tail_rec_m,
1755 core::ops::ControlFlow,
1756 };
1757 let iterations: i64 = 200_000;
1758 let result = tail_rec_m::<ResultErrAppliedBrand<()>, _, _>(
1759 |acc| {
1760 if acc < iterations {
1761 Ok(ControlFlow::Continue(acc + 1))
1762 } else {
1763 Ok(ControlFlow::Break(acc))
1764 }
1765 },
1766 0i64,
1767 );
1768 assert_eq!(result, Ok(iterations));
1769 }
1770
1771 #[quickcheck]
1776 fn monad_rec_ok_applied_identity(x: i32) -> bool {
1777 use {
1778 crate::classes::monad_rec::tail_rec_m,
1779 core::ops::ControlFlow,
1780 };
1781 tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(|a| Err(ControlFlow::Break(a)), x) == Err(x)
1782 }
1783
1784 #[test]
1786 fn monad_rec_ok_applied_sum_range() {
1787 use {
1788 crate::classes::monad_rec::tail_rec_m,
1789 core::ops::ControlFlow,
1790 };
1791 let result = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1792 |(n, acc)| {
1793 if n == 0 {
1794 Err(ControlFlow::Break(acc))
1795 } else {
1796 Err(ControlFlow::Continue((n - 1, acc + n)))
1797 }
1798 },
1799 (100i64, 0i64),
1800 );
1801 assert_eq!(result, Err(5050));
1802 }
1803
1804 #[test]
1806 fn monad_rec_ok_applied_short_circuit() {
1807 use {
1808 crate::classes::monad_rec::tail_rec_m,
1809 core::ops::ControlFlow,
1810 };
1811 let result: Result<&str, i32> = tail_rec_m::<ResultOkAppliedBrand<&str>, _, _>(
1812 |n| {
1813 if n == 5 { Ok("stopped") } else { Err(ControlFlow::Continue(n + 1)) }
1814 },
1815 0,
1816 );
1817 assert_eq!(result, Ok("stopped"));
1818 }
1819
1820 #[test]
1822 fn monad_rec_ok_applied_stack_safety() {
1823 use {
1824 crate::classes::monad_rec::tail_rec_m,
1825 core::ops::ControlFlow,
1826 };
1827 let iterations: i64 = 200_000;
1828 let result = tail_rec_m::<ResultOkAppliedBrand<()>, _, _>(
1829 |acc| {
1830 if acc < iterations {
1831 Err(ControlFlow::Continue(acc + 1))
1832 } else {
1833 Err(ControlFlow::Break(acc))
1834 }
1835 },
1836 0i64,
1837 );
1838 assert_eq!(result, Err(iterations));
1839 }
1840}