1#[fp_macros::document_module]
6mod inner {
7 use crate::{
8 Apply,
9 brands::{Tuple2Brand, Tuple2WithFirstBrand, Tuple2WithSecondBrand},
10 classes::{
11 Applicative, ApplyFirst, ApplySecond, Bifunctor, CloneableFn, Foldable, Functor, Lift,
12 Monoid, ParFoldable, Pointed, Semiapplicative, Semigroup, Semimonad, SendCloneableFn,
13 Traversable,
14 },
15 impl_kind,
16 kinds::*,
17 };
18 use fp_macros::document_parameters;
19
20 impl_kind! {
21 for Tuple2Brand {
22 type Of<First, Second> = (First, Second);
23 }
24 }
25
26 impl_kind! {
27 for Tuple2Brand {
28 type Of<'a, First: 'a, Second: 'a>: 'a = (First, Second);
29 }
30 }
31
32 impl Bifunctor for Tuple2Brand {
33 #[document_signature]
40 #[document_type_parameters(
44 "The lifetime of the values.",
45 "The type of the first value.",
46 "The type of the mapped first value.",
47 "The type of the second value.",
48 "The type of the mapped second value.",
49 "The type of the function to apply to the first value.",
50 "The type of the function to apply to the second value."
51 )]
52 #[document_parameters(
56 "The function to apply to the first value.",
57 "The function to apply to the second value.",
58 "The tuple to map over."
59 )]
60 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a, F, G>(
74 f: F,
75 g: G,
76 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
77 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>)
78 where
79 F: Fn(A) -> B + 'a,
80 G: Fn(C) -> D + 'a,
81 {
82 (f(p.0), g(p.1))
83 }
84 }
85
86 impl_kind! {
89 impl<First: 'static> for Tuple2WithFirstBrand<First> {
90 type Of<'a, A: 'a>: 'a = (First, A);
91 }
92 }
93
94 #[document_type_parameters("The type of the first value in the tuple.")]
97 impl<First: 'static> Functor for Tuple2WithFirstBrand<First> {
98 #[document_signature]
105 #[document_type_parameters(
109 "The lifetime of the values.",
110 "The type of the second value.",
111 "The type of the result of applying the function.",
112 "The type of the function to apply."
113 )]
114 #[document_parameters(
118 "The function to apply to the second value.",
119 "The tuple to map over."
120 )]
121 fn map<'a, A: 'a, B: 'a, Func>(
134 func: Func,
135 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
136 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
137 where
138 Func: Fn(A) -> B + 'a,
139 {
140 (fa.0, func(fa.1))
141 }
142 }
143
144 #[document_type_parameters("The type of the first value in the tuple.")]
147 impl<First: Clone + 'static> Lift for Tuple2WithFirstBrand<First>
148 where
149 First: Semigroup,
150 {
151 #[document_signature]
158 #[document_type_parameters(
162 "The lifetime of the values.",
163 "The type of the first second value.",
164 "The type of the second second value.",
165 "The type of the result second value.",
166 "The type of the binary function."
167 )]
168 #[document_parameters(
172 "The binary function to apply to the second values.",
173 "The first tuple.",
174 "The second tuple."
175 )]
176 fn lift2<'a, A, B, C, Func>(
192 func: Func,
193 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
194 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
195 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
196 where
197 Func: Fn(A, B) -> C + 'a,
198 A: Clone + 'a,
199 B: Clone + 'a,
200 C: 'a,
201 {
202 (Semigroup::append(fa.0, fb.0), func(fa.1, fb.1))
203 }
204 }
205
206 #[document_type_parameters("The type of the first value in the tuple.")]
209 impl<First: Clone + 'static> Pointed for Tuple2WithFirstBrand<First>
210 where
211 First: Monoid,
212 {
213 #[document_signature]
220 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
224 #[document_parameters("The value to wrap.")]
228 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
241 (Monoid::empty(), a)
242 }
243 }
244
245 #[document_type_parameters("The type of the first value in the tuple.")]
248 impl<First: Clone + Semigroup + 'static> ApplyFirst for Tuple2WithFirstBrand<First> {}
249 #[document_type_parameters("The type of the first value in the tuple.")]
252 impl<First: Clone + Semigroup + 'static> ApplySecond for Tuple2WithFirstBrand<First> {}
253
254 #[document_type_parameters("The type of the first value in the tuple.")]
257 impl<First: Clone + 'static> Semiapplicative for Tuple2WithFirstBrand<First>
258 where
259 First: Semigroup,
260 {
261 #[document_signature]
268 #[document_type_parameters(
272 "The lifetime of the values.",
273 "The brand of the cloneable function wrapper.",
274 "The type of the input value.",
275 "The type of the output value."
276 )]
277 #[document_parameters(
281 "The tuple containing the function.",
282 "The tuple containing the value."
283 )]
284 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
298 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
299 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
300 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
301 (Semigroup::append(ff.0, fa.0), ff.1(fa.1))
302 }
303 }
304
305 #[document_type_parameters("The type of the first value in the tuple.")]
308 impl<First: Clone + 'static> Semimonad for Tuple2WithFirstBrand<First>
309 where
310 First: Semigroup,
311 {
312 #[document_signature]
319 #[document_type_parameters(
323 "The lifetime of the values.",
324 "The type of the result of the first computation.",
325 "The type of the result of the second computation.",
326 "The type of the function to apply."
327 )]
328 #[document_parameters("The first tuple.", "The function to apply to the second value.")]
332 fn bind<'a, A: 'a, B: 'a, Func>(
348 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
349 func: Func,
350 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
351 where
352 Func: Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
353 {
354 let (first, second) = ma;
355 let (next_first, next_second) = func(second);
356 (Semigroup::append(first, next_first), next_second)
357 }
358 }
359
360 #[document_type_parameters("The type of the first value in the tuple.")]
363 impl<First: 'static> Foldable for Tuple2WithFirstBrand<First> {
364 #[document_signature]
371 #[document_type_parameters(
375 "The lifetime of the values.",
376 "The brand of the cloneable function to use.",
377 "The type of the elements in the structure.",
378 "The type of the accumulator.",
379 "The type of the folding function."
380 )]
381 #[document_parameters("The folding function.", "The initial value.", "The tuple to fold.")]
385 fn fold_right<'a, FnBrand, A: 'a, B: 'a, Func>(
398 func: Func,
399 initial: B,
400 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
401 ) -> B
402 where
403 Func: Fn(A, B) -> B + 'a,
404 FnBrand: CloneableFn + 'a,
405 {
406 func(fa.1, initial)
407 }
408
409 #[document_signature]
416 #[document_type_parameters(
420 "The lifetime of the values.",
421 "The brand of the cloneable function to use.",
422 "The type of the elements in the structure.",
423 "The type of the accumulator.",
424 "The type of the folding function."
425 )]
426 #[document_parameters(
430 "The function to apply to the accumulator and each element.",
431 "The initial value of the accumulator.",
432 "The tuple to fold."
433 )]
434 fn fold_left<'a, FnBrand, A: 'a, B: 'a, Func>(
447 func: Func,
448 initial: B,
449 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
450 ) -> B
451 where
452 Func: Fn(B, A) -> B + 'a,
453 FnBrand: CloneableFn + 'a,
454 {
455 func(initial, fa.1)
456 }
457
458 #[document_signature]
465 #[document_type_parameters(
469 "The lifetime of the values.",
470 "The brand of the cloneable function to use.",
471 "The type of the elements in the structure.",
472 "The type of the monoid.",
473 "The type of the mapping function."
474 )]
475 #[document_parameters("The mapping function.", "The tuple to fold.")]
479 fn fold_map<'a, FnBrand, A: 'a, M, Func>(
495 func: Func,
496 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
497 ) -> M
498 where
499 M: Monoid + 'a,
500 Func: Fn(A) -> M + 'a,
501 FnBrand: CloneableFn + 'a,
502 {
503 func(fa.1)
504 }
505 }
506
507 #[document_type_parameters("The type of the first value in the tuple.")]
510 impl<First: Clone + 'static> Traversable for Tuple2WithFirstBrand<First> {
511 #[document_signature]
518 #[document_type_parameters(
522 "The lifetime of the values.",
523 "The type of the elements in the traversable structure.",
524 "The type of the elements in the resulting traversable structure.",
525 "The applicative context.",
526 "The type of the function to apply."
527 )]
528 #[document_parameters(
532 "The function to apply to each element, returning a value in an applicative context.",
533 "The tuple to traverse."
534 )]
535 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
551 func: Func,
552 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
553 ) -> 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>)>)
554 where
555 Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
556 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
557 {
558 let (first, second) = ta;
559 F::map(move |b| (first.clone(), b), func(second))
560 }
561 #[document_signature]
568 #[document_type_parameters(
572 "The lifetime of the values.",
573 "The type of the elements in the traversable structure.",
574 "The applicative context."
575 )]
576 #[document_parameters("The tuple containing the applicative value.")]
580 fn sequence<'a, A: 'a + Clone, F: Applicative>(
596 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>)>)
597 ) -> 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>)>)
598 where
599 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
600 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
601 {
602 let (first, second) = ta;
603 F::map(move |a| (first.clone(), a), second)
604 }
605 }
606
607 #[document_type_parameters("The type of the first value in the tuple.")]
610 impl<First: 'static> ParFoldable for Tuple2WithFirstBrand<First> {
611 #[document_signature]
618 #[document_type_parameters(
622 "The lifetime of the values.",
623 "The brand of the cloneable function wrapper.",
624 "The element type.",
625 "The monoid type."
626 )]
627 #[document_parameters(
631 "The thread-safe function to map each element to a monoid.",
632 "The tuple to fold."
633 )]
634 fn par_fold_map<'a, FnBrand, A, M>(
652 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
653 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
654 ) -> M
655 where
656 FnBrand: 'a + SendCloneableFn,
657 A: 'a + Clone + Send + Sync,
658 M: Monoid + Send + Sync + 'a,
659 {
660 func(fa.1)
661 }
662
663 #[document_signature]
670 #[document_type_parameters(
674 "The lifetime of the values.",
675 "The brand of the cloneable function wrapper.",
676 "The element type.",
677 "The accumulator type."
678 )]
679 #[document_parameters(
683 "The thread-safe function to apply to each element and the accumulator.",
684 "The initial value.",
685 "The tuple to fold."
686 )]
687 fn par_fold_right<'a, FnBrand, A, B>(
702 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
703 initial: B,
704 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
705 ) -> B
706 where
707 FnBrand: 'a + SendCloneableFn,
708 A: 'a + Clone + Send + Sync,
709 B: Send + Sync + 'a,
710 {
711 func((fa.1, initial))
712 }
713 }
714
715 impl_kind! {
718 impl<Second: 'static> for Tuple2WithSecondBrand<Second> {
719 type Of<'a, A: 'a>: 'a = (A, Second);
720 }
721 }
722
723 #[document_type_parameters("The type of the second value in the tuple.")]
726 impl<Second: 'static> Functor for Tuple2WithSecondBrand<Second> {
727 #[document_signature]
734 #[document_type_parameters(
738 "The lifetime of the values.",
739 "The type of the first value.",
740 "The type of the result of applying the function.",
741 "The type of the function to apply."
742 )]
743 #[document_parameters(
747 "The function to apply to the first value.",
748 "The tuple to map over."
749 )]
750 fn map<'a, A: 'a, B: 'a, Func>(
763 func: Func,
764 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
765 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
766 where
767 Func: Fn(A) -> B + 'a,
768 {
769 (func(fa.0), fa.1)
770 }
771 }
772
773 #[document_type_parameters("The type of the second value in the tuple.")]
776 impl<Second: Clone + 'static> Lift for Tuple2WithSecondBrand<Second>
777 where
778 Second: Semigroup,
779 {
780 #[document_signature]
787 #[document_type_parameters(
791 "The lifetime of the values.",
792 "The type of the first first value.",
793 "The type of the second first value.",
794 "The type of the result first value.",
795 "The type of the binary function."
796 )]
797 #[document_parameters(
801 "The binary function to apply to the first values.",
802 "The first tuple.",
803 "The second tuple."
804 )]
805 fn lift2<'a, A, B, C, Func>(
821 func: Func,
822 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
823 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
824 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
825 where
826 Func: Fn(A, B) -> C + 'a,
827 A: Clone + 'a,
828 B: Clone + 'a,
829 C: 'a,
830 {
831 (func(fa.0, fb.0), Semigroup::append(fa.1, fb.1))
832 }
833 }
834
835 #[document_type_parameters("The type of the second value in the tuple.")]
838 impl<Second: Clone + 'static> Pointed for Tuple2WithSecondBrand<Second>
839 where
840 Second: Monoid,
841 {
842 #[document_signature]
849 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
853 #[document_parameters("The value to wrap.")]
857 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
870 (a, Monoid::empty())
871 }
872 }
873
874 #[document_type_parameters("The type of the second value in the tuple.")]
877 impl<Second: Clone + Semigroup + 'static> ApplyFirst for Tuple2WithSecondBrand<Second> {}
878 #[document_type_parameters("The type of the second value in the tuple.")]
881 impl<Second: Clone + Semigroup + 'static> ApplySecond for Tuple2WithSecondBrand<Second> {}
882
883 #[document_type_parameters("The type of the second value in the tuple.")]
886 impl<Second: Clone + 'static> Semiapplicative for Tuple2WithSecondBrand<Second>
887 where
888 Second: Semigroup,
889 {
890 #[document_signature]
897 #[document_type_parameters(
901 "The lifetime of the values.",
902 "The brand of the cloneable function wrapper.",
903 "The type of the input value.",
904 "The type of the output value."
905 )]
906 #[document_parameters(
910 "The tuple containing the function.",
911 "The tuple containing the value."
912 )]
913 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
927 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
928 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
929 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
930 (ff.0(fa.0), Semigroup::append(ff.1, fa.1))
931 }
932 }
933
934 #[document_type_parameters("The type of the second value in the tuple.")]
937 impl<Second: Clone + 'static> Semimonad for Tuple2WithSecondBrand<Second>
938 where
939 Second: Semigroup,
940 {
941 #[document_signature]
948 #[document_type_parameters(
952 "The lifetime of the values.",
953 "The type of the result of the first computation.",
954 "The type of the result of the second computation.",
955 "The type of the function to apply."
956 )]
957 #[document_parameters("The first tuple.", "The function to apply to the first value.")]
961 fn bind<'a, A: 'a, B: 'a, Func>(
977 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
978 func: Func,
979 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
980 where
981 Func: Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
982 {
983 let (first, second) = ma;
984 let (next_first, next_second) = func(first);
985 (next_first, Semigroup::append(second, next_second))
986 }
987 }
988
989 #[document_type_parameters("The type of the second value in the tuple.")]
992 impl<Second: 'static> Foldable for Tuple2WithSecondBrand<Second> {
993 #[document_signature]
1000 #[document_type_parameters(
1004 "The lifetime of the values.",
1005 "The brand of the cloneable function to use.",
1006 "The type of the elements in the structure.",
1007 "The type of the accumulator.",
1008 "The type of the folding function."
1009 )]
1010 #[document_parameters("The folding function.", "The initial value.", "The tuple to fold.")]
1014 fn fold_right<'a, FnBrand, A: 'a, B: 'a, F>(
1027 func: F,
1028 initial: B,
1029 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1030 ) -> B
1031 where
1032 F: Fn(A, B) -> B + 'a,
1033 FnBrand: CloneableFn + 'a,
1034 {
1035 func(fa.0, initial)
1036 }
1037
1038 #[document_signature]
1045 #[document_type_parameters(
1049 "The lifetime of the values.",
1050 "The brand of the cloneable function to use.",
1051 "The type of the elements in the structure.",
1052 "The type of the accumulator.",
1053 "The type of the folding function."
1054 )]
1055 #[document_parameters("The folding function.", "The initial value.", "The tuple to fold.")]
1059 fn fold_left<'a, FnBrand, A: 'a, B: 'a, F>(
1072 func: F,
1073 initial: B,
1074 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1075 ) -> B
1076 where
1077 F: Fn(B, A) -> B + 'a,
1078 FnBrand: CloneableFn + 'a,
1079 {
1080 func(initial, fa.0)
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 tuple to fold.")]
1104 fn fold_map<'a, FnBrand, A: 'a, M, Func>(
1120 func: Func,
1121 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1122 ) -> M
1123 where
1124 M: Monoid + 'a,
1125 Func: Fn(A) -> M + 'a,
1126 FnBrand: CloneableFn + 'a,
1127 {
1128 func(fa.0)
1129 }
1130 }
1131
1132 #[document_type_parameters("The type of the second value in the tuple.")]
1135 impl<Second: Clone + 'static> Traversable for Tuple2WithSecondBrand<Second> {
1136 #[document_signature]
1143 #[document_type_parameters(
1147 "The lifetime of the values.",
1148 "The type of the elements in the traversable structure.",
1149 "The type of the elements in the resulting traversable structure.",
1150 "The applicative context.",
1151 "The type of the function to apply."
1152 )]
1153 #[document_parameters("The function to apply.", "The tuple to traverse.")]
1157 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
1173 func: Func,
1174 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1175 ) -> 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>)>)
1176 where
1177 Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1178 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1179 {
1180 let (first, second) = ta;
1181 F::map(move |b| (b, second.clone()), func(first))
1182 }
1183
1184 #[document_signature]
1191 #[document_type_parameters(
1195 "The lifetime of the values.",
1196 "The type of the elements in the traversable structure.",
1197 "The applicative context."
1198 )]
1199 #[document_parameters("The tuple containing the applicative value.")]
1203 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1219 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>)>)
1220 ) -> 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>)>)
1221 where
1222 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1223 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1224 {
1225 let (first, second) = ta;
1226 F::map(move |a| (a, second.clone()), first)
1227 }
1228 }
1229
1230 #[document_type_parameters("The type of the second value in the tuple.")]
1233 impl<Second: 'static> ParFoldable for Tuple2WithSecondBrand<Second> {
1234 #[document_signature]
1241 #[document_type_parameters(
1245 "The lifetime of the values.",
1246 "The brand of the cloneable function wrapper.",
1247 "The element type.",
1248 "The monoid type."
1249 )]
1250 #[document_parameters(
1254 "The thread-safe function to map each element to a monoid.",
1255 "The tuple to fold."
1256 )]
1257 fn par_fold_map<'a, FnBrand, A, M>(
1275 func: <FnBrand as SendCloneableFn>::SendOf<'a, A, M>,
1276 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1277 ) -> M
1278 where
1279 FnBrand: 'a + SendCloneableFn,
1280 A: 'a + Clone + Send + Sync,
1281 M: Monoid + Send + Sync + 'a,
1282 {
1283 func(fa.0)
1284 }
1285
1286 #[document_signature]
1293 #[document_type_parameters(
1297 "The lifetime of the values.",
1298 "The brand of the cloneable function wrapper.",
1299 "The element type.",
1300 "The accumulator type."
1301 )]
1302 #[document_parameters(
1306 "The thread-safe function to apply to each element and the accumulator.",
1307 "The initial value.",
1308 "The tuple to fold."
1309 )]
1310 fn par_fold_right<'a, FnBrand, A, B>(
1325 func: <FnBrand as SendCloneableFn>::SendOf<'a, (A, B), B>,
1326 initial: B,
1327 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1328 ) -> B
1329 where
1330 FnBrand: 'a + SendCloneableFn,
1331 A: 'a + Clone + Send + Sync,
1332 B: Send + Sync + 'a,
1333 {
1334 func((fa.0, initial))
1335 }
1336 }
1337}
1338
1339#[cfg(test)]
1340mod tests {
1341
1342 use crate::{
1343 brands::*,
1344 classes::{CloneableFn, bifunctor::*},
1345 functions::*,
1346 };
1347 use quickcheck_macros::quickcheck;
1348
1349 #[test]
1353 fn test_bimap() {
1354 let x = (1, 5);
1355 assert_eq!(bimap::<Tuple2Brand, _, _, _, _, _, _>(|a| a + 1, |b| b * 2, x), (2, 10));
1356 }
1357
1358 #[quickcheck]
1362 fn bifunctor_identity(
1363 first: String,
1364 second: i32,
1365 ) -> bool {
1366 let x = (first, second);
1367 bimap::<Tuple2Brand, _, _, _, _, _, _>(identity, identity, x.clone()) == x
1368 }
1369
1370 #[quickcheck]
1372 fn bifunctor_composition(
1373 first: i32,
1374 second: i32,
1375 ) -> bool {
1376 let x = (first, second);
1377 let f = |x: i32| x.wrapping_add(1);
1378 let g = |x: i32| x.wrapping_mul(2);
1379 let h = |x: i32| x.wrapping_sub(1);
1380 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
1381
1382 bimap::<Tuple2Brand, _, _, _, _, _, _>(compose(f, g), compose(h, i), x.clone())
1383 == bimap::<Tuple2Brand, _, _, _, _, _, _>(
1384 f,
1385 h,
1386 bimap::<Tuple2Brand, _, _, _, _, _, _>(g, i, x),
1387 )
1388 }
1389
1390 #[quickcheck]
1394 fn functor_identity(
1395 first: String,
1396 second: i32,
1397 ) -> bool {
1398 let x = (first, second);
1399 map::<Tuple2WithFirstBrand<String>, _, _, _>(identity, x.clone()) == x
1400 }
1401
1402 #[quickcheck]
1404 fn functor_composition(
1405 first: String,
1406 second: i32,
1407 ) -> bool {
1408 let x = (first, second);
1409 let f = |x: i32| x.wrapping_add(1);
1410 let g = |x: i32| x.wrapping_mul(2);
1411 map::<Tuple2WithFirstBrand<String>, _, _, _>(compose(f, g), x.clone())
1412 == map::<Tuple2WithFirstBrand<String>, _, _, _>(
1413 f,
1414 map::<Tuple2WithFirstBrand<String>, _, _, _>(g, x),
1415 )
1416 }
1417
1418 #[quickcheck]
1422 fn applicative_identity(
1423 first: String,
1424 second: i32,
1425 ) -> bool {
1426 let v = (first, second);
1427 apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(
1428 pure::<Tuple2WithFirstBrand<String>, _>(<RcFnBrand as CloneableFn>::new(identity)),
1429 v.clone(),
1430 ) == v
1431 }
1432
1433 #[quickcheck]
1435 fn applicative_homomorphism(x: i32) -> bool {
1436 let f = |x: i32| x.wrapping_mul(2);
1437 apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(
1438 pure::<Tuple2WithFirstBrand<String>, _>(<RcFnBrand as CloneableFn>::new(f)),
1439 pure::<Tuple2WithFirstBrand<String>, _>(x),
1440 ) == pure::<Tuple2WithFirstBrand<String>, _>(f(x))
1441 }
1442
1443 #[quickcheck]
1445 fn applicative_composition(
1446 w_first: String,
1447 w_second: i32,
1448 u_seed: i32,
1449 v_seed: i32,
1450 ) -> bool {
1451 let w = (w_first, w_second);
1452
1453 let u_fn = <RcFnBrand as CloneableFn>::new(move |x: i32| x.wrapping_add(u_seed));
1454 let u = pure::<Tuple2WithFirstBrand<String>, _>(u_fn);
1455
1456 let v_fn = <RcFnBrand as CloneableFn>::new(move |x: i32| x.wrapping_mul(v_seed));
1457 let v = pure::<Tuple2WithFirstBrand<String>, _>(v_fn);
1458
1459 let vw = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(v.clone(), w.clone());
1461 let rhs = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(u.clone(), vw);
1462
1463 let compose_fn = <RcFnBrand as CloneableFn>::new(|f: std::rc::Rc<dyn Fn(i32) -> i32>| {
1465 let f = f.clone();
1466 <RcFnBrand as CloneableFn>::new(move |g: std::rc::Rc<dyn Fn(i32) -> i32>| {
1467 let f = f.clone();
1468 let g = g.clone();
1469 <RcFnBrand as CloneableFn>::new(move |x| f(g(x)))
1470 })
1471 });
1472
1473 let pure_compose = pure::<Tuple2WithFirstBrand<String>, _>(compose_fn);
1474 let u_applied = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(pure_compose, u);
1475 let uv = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(u_applied, v);
1476 let lhs = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(uv, w);
1477
1478 lhs == rhs
1479 }
1480
1481 #[quickcheck]
1483 fn applicative_interchange(
1484 y: i32,
1485 u_seed: i32,
1486 ) -> bool {
1487 let f = move |x: i32| x.wrapping_mul(u_seed);
1489 let u = pure::<Tuple2WithFirstBrand<String>, _>(<RcFnBrand as CloneableFn>::new(f));
1490
1491 let lhs = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(
1492 u.clone(),
1493 pure::<Tuple2WithFirstBrand<String>, _>(y),
1494 );
1495
1496 let rhs_fn =
1497 <RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1498 let rhs = apply::<RcFnBrand, Tuple2WithFirstBrand<String>, _, _>(
1499 pure::<Tuple2WithFirstBrand<String>, _>(rhs_fn),
1500 u,
1501 );
1502
1503 lhs == rhs
1504 }
1505
1506 #[quickcheck]
1510 fn monad_left_identity(a: i32) -> bool {
1511 let f = |x: i32| ("f".to_string(), x.wrapping_mul(2));
1512 bind::<Tuple2WithFirstBrand<String>, _, _, _>(pure::<Tuple2WithFirstBrand<String>, _>(a), f)
1513 == f(a)
1514 }
1515
1516 #[quickcheck]
1518 fn monad_right_identity(
1519 first: String,
1520 second: i32,
1521 ) -> bool {
1522 let m = (first, second);
1523 bind::<Tuple2WithFirstBrand<String>, _, _, _>(
1524 m.clone(),
1525 pure::<Tuple2WithFirstBrand<String>, _>,
1526 ) == m
1527 }
1528
1529 #[quickcheck]
1531 fn monad_associativity(
1532 first: String,
1533 second: i32,
1534 ) -> bool {
1535 let m = (first, second);
1536 let f = |x: i32| ("f".to_string(), x.wrapping_mul(2));
1537 let g = |x: i32| ("g".to_string(), x.wrapping_add(1));
1538 bind::<Tuple2WithFirstBrand<String>, _, _, _>(
1539 bind::<Tuple2WithFirstBrand<String>, _, _, _>(m.clone(), f),
1540 g,
1541 ) == bind::<Tuple2WithFirstBrand<String>, _, _, _>(m, |x| {
1542 bind::<Tuple2WithFirstBrand<String>, _, _, _>(f(x), g)
1543 })
1544 }
1545
1546 #[test]
1550 fn par_fold_map_tuple2_with_first() {
1551 let x = ("a".to_string(), 1);
1552 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1553 assert_eq!(
1554 par_fold_map::<ArcFnBrand, Tuple2WithFirstBrand<String>, _, _>(f, x),
1555 "1".to_string()
1556 );
1557 }
1558
1559 #[test]
1561 fn par_fold_right_tuple2_with_first() {
1562 let x = ("a".to_string(), 1);
1563 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1564 assert_eq!(par_fold_right::<ArcFnBrand, Tuple2WithFirstBrand<String>, _, _>(f, 10, x), 11);
1565 }
1566
1567 #[test]
1571 fn par_fold_map_tuple2_with_second() {
1572 let x = (1, "a".to_string());
1573 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|x: i32| x.to_string());
1574 assert_eq!(
1575 par_fold_map::<ArcFnBrand, Tuple2WithSecondBrand<String>, _, _>(f, x),
1576 "1".to_string()
1577 );
1578 }
1579
1580 #[test]
1582 fn par_fold_right_tuple2_with_second() {
1583 let x = (1, "a".to_string());
1584 let f = send_cloneable_fn_new::<ArcFnBrand, _, _>(|(a, b): (i32, i32)| a + b);
1585 assert_eq!(par_fold_right::<ArcFnBrand, Tuple2WithSecondBrand<String>, _, _>(f, 10, x), 11);
1586 }
1587}