1#[fp_macros::document_module]
6mod inner {
7 use {
8 crate::{
9 Apply,
10 brands::{
11 Tuple2Brand,
12 Tuple2FirstAppliedBrand,
13 Tuple2SecondAppliedBrand,
14 },
15 classes::*,
16 dispatch::Ref,
17 impl_kind,
18 kinds::*,
19 },
20 core::ops::ControlFlow,
21 fp_macros::*,
22 };
23
24 impl_kind! {
25 for Tuple2Brand {
26 type Of<First, Second> = (First, Second);
27 }
28 }
29
30 impl_kind! {
31 for Tuple2Brand {
32 type Of<'a, First: 'a, Second: 'a>: 'a = (First, Second);
33 }
34 }
35
36 impl Bifunctor for Tuple2Brand {
37 #[document_signature]
41 #[document_type_parameters(
43 "The lifetime of the values.",
44 "The type of the first value.",
45 "The type of the mapped first value.",
46 "The type of the second value.",
47 "The type of the mapped second value."
48 )]
49 #[document_parameters(
51 "The function to apply to the first value.",
52 "The function to apply to the second value.",
53 "The tuple to map over."
54 )]
55 #[document_returns("A new tuple containing the mapped values.")]
57 #[document_examples]
58 fn bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
72 f: impl Fn(A) -> B + 'a,
73 g: impl Fn(C) -> D + 'a,
74 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
75 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
76 (f(p.0), g(p.1))
77 }
78 }
79
80 impl RefBifunctor for Tuple2Brand {
81 #[document_signature]
87 #[document_type_parameters(
89 "The lifetime of the values.",
90 "The type of the first value.",
91 "The type of the mapped first value.",
92 "The type of the second value.",
93 "The type of the mapped second value."
94 )]
95 #[document_parameters(
97 "The function to apply to a reference of the first value.",
98 "The function to apply to a reference of the second value.",
99 "The tuple to map over by reference."
100 )]
101 #[document_returns("A new tuple containing the mapped values.")]
103 #[document_examples]
104 fn ref_bimap<'a, A: 'a, B: 'a, C: 'a, D: 'a>(
116 f: impl Fn(&A) -> B + 'a,
117 g: impl Fn(&C) -> D + 'a,
118 p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, C>),
119 ) -> Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, B, D>) {
120 (f(&p.0), g(&p.1))
121 }
122 }
123
124 impl RefBifoldable for Tuple2Brand {
125 #[document_signature]
130 #[document_type_parameters(
132 "The lifetime of the values.",
133 "The brand of the cloneable function to use.",
134 "The type of the first element.",
135 "The type of the second element.",
136 "The accumulator type."
137 )]
138 #[document_parameters(
140 "The step function applied to a reference of the first element.",
141 "The step function applied to a reference of the second element.",
142 "The initial accumulator.",
143 "The tuple to fold by reference."
144 )]
145 #[document_returns("`f(&a, g(&b, z))`.")]
147 #[document_examples]
148 fn ref_bi_fold_right<'a, FnBrand: LiftFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
166 f: impl Fn(&A, C) -> C + 'a,
167 g: impl Fn(&B, C) -> C + 'a,
168 z: C,
169 p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
170 ) -> C {
171 f(&p.0, g(&p.1, z))
172 }
173 }
174
175 impl RefBitraversable for Tuple2Brand {
176 #[document_signature]
181 #[document_type_parameters(
183 "The lifetime of the values.",
184 "The brand of the cloneable function wrapper.",
185 "The type of the first element.",
186 "The type of the second element.",
187 "The output type for the first element.",
188 "The output type for the second element.",
189 "The applicative context."
190 )]
191 #[document_parameters(
193 "The function applied to a reference of the first element.",
194 "The function applied to a reference of the second element.",
195 "The tuple to traverse by reference."
196 )]
197 #[document_returns("`lift2(|c, d| (c, d), f(&a), g(&b))`.")]
199 #[document_examples]
200 fn ref_bi_traverse<
217 'a,
218 FnBrand,
219 A: 'a + Clone,
220 B: 'a + Clone,
221 C: 'a + Clone,
222 D: 'a + Clone,
223 F: Applicative,
224 >(
225 f: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
226 g: impl Fn(&B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
227 p: &Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
228 ) -> 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>)>)
229 where
230 FnBrand: LiftFn + 'a,
231 Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, C, D>): Clone,
232 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>): Clone,
233 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>): Clone, {
234 F::lift2(|c, d| (c, d), f(&p.0), g(&p.1))
235 }
236 }
237
238 impl Bifoldable for Tuple2Brand {
239 #[document_signature]
244 #[document_type_parameters(
246 "The lifetime of the values.",
247 "The brand of the cloneable function to use.",
248 "The type of the first element.",
249 "The type of the second element.",
250 "The accumulator type."
251 )]
252 #[document_parameters(
254 "The step function applied to the first element.",
255 "The step function applied to the second element.",
256 "The initial accumulator.",
257 "The tuple to fold."
258 )]
259 #[document_returns("`f(a, g(b, z))`.")]
261 #[document_examples]
262 fn bi_fold_right<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
279 f: impl Fn(A, C) -> C + 'a,
280 g: impl Fn(B, C) -> C + 'a,
281 z: C,
282 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
283 ) -> C {
284 let (a, b) = p;
285 f(a, g(b, z))
286 }
287
288 #[document_signature]
293 #[document_type_parameters(
295 "The lifetime of the values.",
296 "The brand of the cloneable function to use.",
297 "The type of the first element.",
298 "The type of the second element.",
299 "The accumulator type."
300 )]
301 #[document_parameters(
303 "The step function applied to the first element.",
304 "The step function applied to the second element.",
305 "The initial accumulator.",
306 "The tuple to fold."
307 )]
308 #[document_returns("`g(f(z, a), b)`.")]
310 #[document_examples]
311 fn bi_fold_left<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, C: 'a>(
328 f: impl Fn(C, A) -> C + 'a,
329 g: impl Fn(C, B) -> C + 'a,
330 z: C,
331 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
332 ) -> C {
333 let (a, b) = p;
334 g(f(z, a), b)
335 }
336
337 #[document_signature]
341 #[document_type_parameters(
343 "The lifetime of the values.",
344 "The brand of the cloneable function to use.",
345 "The type of the first element.",
346 "The type of the second element.",
347 "The monoid type."
348 )]
349 #[document_parameters(
351 "The function mapping the first element to the monoid.",
352 "The function mapping the second element to the monoid.",
353 "The tuple to fold."
354 )]
355 #[document_returns("`M::append(f(a), g(b))`.")]
357 #[document_examples]
358 fn bi_fold_map<'a, FnBrand: CloneFn + 'a, A: 'a + Clone, B: 'a + Clone, M>(
374 f: impl Fn(A) -> M + 'a,
375 g: impl Fn(B) -> M + 'a,
376 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
377 ) -> M
378 where
379 M: Monoid + 'a, {
380 let (a, b) = p;
381 M::append(f(a), g(b))
382 }
383 }
384
385 impl Bitraversable for Tuple2Brand {
386 #[document_signature]
391 #[document_type_parameters(
393 "The lifetime of the values.",
394 "The type of the first element.",
395 "The type of the second element.",
396 "The output type for the first element.",
397 "The output type for the second element.",
398 "The applicative context."
399 )]
400 #[document_parameters(
402 "The function applied to the first element.",
403 "The function applied to the second element.",
404 "The tuple to traverse."
405 )]
406 #[document_returns("`lift2(|c, d| (c, d), f(a), g(b))`.")]
408 #[document_examples]
409 fn bi_traverse<
425 'a,
426 A: 'a + Clone,
427 B: 'a + Clone,
428 C: 'a + Clone,
429 D: 'a + Clone,
430 F: Applicative,
431 >(
432 f: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) + 'a,
433 g: impl Fn(B) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, D>) + 'a,
434 p: Apply!(<Self as Kind!( type Of<'a, A: 'a, B: 'a>: 'a; )>::Of<'a, A, B>),
435 ) -> 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>)>)
436 {
437 let (a, b) = p;
438 F::lift2(|c, d| (c, d), f(a), g(b))
439 }
440 }
441
442 impl_kind! {
445 #[multi_brand]
446 impl<First: 'static> for Tuple2FirstAppliedBrand<First> {
447 type Of<'a, A: 'a>: 'a = (First, A);
448 }
449 }
450
451 #[document_type_parameters("The type of the first value in the tuple.")]
452 impl<First: 'static> Functor for Tuple2FirstAppliedBrand<First> {
453 #[document_signature]
457 #[document_type_parameters(
459 "The lifetime of the values.",
460 "The type of the second value.",
461 "The type of the result of applying the function."
462 )]
463 #[document_parameters(
465 "The function to apply to the second value.",
466 "The tuple to map over."
467 )]
468 #[document_returns(
470 "A new tuple containing the result of applying the function to the second value."
471 )]
472 #[document_examples]
473 fn map<'a, A: 'a, B: 'a>(
486 func: impl Fn(A) -> B + 'a,
487 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
488 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
489 (fa.0, func(fa.1))
490 }
491 }
492
493 #[document_type_parameters("The type of the first value in the tuple.")]
494 impl<First: Clone + 'static> Lift for Tuple2FirstAppliedBrand<First>
495 where
496 First: Semigroup,
497 {
498 #[document_signature]
502 #[document_type_parameters(
504 "The lifetime of the values.",
505 "The type of the first second value.",
506 "The type of the second second value.",
507 "The type of the result second value."
508 )]
509 #[document_parameters(
511 "The binary function to apply to the second values.",
512 "The first tuple.",
513 "The second tuple."
514 )]
515 #[document_returns(
517 "A new tuple where the first values are combined using `Semigroup::append` and the second values are combined using `f`."
518 )]
519 #[document_examples]
520 fn lift2<'a, A, B, C>(
537 func: impl Fn(A, B) -> C + 'a,
538 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
539 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
540 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
541 where
542 A: Clone + 'a,
543 B: Clone + 'a,
544 C: 'a, {
545 (Semigroup::append(fa.0, fb.0), func(fa.1, fb.1))
546 }
547 }
548
549 #[document_type_parameters("The type of the first value in the tuple.")]
550 impl<First: Clone + 'static> Pointed for Tuple2FirstAppliedBrand<First>
551 where
552 First: Monoid,
553 {
554 #[document_signature]
558 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
560 #[document_parameters("The value to wrap.")]
562 #[document_returns("A tuple containing the empty value of the first type and `a`.")]
564 #[document_examples]
566 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
576 (Monoid::empty(), a)
577 }
578 }
579
580 #[document_type_parameters("The type of the first value in the tuple.")]
581 impl<First: Clone + Semigroup + 'static> ApplyFirst for Tuple2FirstAppliedBrand<First> {}
582 #[document_type_parameters("The type of the first value in the tuple.")]
583 impl<First: Clone + Semigroup + 'static> ApplySecond for Tuple2FirstAppliedBrand<First> {}
584
585 #[document_type_parameters("The type of the first value in the tuple.")]
586 impl<First: Clone + 'static> Semiapplicative for Tuple2FirstAppliedBrand<First>
587 where
588 First: Semigroup,
589 {
590 #[document_signature]
594 #[document_type_parameters(
596 "The lifetime of the values.",
597 "The brand of the cloneable function wrapper.",
598 "The type of the input value.",
599 "The type of the output value."
600 )]
601 #[document_parameters(
603 "The tuple containing the function.",
604 "The tuple containing the value."
605 )]
606 #[document_returns(
608 "A new tuple where the first values are combined and the function is applied to the second value."
609 )]
610 #[document_examples]
611 fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
626 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
627 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
628 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
629 (Semigroup::append(ff.0, fa.0), ff.1(fa.1))
630 }
631 }
632
633 #[document_type_parameters("The type of the first value in the tuple.")]
634 impl<First: Clone + 'static> Semimonad for Tuple2FirstAppliedBrand<First>
635 where
636 First: Semigroup,
637 {
638 #[document_signature]
642 #[document_type_parameters(
644 "The lifetime of the values.",
645 "The type of the result of the first computation.",
646 "The type of the result of the second computation."
647 )]
648 #[document_parameters("The first tuple.", "The function to apply to the second value.")]
650 #[document_returns("A new tuple where the first values are combined.")]
652 #[document_examples]
654 fn bind<'a, A: 'a, B: 'a>(
670 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
671 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
672 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
673 let (first, second) = ma;
674 let (next_first, next_second) = func(second);
675 (Semigroup::append(first, next_first), next_second)
676 }
677 }
678
679 #[document_type_parameters("The type of the first value in the tuple.")]
680 impl<First: Clone + 'static> MonadRec for Tuple2FirstAppliedBrand<First>
681 where
682 First: Monoid,
683 {
684 #[document_signature]
690 #[document_type_parameters(
692 "The lifetime of the computation.",
693 "The type of the initial value and loop state.",
694 "The type of the result."
695 )]
696 #[document_parameters("The step function.", "The initial value.")]
698 #[document_returns(
700 "A tuple with the accumulated first value and the result of the computation."
701 )]
702 #[document_examples]
704 fn tail_rec_m<'a, A: 'a, B: 'a>(
728 func: impl Fn(
729 A,
730 )
731 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
732 + 'a,
733 initial: A,
734 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
735 let mut acc: First = Monoid::empty();
736 let mut current = initial;
737 loop {
738 let (first, step) = func(current);
739 acc = Semigroup::append(acc, first);
740 match step {
741 ControlFlow::Continue(next) => current = next,
742 ControlFlow::Break(b) => return (acc, b),
743 }
744 }
745 }
746 }
747
748 #[document_type_parameters("The type of the first value in the tuple.")]
749 impl<First: 'static> Foldable for Tuple2FirstAppliedBrand<First> {
750 #[document_signature]
754 #[document_type_parameters(
756 "The lifetime of the values.",
757 "The brand of the cloneable function to use.",
758 "The type of the elements in the structure.",
759 "The type of the accumulator."
760 )]
761 #[document_parameters("The folding function.", "The initial value.", "The tuple to fold.")]
763 #[document_returns("`func(a, initial)`.")]
765 #[document_examples]
767 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
784 func: impl Fn(A, B) -> B + 'a,
785 initial: B,
786 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
787 ) -> B
788 where
789 FnBrand: CloneFn + 'a, {
790 func(fa.1, initial)
791 }
792
793 #[document_signature]
797 #[document_type_parameters(
799 "The lifetime of the values.",
800 "The brand of the cloneable function to use.",
801 "The type of the elements in the structure.",
802 "The type of the accumulator."
803 )]
804 #[document_parameters(
806 "The function to apply to the accumulator and each element.",
807 "The initial value of the accumulator.",
808 "The tuple to fold."
809 )]
810 #[document_returns("`func(initial, a)`.")]
812 #[document_examples]
813 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
830 func: impl Fn(B, A) -> B + 'a,
831 initial: B,
832 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
833 ) -> B
834 where
835 FnBrand: CloneFn + 'a, {
836 func(initial, fa.1)
837 }
838
839 #[document_signature]
843 #[document_type_parameters(
845 "The lifetime of the values.",
846 "The brand of the cloneable function to use.",
847 "The type of the elements in the structure.",
848 "The type of the monoid."
849 )]
850 #[document_parameters("The mapping function.", "The tuple to fold.")]
852 #[document_returns("`func(a)`.")]
854 #[document_examples]
856 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
872 func: impl Fn(A) -> M + 'a,
873 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
874 ) -> M
875 where
876 M: Monoid + 'a,
877 FnBrand: CloneFn + 'a, {
878 func(fa.1)
879 }
880 }
881
882 #[document_type_parameters("The type of the first value in the tuple.")]
883 impl<First: Clone + 'static> Traversable for Tuple2FirstAppliedBrand<First> {
884 #[document_signature]
888 #[document_type_parameters(
890 "The lifetime of the values.",
891 "The type of the elements in the traversable structure.",
892 "The type of the elements in the resulting traversable structure.",
893 "The applicative context."
894 )]
895 #[document_parameters(
897 "The function to apply to each element, returning a value in an applicative context.",
898 "The tuple to traverse."
899 )]
900 #[document_returns("The tuple wrapped in the applicative context.")]
902 #[document_examples]
903 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
919 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
920 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
921 ) -> 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>)>)
922 where
923 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
924 let (first, second) = ta;
925 F::map(move |b| (first.clone(), b), func(second))
926 }
927
928 #[document_signature]
932 #[document_type_parameters(
934 "The lifetime of the values.",
935 "The type of the elements in the traversable structure.",
936 "The applicative context."
937 )]
938 #[document_parameters("The tuple containing the applicative value.")]
940 #[document_returns("The tuple wrapped in the applicative context.")]
942 #[document_examples]
944 fn sequence<'a, A: 'a + Clone, F: Applicative>(
957 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>)>)
958 ) -> 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>)>)
959 where
960 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
961 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
962 let (first, second) = ta;
963 F::map(move |a| (first.clone(), a), second)
964 }
965 }
966
967 #[document_type_parameters("The type of the first value in the tuple.")]
970 impl<First: Clone + 'static> RefFunctor for Tuple2FirstAppliedBrand<First> {
971 #[document_signature]
973 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
974 #[document_parameters("The function.", "The tuple.")]
975 #[document_returns("A new tuple with the mapped second value.")]
976 #[document_examples]
977 fn ref_map<'a, A: 'a, B: 'a>(
989 func: impl Fn(&A) -> B + 'a,
990 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
991 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
992 (fa.0.clone(), func(&fa.1))
993 }
994 }
995
996 #[document_type_parameters("The type of the first value in the tuple.")]
997 impl<First: Clone + 'static> RefFoldable for Tuple2FirstAppliedBrand<First> {
998 #[document_signature]
1000 #[document_type_parameters(
1001 "The lifetime.",
1002 "The brand.",
1003 "The element type.",
1004 "The monoid type."
1005 )]
1006 #[document_parameters("The mapping function.", "The tuple.")]
1007 #[document_returns("The monoid value.")]
1008 #[document_examples]
1009 fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1022 func: impl Fn(&A) -> M + 'a,
1023 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1024 ) -> M
1025 where
1026 FnBrand: LiftFn + 'a,
1027 M: Monoid + 'a, {
1028 func(&fa.1)
1029 }
1030 }
1031
1032 #[document_type_parameters("The type of the first value in the tuple.")]
1033 impl<First: Clone + 'static> RefTraversable for Tuple2FirstAppliedBrand<First> {
1034 #[document_signature]
1036 #[document_type_parameters(
1037 "The lifetime.",
1038 "The brand.",
1039 "The input type.",
1040 "The output type.",
1041 "The applicative."
1042 )]
1043 #[document_parameters("The function.", "The tuple.")]
1044 #[document_returns("The traversed result.")]
1045 #[document_examples]
1046 fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1060 func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1061 ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1062 ) -> 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>)>)
1063 where
1064 FnBrand: LiftFn + 'a,
1065 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1066 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1067 let first = ta.0.clone();
1068 F::map(move |b| (first.clone(), b), func(&ta.1))
1069 }
1070 }
1071
1072 #[document_type_parameters("The type of the first value in the tuple.")]
1073 impl<First: Clone + 'static> RefPointed for Tuple2FirstAppliedBrand<First>
1074 where
1075 First: Monoid,
1076 {
1077 #[document_signature]
1079 #[document_type_parameters("The lifetime.", "The value type.")]
1080 #[document_parameters("The reference to wrap.")]
1081 #[document_returns("A tuple containing `Monoid::empty()` and a clone of the value.")]
1082 #[document_examples]
1083 fn ref_pure<'a, A: Clone + 'a>(
1095 a: &A
1096 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1097 (Monoid::empty(), a.clone())
1098 }
1099 }
1100
1101 #[document_type_parameters("The type of the first value in the tuple.")]
1102 impl<First: Clone + 'static> RefLift for Tuple2FirstAppliedBrand<First>
1103 where
1104 First: Semigroup,
1105 {
1106 #[document_signature]
1108 #[document_type_parameters("The lifetime.", "First input.", "Second input.", "Output.")]
1109 #[document_parameters("The binary function.", "The first tuple.", "The second tuple.")]
1110 #[document_returns("A tuple with combined first values and the function result.")]
1111 #[document_examples]
1112 fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1127 func: impl Fn(&A, &B) -> C + 'a,
1128 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1129 fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1130 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1131 (Semigroup::append(fa.0.clone(), fb.0.clone()), func(&fa.1, &fb.1))
1132 }
1133 }
1134
1135 #[document_type_parameters("The type of the first value in the tuple.")]
1136 impl<First: Clone + 'static> RefSemiapplicative for Tuple2FirstAppliedBrand<First>
1137 where
1138 First: Semigroup,
1139 {
1140 #[document_signature]
1142 #[document_type_parameters(
1143 "The lifetime.",
1144 "The function brand.",
1145 "The input type.",
1146 "The output type."
1147 )]
1148 #[document_parameters(
1149 "The tuple containing the function.",
1150 "The tuple containing the value."
1151 )]
1152 #[document_returns("A tuple with combined first values and the function result.")]
1153 #[document_examples]
1154 fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1169 ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1170 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1171 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1172 (Semigroup::append(ff.0.clone(), fa.0.clone()), (*ff.1)(&fa.1))
1173 }
1174 }
1175
1176 #[document_type_parameters("The type of the first value in the tuple.")]
1177 impl<First: Clone + 'static> RefSemimonad for Tuple2FirstAppliedBrand<First>
1178 where
1179 First: Semigroup,
1180 {
1181 #[document_signature]
1183 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1184 #[document_parameters("The input tuple.", "The function to apply by reference.")]
1185 #[document_returns("A tuple with combined first values.")]
1186 #[document_examples]
1187 fn ref_bind<'a, A: 'a, B: 'a>(
1201 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1202 f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1203 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1204 let (next_first, next_second) = f(&fa.1);
1205 (Semigroup::append(fa.0.clone(), next_first), next_second)
1206 }
1207 }
1208
1209 impl_kind! {
1212 #[multi_brand]
1213 impl<Second: 'static> for Tuple2SecondAppliedBrand<Second> {
1214 type Of<'a, A: 'a>: 'a = (A, Second);
1215 }
1216 }
1217
1218 #[document_type_parameters("The type of the second value in the tuple.")]
1219 impl<Second: 'static> Functor for Tuple2SecondAppliedBrand<Second> {
1220 #[document_signature]
1224 #[document_type_parameters(
1226 "The lifetime of the values.",
1227 "The type of the first value.",
1228 "The type of the result of applying the function."
1229 )]
1230 #[document_parameters(
1232 "The function to apply to the first value.",
1233 "The tuple to map over."
1234 )]
1235 #[document_returns(
1237 "A new tuple containing the result of applying the function to the first value."
1238 )]
1239 #[document_examples]
1240 fn map<'a, A: 'a, B: 'a>(
1253 func: impl Fn(A) -> B + 'a,
1254 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1255 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1256 (func(fa.0), fa.1)
1257 }
1258 }
1259
1260 #[document_type_parameters("The type of the second value in the tuple.")]
1261 impl<Second: Clone + 'static> Lift for Tuple2SecondAppliedBrand<Second>
1262 where
1263 Second: Semigroup,
1264 {
1265 #[document_signature]
1269 #[document_type_parameters(
1271 "The lifetime of the values.",
1272 "The type of the first first value.",
1273 "The type of the second first value.",
1274 "The type of the result first value."
1275 )]
1276 #[document_parameters(
1278 "The binary function to apply to the first values.",
1279 "The first tuple.",
1280 "The second tuple."
1281 )]
1282 #[document_returns(
1284 "A new tuple where the first values are combined using `f` and the second values are combined using `Semigroup::append`."
1285 )]
1286 #[document_examples]
1287 fn lift2<'a, A, B, C>(
1304 func: impl Fn(A, B) -> C + 'a,
1305 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1306 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1307 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
1308 where
1309 A: Clone + 'a,
1310 B: Clone + 'a,
1311 C: 'a, {
1312 (func(fa.0, fb.0), Semigroup::append(fa.1, fb.1))
1313 }
1314 }
1315
1316 #[document_type_parameters("The type of the second value in the tuple.")]
1317 impl<Second: Clone + 'static> Pointed for Tuple2SecondAppliedBrand<Second>
1318 where
1319 Second: Monoid,
1320 {
1321 #[document_signature]
1325 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
1327 #[document_parameters("The value to wrap.")]
1329 #[document_returns("A tuple containing `a` and the empty value of the second type.")]
1331 #[document_examples]
1333 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1343 (a, Monoid::empty())
1344 }
1345 }
1346
1347 #[document_type_parameters("The type of the second value in the tuple.")]
1348 impl<Second: Clone + Semigroup + 'static> ApplyFirst for Tuple2SecondAppliedBrand<Second> {}
1349 #[document_type_parameters("The type of the second value in the tuple.")]
1350 impl<Second: Clone + Semigroup + 'static> ApplySecond for Tuple2SecondAppliedBrand<Second> {}
1351
1352 #[document_type_parameters("The type of the second value in the tuple.")]
1353 impl<Second: Clone + 'static> Semiapplicative for Tuple2SecondAppliedBrand<Second>
1354 where
1355 Second: Semigroup,
1356 {
1357 #[document_signature]
1361 #[document_type_parameters(
1363 "The lifetime of the values.",
1364 "The brand of the cloneable function wrapper.",
1365 "The type of the input value.",
1366 "The type of the output value."
1367 )]
1368 #[document_parameters(
1370 "The tuple containing the function.",
1371 "The tuple containing the value."
1372 )]
1373 #[document_returns(
1375 "A new tuple where the function is applied to the first value and the second values are combined."
1376 )]
1377 #[document_examples]
1378 fn apply<'a, FnBrand: 'a + CloneFn, A: 'a + Clone, B: 'a>(
1396 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn>::Of<'a, A, B>>),
1397 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1398 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1399 (ff.0(fa.0), Semigroup::append(ff.1, fa.1))
1400 }
1401 }
1402
1403 #[document_type_parameters("The type of the second value in the tuple.")]
1404 impl<Second: Clone + 'static> Semimonad for Tuple2SecondAppliedBrand<Second>
1405 where
1406 Second: Semigroup,
1407 {
1408 #[document_signature]
1412 #[document_type_parameters(
1414 "The lifetime of the values.",
1415 "The type of the result of the first computation.",
1416 "The type of the result of the second computation."
1417 )]
1418 #[document_parameters("The first tuple.", "The function to apply to the first value.")]
1420 #[document_returns("A new tuple where the second values are combined.")]
1422 #[document_examples]
1424 fn bind<'a, A: 'a, B: 'a>(
1440 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1441 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1442 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1443 let (first, second) = ma;
1444 let (next_first, next_second) = func(first);
1445 (next_first, Semigroup::append(second, next_second))
1446 }
1447 }
1448
1449 #[document_type_parameters("The type of the second value in the tuple.")]
1450 impl<Second: Clone + 'static> MonadRec for Tuple2SecondAppliedBrand<Second>
1451 where
1452 Second: Monoid,
1453 {
1454 #[document_signature]
1460 #[document_type_parameters(
1462 "The lifetime of the computation.",
1463 "The type of the initial value and loop state.",
1464 "The type of the result."
1465 )]
1466 #[document_parameters("The step function.", "The initial value.")]
1468 #[document_returns(
1470 "A tuple with the result of the computation and the accumulated second value."
1471 )]
1472 #[document_examples]
1474 fn tail_rec_m<'a, A: 'a, B: 'a>(
1498 func: impl Fn(
1499 A,
1500 )
1501 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1502 + 'a,
1503 initial: A,
1504 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1505 let mut acc: Second = Monoid::empty();
1506 let mut current = initial;
1507 loop {
1508 let (step, second) = func(current);
1509 acc = Semigroup::append(acc, second);
1510 match step {
1511 ControlFlow::Continue(next) => current = next,
1512 ControlFlow::Break(b) => return (b, acc),
1513 }
1514 }
1515 }
1516 }
1517
1518 #[document_type_parameters("The type of the second value in the tuple.")]
1519 impl<Second: 'static> Foldable for Tuple2SecondAppliedBrand<Second> {
1520 #[document_signature]
1524 #[document_type_parameters(
1526 "The lifetime of the values.",
1527 "The brand of the cloneable function to use.",
1528 "The type of the elements in the structure.",
1529 "The type of the accumulator."
1530 )]
1531 #[document_parameters("The folding function.", "The initial value.", "The tuple to fold.")]
1533 #[document_returns("`func(a, initial)`.")]
1535 #[document_examples]
1537 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
1554 func: impl Fn(A, B) -> B + 'a,
1555 initial: B,
1556 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1557 ) -> B
1558 where
1559 FnBrand: CloneFn + 'a, {
1560 func(fa.0, initial)
1561 }
1562
1563 #[document_signature]
1567 #[document_type_parameters(
1569 "The lifetime of the values.",
1570 "The brand of the cloneable function to use.",
1571 "The type of the elements in the structure.",
1572 "The type of the accumulator."
1573 )]
1574 #[document_parameters("The folding function.", "The initial value.", "The tuple to fold.")]
1576 #[document_returns("`func(initial, a)`.")]
1578 #[document_examples]
1580 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
1597 func: impl Fn(B, A) -> B + 'a,
1598 initial: B,
1599 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1600 ) -> B
1601 where
1602 FnBrand: CloneFn + 'a, {
1603 func(initial, fa.0)
1604 }
1605
1606 #[document_signature]
1610 #[document_type_parameters(
1612 "The lifetime of the values.",
1613 "The brand of the cloneable function to use.",
1614 "The type of the elements in the structure.",
1615 "The type of the monoid."
1616 )]
1617 #[document_parameters("The mapping function.", "The tuple to fold.")]
1619 #[document_returns("`func(a)`.")]
1621 #[document_examples]
1623 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
1639 func: impl Fn(A) -> M + 'a,
1640 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1641 ) -> M
1642 where
1643 M: Monoid + 'a,
1644 FnBrand: CloneFn + 'a, {
1645 func(fa.0)
1646 }
1647 }
1648
1649 #[document_type_parameters("The type of the second value in the tuple.")]
1650 impl<Second: Clone + 'static> Traversable for Tuple2SecondAppliedBrand<Second> {
1651 #[document_signature]
1655 #[document_type_parameters(
1657 "The lifetime of the values.",
1658 "The type of the elements in the traversable structure.",
1659 "The type of the elements in the resulting traversable structure.",
1660 "The applicative context."
1661 )]
1662 #[document_parameters("The function to apply.", "The tuple to traverse.")]
1664 #[document_returns("The tuple wrapped in the applicative context.")]
1666 #[document_examples]
1668 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1684 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1685 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1686 ) -> 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>)>)
1687 where
1688 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1689 let (first, second) = ta;
1690 F::map(move |b| (b, second.clone()), func(first))
1691 }
1692
1693 #[document_signature]
1697 #[document_type_parameters(
1699 "The lifetime of the values.",
1700 "The type of the elements in the traversable structure.",
1701 "The applicative context."
1702 )]
1703 #[document_parameters("The tuple containing the applicative value.")]
1705 #[document_returns("The tuple wrapped in the applicative context.")]
1707 #[document_examples]
1709 fn sequence<'a, A: 'a + Clone, F: Applicative>(
1722 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>)>)
1723 ) -> 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>)>)
1724 where
1725 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
1726 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
1727 let (first, second) = ta;
1728 F::map(move |a| (a, second.clone()), first)
1729 }
1730 }
1731 #[document_type_parameters("The type of the second value in the tuple.")]
1734 impl<Second: Clone + 'static> RefFunctor for Tuple2SecondAppliedBrand<Second> {
1735 #[document_signature]
1737 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1738 #[document_parameters("The function.", "The tuple.")]
1739 #[document_returns("A new tuple with the mapped first value.")]
1740 #[document_examples]
1741 fn ref_map<'a, A: 'a, B: 'a>(
1753 func: impl Fn(&A) -> B + 'a,
1754 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1755 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1756 (func(&fa.0), fa.1.clone())
1757 }
1758 }
1759
1760 #[document_type_parameters("The type of the second value in the tuple.")]
1761 impl<Second: Clone + 'static> RefFoldable for Tuple2SecondAppliedBrand<Second> {
1762 #[document_signature]
1764 #[document_type_parameters(
1765 "The lifetime.",
1766 "The brand.",
1767 "The element type.",
1768 "The monoid type."
1769 )]
1770 #[document_parameters("The mapping function.", "The tuple.")]
1771 #[document_returns("The monoid value.")]
1772 #[document_examples]
1773 fn ref_fold_map<'a, FnBrand, A: 'a + Clone, M>(
1786 func: impl Fn(&A) -> M + 'a,
1787 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1788 ) -> M
1789 where
1790 FnBrand: LiftFn + 'a,
1791 M: Monoid + 'a, {
1792 func(&fa.0)
1793 }
1794 }
1795
1796 #[document_type_parameters("The type of the second value in the tuple.")]
1797 impl<Second: Clone + 'static> RefTraversable for Tuple2SecondAppliedBrand<Second> {
1798 #[document_signature]
1800 #[document_type_parameters(
1801 "The lifetime.",
1802 "The brand.",
1803 "The input type.",
1804 "The output type.",
1805 "The applicative."
1806 )]
1807 #[document_parameters("The function.", "The tuple.")]
1808 #[document_returns("The traversed result.")]
1809 #[document_examples]
1810 fn ref_traverse<'a, FnBrand, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
1824 func: impl Fn(&A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1825 ta: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1826 ) -> 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>)>)
1827 where
1828 FnBrand: LiftFn + 'a,
1829 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
1830 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
1831 let second = ta.1.clone();
1832 F::map(move |a| (a, second.clone()), func(&ta.0))
1833 }
1834 }
1835
1836 #[document_type_parameters("The type of the second value in the tuple.")]
1837 impl<Second: Clone + 'static> RefPointed for Tuple2SecondAppliedBrand<Second>
1838 where
1839 Second: Monoid,
1840 {
1841 #[document_signature]
1843 #[document_type_parameters("The lifetime.", "The value type.")]
1844 #[document_parameters("The reference to wrap.")]
1845 #[document_returns("A tuple containing a clone of the value and `Monoid::empty()`.")]
1846 #[document_examples]
1847 fn ref_pure<'a, A: Clone + 'a>(
1859 a: &A
1860 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1861 (a.clone(), Monoid::empty())
1862 }
1863 }
1864
1865 #[document_type_parameters("The type of the second value in the tuple.")]
1866 impl<Second: Clone + 'static> RefLift for Tuple2SecondAppliedBrand<Second>
1867 where
1868 Second: Semigroup,
1869 {
1870 #[document_signature]
1872 #[document_type_parameters("The lifetime.", "First input.", "Second input.", "Output.")]
1873 #[document_parameters("The binary function.", "The first tuple.", "The second tuple.")]
1874 #[document_returns("A tuple with the function result and combined second values.")]
1875 #[document_examples]
1876 fn ref_lift2<'a, A: 'a, B: 'a, C: 'a>(
1891 func: impl Fn(&A, &B) -> C + 'a,
1892 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1893 fb: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1894 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>) {
1895 (func(&fa.0, &fb.0), Semigroup::append(fa.1.clone(), fb.1.clone()))
1896 }
1897 }
1898
1899 #[document_type_parameters("The type of the second value in the tuple.")]
1900 impl<Second: Clone + 'static> RefSemiapplicative for Tuple2SecondAppliedBrand<Second>
1901 where
1902 Second: Semigroup,
1903 {
1904 #[document_signature]
1906 #[document_type_parameters(
1907 "The lifetime.",
1908 "The function brand.",
1909 "The input type.",
1910 "The output type."
1911 )]
1912 #[document_parameters(
1913 "The tuple containing the function.",
1914 "The tuple containing the value."
1915 )]
1916 #[document_returns("A tuple with the function result and combined second values.")]
1917 #[document_examples]
1918 fn ref_apply<'a, FnBrand: 'a + CloneFn<Ref>, A: 'a, B: 'a>(
1933 ff: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneFn<Ref>>::Of<'a, A, B>>),
1934 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1935 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1936 ((*ff.0)(&fa.0), Semigroup::append(ff.1.clone(), fa.1.clone()))
1937 }
1938 }
1939
1940 #[document_type_parameters("The type of the second value in the tuple.")]
1941 impl<Second: Clone + 'static> RefSemimonad for Tuple2SecondAppliedBrand<Second>
1942 where
1943 Second: Semigroup,
1944 {
1945 #[document_signature]
1947 #[document_type_parameters("The lifetime.", "The input type.", "The output type.")]
1948 #[document_parameters("The input tuple.", "The function to apply by reference.")]
1949 #[document_returns("A tuple with combined second values.")]
1950 #[document_examples]
1951 fn ref_bind<'a, A: 'a, B: 'a>(
1965 fa: &Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1966 f: impl Fn(&A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
1967 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1968 let (next_first, next_second) = f(&fa.0);
1969 (next_first, Semigroup::append(fa.1.clone(), next_second))
1970 }
1971 }
1972}
1973
1974#[cfg(test)]
1975mod tests {
1976
1977 use {
1978 crate::{
1979 brands::*,
1980 classes::{
1981 semiapplicative::apply as explicit_apply,
1982 *,
1983 },
1984 functions::*,
1985 },
1986 core::ops::ControlFlow,
1987 quickcheck_macros::quickcheck,
1988 };
1989
1990 #[test]
1994 fn test_bimap() {
1995 let x = (1, 5);
1996 assert_eq!(
1997 explicit::bimap::<Tuple2Brand, _, _, _, _, _, _>((|a| a + 1, |b| b * 2), x),
1998 (2, 10)
1999 );
2000 }
2001
2002 #[quickcheck]
2006 fn bifunctor_identity(
2007 first: String,
2008 second: i32,
2009 ) -> bool {
2010 let x = (first, second);
2011 explicit::bimap::<Tuple2Brand, _, _, _, _, _, _>((identity, identity), x.clone()) == x
2012 }
2013
2014 #[quickcheck]
2016 fn bifunctor_composition(
2017 first: i32,
2018 second: i32,
2019 ) -> bool {
2020 let x = (first, second);
2021 let f = |x: i32| x.wrapping_add(1);
2022 let g = |x: i32| x.wrapping_mul(2);
2023 let h = |x: i32| x.wrapping_sub(1);
2024 let i = |x: i32| if x == 0 { 0 } else { x.wrapping_div(2) };
2025
2026 explicit::bimap::<Tuple2Brand, _, _, _, _, _, _>((compose(f, g), compose(h, i)), x)
2027 == explicit::bimap::<Tuple2Brand, _, _, _, _, _, _>(
2028 (f, h),
2029 explicit::bimap::<Tuple2Brand, _, _, _, _, _, _>((g, i), x),
2030 )
2031 }
2032
2033 #[quickcheck]
2037 fn functor_identity(
2038 first: String,
2039 second: i32,
2040 ) -> bool {
2041 let x = (first, second);
2042 explicit::map::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(identity, x.clone()) == x
2043 }
2044
2045 #[quickcheck]
2047 fn functor_composition(
2048 first: String,
2049 second: i32,
2050 ) -> bool {
2051 let x = (first, second);
2052 let f = |x: i32| x.wrapping_add(1);
2053 let g = |x: i32| x.wrapping_mul(2);
2054 explicit::map::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(compose(f, g), x.clone())
2055 == explicit::map::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(
2056 f,
2057 explicit::map::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(g, x),
2058 )
2059 }
2060
2061 #[quickcheck]
2065 fn applicative_identity(
2066 first: String,
2067 second: i32,
2068 ) -> bool {
2069 let v = (first, second);
2070 explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(
2071 pure::<Tuple2FirstAppliedBrand<String>, _>(<RcFnBrand as LiftFn>::new(identity)),
2072 v.clone(),
2073 ) == v
2074 }
2075
2076 #[quickcheck]
2078 fn applicative_homomorphism(x: i32) -> bool {
2079 let f = |x: i32| x.wrapping_mul(2);
2080 explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(
2081 pure::<Tuple2FirstAppliedBrand<String>, _>(<RcFnBrand as LiftFn>::new(f)),
2082 pure::<Tuple2FirstAppliedBrand<String>, _>(x),
2083 ) == pure::<Tuple2FirstAppliedBrand<String>, _>(f(x))
2084 }
2085
2086 #[quickcheck]
2088 fn applicative_composition(
2089 w_first: String,
2090 w_second: i32,
2091 u_seed: i32,
2092 v_seed: i32,
2093 ) -> bool {
2094 let w = (w_first, w_second);
2095
2096 let u_fn = <RcFnBrand as LiftFn>::new(move |x: i32| x.wrapping_add(u_seed));
2097 let u = pure::<Tuple2FirstAppliedBrand<String>, _>(u_fn);
2098
2099 let v_fn = <RcFnBrand as LiftFn>::new(move |x: i32| x.wrapping_mul(v_seed));
2100 let v = pure::<Tuple2FirstAppliedBrand<String>, _>(v_fn);
2101
2102 let vw = explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(
2104 v.clone(),
2105 w.clone(),
2106 );
2107 let rhs = explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(u.clone(), vw);
2108
2109 let compose_fn = <RcFnBrand as LiftFn>::new(|f: std::rc::Rc<dyn Fn(i32) -> i32>| {
2111 let f = f.clone();
2112 <RcFnBrand as LiftFn>::new(move |g: std::rc::Rc<dyn Fn(i32) -> i32>| {
2113 let f = f.clone();
2114 let g = g.clone();
2115 <RcFnBrand as LiftFn>::new(move |x| f(g(x)))
2116 })
2117 });
2118
2119 let pure_compose = pure::<Tuple2FirstAppliedBrand<String>, _>(compose_fn);
2120 let u_applied =
2121 explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(pure_compose, u);
2122 let uv = explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(u_applied, v);
2123 let lhs = explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(uv, w);
2124
2125 lhs == rhs
2126 }
2127
2128 #[quickcheck]
2130 fn applicative_interchange(
2131 y: i32,
2132 u_seed: i32,
2133 ) -> bool {
2134 let f = move |x: i32| x.wrapping_mul(u_seed);
2136 let u = pure::<Tuple2FirstAppliedBrand<String>, _>(<RcFnBrand as LiftFn>::new(f));
2137
2138 let lhs = explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(
2139 u.clone(),
2140 pure::<Tuple2FirstAppliedBrand<String>, _>(y),
2141 );
2142
2143 let rhs_fn = <RcFnBrand as LiftFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
2144 let rhs = explicit_apply::<RcFnBrand, Tuple2FirstAppliedBrand<String>, _, _>(
2145 pure::<Tuple2FirstAppliedBrand<String>, _>(rhs_fn),
2146 u,
2147 );
2148
2149 lhs == rhs
2150 }
2151
2152 #[quickcheck]
2156 fn monad_left_identity(a: i32) -> bool {
2157 let f = |x: i32| ("f".to_string(), x.wrapping_mul(2));
2158 explicit::bind::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(
2159 pure::<Tuple2FirstAppliedBrand<String>, _>(a),
2160 f,
2161 ) == f(a)
2162 }
2163
2164 #[quickcheck]
2166 fn monad_right_identity(
2167 first: String,
2168 second: i32,
2169 ) -> bool {
2170 let m = (first, second);
2171 explicit::bind::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(
2172 m.clone(),
2173 pure::<Tuple2FirstAppliedBrand<String>, _>,
2174 ) == m
2175 }
2176
2177 #[quickcheck]
2179 fn monad_associativity(
2180 first: String,
2181 second: i32,
2182 ) -> bool {
2183 let m = (first, second);
2184 let f = |x: i32| ("f".to_string(), x.wrapping_mul(2));
2185 let g = |x: i32| ("g".to_string(), x.wrapping_add(1));
2186 explicit::bind::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(
2187 explicit::bind::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(m.clone(), f),
2188 g,
2189 ) == explicit::bind::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(m, |x| {
2190 explicit::bind::<Tuple2FirstAppliedBrand<String>, _, _, _, _>(f(x), g)
2191 })
2192 }
2193
2194 #[quickcheck]
2199 fn monad_rec_first_identity(x: i32) -> bool {
2200 tail_rec_m::<Tuple2FirstAppliedBrand<String>, _, _>(
2201 |a| (String::new(), ControlFlow::Break(a)),
2202 x,
2203 ) == pure::<Tuple2FirstAppliedBrand<String>, _>(x)
2204 }
2205
2206 #[test]
2208 fn monad_rec_first_accumulation() {
2209 let result = tail_rec_m::<Tuple2FirstAppliedBrand<String>, _, _>(
2210 |n: i32| {
2211 if n < 3 {
2212 (format!("{n},"), ControlFlow::Continue(n + 1))
2213 } else {
2214 (format!("{n}"), ControlFlow::Break(n))
2215 }
2216 },
2217 0,
2218 );
2219 assert_eq!(result, ("0,1,2,3".to_string(), 3));
2220 }
2221
2222 #[test]
2224 fn monad_rec_first_stack_safety() {
2225 let iterations: i64 = 200_000;
2226 let result = tail_rec_m::<Tuple2FirstAppliedBrand<String>, _, _>(
2227 |acc| {
2228 if acc < iterations {
2229 (String::new(), ControlFlow::Continue(acc + 1))
2230 } else {
2231 (String::new(), ControlFlow::Break(acc))
2232 }
2233 },
2234 0i64,
2235 );
2236 assert_eq!(result, (String::new(), iterations));
2237 }
2238
2239 #[quickcheck]
2244 fn monad_rec_second_identity(x: i32) -> bool {
2245 tail_rec_m::<Tuple2SecondAppliedBrand<String>, _, _>(
2246 |a| (ControlFlow::Break(a), String::new()),
2247 x,
2248 ) == pure::<Tuple2SecondAppliedBrand<String>, _>(x)
2249 }
2250
2251 #[test]
2253 fn monad_rec_second_accumulation() {
2254 let result = tail_rec_m::<Tuple2SecondAppliedBrand<String>, _, _>(
2255 |n: i32| {
2256 if n < 3 {
2257 (ControlFlow::Continue(n + 1), format!("{n},"))
2258 } else {
2259 (ControlFlow::Break(n), format!("{n}"))
2260 }
2261 },
2262 0,
2263 );
2264 assert_eq!(result, (3, "0,1,2,3".to_string()));
2265 }
2266
2267 #[test]
2269 fn monad_rec_second_stack_safety() {
2270 let iterations: i64 = 200_000;
2271 let result = tail_rec_m::<Tuple2SecondAppliedBrand<String>, _, _>(
2272 |acc| {
2273 if acc < iterations {
2274 (ControlFlow::Continue(acc + 1), String::new())
2275 } else {
2276 (ControlFlow::Break(acc), String::new())
2277 }
2278 },
2279 0i64,
2280 );
2281 assert_eq!(result, (iterations, String::new()));
2282 }
2283}