1#[fp_macros::document_module]
48
49pub(crate) mod inner {
50 use {
51 crate::{
52 classes::{
53 FoldableWithIndex,
54 LiftFn,
55 Monoid,
56 RefFoldableWithIndex,
57 WithIndex,
58 },
59 dispatch::{
60 Ref,
61 Val,
62 },
63 kinds::*,
64 },
65 fp_macros::*,
66 };
67
68 #[document_type_parameters(
76 "The lifetime of the values.",
77 "The brand of the cloneable function to use.",
78 "The brand of the foldable structure.",
79 "The type of the elements.",
80 "The type of the accumulator.",
81 "The container type (owned or borrowed), inferred from the argument.",
82 "Dispatch marker type, inferred automatically."
83 )]
84 #[document_parameters("The closure implementing this dispatch.")]
85 pub trait FoldLeftWithIndexDispatch<
86 'a,
87 FnBrand,
88 Brand: Kind_cdc7cd43dac7585f + WithIndex,
89 A: 'a,
90 B: 'a,
91 FA,
92 Marker,
93 > {
94 #[document_signature]
96 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
97 #[document_returns("The final accumulator value.")]
98 #[document_examples]
99 fn dispatch(
113 self,
114 initial: B,
115 fa: FA,
116 ) -> B;
117 }
118
119 #[document_type_parameters(
121 "The lifetime.",
122 "The cloneable function brand.",
123 "The foldable brand.",
124 "The element type.",
125 "The accumulator type.",
126 "The closure type."
127 )]
128 #[document_parameters("The closure.")]
129 impl<'a, FnBrand, Brand, A, B, F>
130 FoldLeftWithIndexDispatch<
131 'a,
132 FnBrand,
133 Brand,
134 A,
135 B,
136 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
137 Val,
138 > for F
139 where
140 Brand: FoldableWithIndex,
141 FnBrand: LiftFn + 'a,
142 A: 'a + Clone,
143 B: 'a,
144 Brand::Index: 'a,
145 F: Fn(Brand::Index, B, A) -> B + 'a,
146 {
147 #[document_signature]
148 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
149 #[document_returns("The final accumulator value.")]
150 #[document_examples]
151 fn dispatch(
165 self,
166 initial: B,
167 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
168 ) -> B {
169 Brand::fold_left_with_index::<FnBrand, A, B>(self, initial, fa)
170 }
171 }
172
173 #[document_type_parameters(
177 "The lifetime.",
178 "The borrow lifetime.",
179 "The cloneable function brand.",
180 "The foldable brand.",
181 "The element type.",
182 "The accumulator type.",
183 "The closure type."
184 )]
185 #[document_parameters("The closure.")]
186 impl<'a, 'b, FnBrand, Brand, A, B, F>
187 FoldLeftWithIndexDispatch<
188 'a,
189 FnBrand,
190 Brand,
191 A,
192 B,
193 &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
194 Ref,
195 > for F
196 where
197 Brand: RefFoldableWithIndex,
198 FnBrand: LiftFn + 'a,
199 A: 'a + Clone,
200 B: 'a,
201 Brand::Index: 'a,
202 F: Fn(Brand::Index, B, &A) -> B + 'a,
203 {
204 #[document_signature]
205 #[document_parameters(
206 "The initial accumulator value.",
207 "A reference to the structure to fold."
208 )]
209 #[document_returns("The final accumulator value.")]
210 #[document_examples]
211 fn dispatch(
227 self,
228 initial: B,
229 fa: &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
230 ) -> B {
231 Brand::ref_fold_left_with_index::<FnBrand, A, B>(self, initial, fa)
232 }
233 }
234
235 #[document_type_parameters(
243 "The lifetime of the values.",
244 "The brand of the cloneable function to use.",
245 "The brand of the foldable structure.",
246 "The type of the elements.",
247 "The type of the accumulator.",
248 "The container type (owned or borrowed), inferred from the argument.",
249 "Dispatch marker type, inferred automatically."
250 )]
251 #[document_parameters("The closure implementing this dispatch.")]
252 pub trait FoldRightWithIndexDispatch<
253 'a,
254 FnBrand,
255 Brand: Kind_cdc7cd43dac7585f + WithIndex,
256 A: 'a,
257 B: 'a,
258 FA,
259 Marker,
260 > {
261 #[document_signature]
263 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
264 #[document_returns("The final accumulator value.")]
265 #[document_examples]
266 fn dispatch(
280 self,
281 initial: B,
282 fa: FA,
283 ) -> B;
284 }
285
286 #[document_type_parameters(
288 "The lifetime.",
289 "The cloneable function brand.",
290 "The foldable brand.",
291 "The element type.",
292 "The accumulator type.",
293 "The closure type."
294 )]
295 #[document_parameters("The closure.")]
296 impl<'a, FnBrand, Brand, A, B, F>
297 FoldRightWithIndexDispatch<
298 'a,
299 FnBrand,
300 Brand,
301 A,
302 B,
303 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
304 Val,
305 > for F
306 where
307 Brand: FoldableWithIndex,
308 FnBrand: LiftFn + 'a,
309 A: 'a + Clone,
310 B: 'a,
311 Brand::Index: 'a,
312 F: Fn(Brand::Index, A, B) -> B + 'a,
313 {
314 #[document_signature]
315 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
316 #[document_returns("The final accumulator value.")]
317 #[document_examples]
318 fn dispatch(
332 self,
333 initial: B,
334 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
335 ) -> B {
336 Brand::fold_right_with_index::<FnBrand, A, B>(self, initial, fa)
337 }
338 }
339
340 #[document_type_parameters(
344 "The lifetime.",
345 "The borrow lifetime.",
346 "The cloneable function brand.",
347 "The foldable brand.",
348 "The element type.",
349 "The accumulator type.",
350 "The closure type."
351 )]
352 #[document_parameters("The closure.")]
353 impl<'a, 'b, FnBrand, Brand, A, B, F>
354 FoldRightWithIndexDispatch<
355 'a,
356 FnBrand,
357 Brand,
358 A,
359 B,
360 &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
361 Ref,
362 > for F
363 where
364 Brand: RefFoldableWithIndex,
365 FnBrand: LiftFn + 'a,
366 A: 'a + Clone,
367 B: 'a,
368 Brand::Index: 'a,
369 F: Fn(Brand::Index, &A, B) -> B + 'a,
370 {
371 #[document_signature]
372 #[document_parameters(
373 "The initial accumulator value.",
374 "A reference to the structure to fold."
375 )]
376 #[document_returns("The final accumulator value.")]
377 #[document_examples]
378 fn dispatch(
394 self,
395 initial: B,
396 fa: &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
397 ) -> B {
398 Brand::ref_fold_right_with_index::<FnBrand, A, B>(self, initial, fa)
399 }
400 }
401
402 #[document_type_parameters(
410 "The lifetime of the values.",
411 "The brand of the cloneable function to use.",
412 "The brand of the foldable structure.",
413 "The type of the elements.",
414 "The monoid type.",
415 "The container type (owned or borrowed), inferred from the argument.",
416 "Dispatch marker type, inferred automatically."
417 )]
418 #[document_parameters("The closure implementing this dispatch.")]
419 pub trait FoldMapWithIndexDispatch<
420 'a,
421 FnBrand,
422 Brand: Kind_cdc7cd43dac7585f + WithIndex,
423 A: 'a,
424 M,
425 FA,
426 Marker,
427 > {
428 #[document_signature]
430 #[document_parameters("The structure to fold.")]
431 #[document_returns("The combined monoid value.")]
432 #[document_examples]
433 fn dispatch(
446 self,
447 fa: FA,
448 ) -> M;
449 }
450
451 #[document_type_parameters(
453 "The lifetime.",
454 "The cloneable function brand.",
455 "The foldable brand.",
456 "The element type.",
457 "The monoid type.",
458 "The closure type."
459 )]
460 #[document_parameters("The closure.")]
461 impl<'a, FnBrand, Brand, A, M, F>
462 FoldMapWithIndexDispatch<
463 'a,
464 FnBrand,
465 Brand,
466 A,
467 M,
468 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
469 Val,
470 > for F
471 where
472 Brand: FoldableWithIndex,
473 FnBrand: LiftFn + 'a,
474 A: 'a + Clone,
475 M: Monoid + 'a,
476 Brand::Index: 'a,
477 F: Fn(Brand::Index, A) -> M + 'a,
478 {
479 #[document_signature]
480 #[document_parameters("The structure to fold.")]
481 #[document_returns("The combined monoid value.")]
482 #[document_examples]
483 fn dispatch(
496 self,
497 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
498 ) -> M {
499 Brand::fold_map_with_index::<FnBrand, A, M>(self, fa)
500 }
501 }
502
503 #[document_type_parameters(
507 "The lifetime.",
508 "The borrow lifetime.",
509 "The cloneable function brand.",
510 "The foldable brand.",
511 "The element type.",
512 "The monoid type.",
513 "The closure type."
514 )]
515 #[document_parameters("The closure.")]
516 impl<'a, 'b, FnBrand, Brand, A, M, F>
517 FoldMapWithIndexDispatch<
518 'a,
519 FnBrand,
520 Brand,
521 A,
522 M,
523 &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
524 Ref,
525 > for F
526 where
527 Brand: RefFoldableWithIndex,
528 FnBrand: LiftFn + 'a,
529 A: Clone + 'a,
530 M: Monoid + 'a,
531 Brand::Index: 'a,
532 F: Fn(Brand::Index, &A) -> M + 'a,
533 {
534 #[document_signature]
535 #[document_parameters("A reference to the structure to fold.")]
536 #[document_returns("The combined monoid value.")]
537 #[document_examples]
538 fn dispatch(
553 self,
554 fa: &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
555 ) -> M {
556 Brand::ref_fold_map_with_index::<FnBrand, A, M>(self, fa)
557 }
558 }
559
560 #[document_signature]
572 #[document_type_parameters(
574 "The lifetime of the values.",
575 "The brand of the cloneable function to use (must be specified explicitly).",
576 "The container type (owned or borrowed). Brand is inferred from this.",
577 "The type of the elements.",
578 "The monoid type.",
579 "The brand, inferred via InferableBrand from FA and the closure's input type."
580 )]
581 #[document_parameters(
583 "The mapping function that receives an index and element.",
584 "The structure to fold (owned for Val, borrowed for Ref)."
585 )]
586 #[document_returns("The combined monoid value.")]
588 #[document_examples]
589 pub fn fold_map_with_index<'a, FnBrand, FA, A: 'a, M: Monoid + 'a, Brand>(
603 func: impl FoldMapWithIndexDispatch<
604 'a,
605 FnBrand,
606 Brand,
607 A,
608 M,
609 FA,
610 <FA as InferableBrand_cdc7cd43dac7585f<'a, Brand, A>>::Marker,
611 >,
612 fa: FA,
613 ) -> M
614 where
615 Brand: Kind_cdc7cd43dac7585f + WithIndex,
616 FA: InferableBrand_cdc7cd43dac7585f<'a, Brand, A>, {
617 func.dispatch(fa)
618 }
619
620 #[document_signature]
629 #[document_type_parameters(
631 "The lifetime of the values.",
632 "The brand of the cloneable function to use (must be specified explicitly).",
633 "The container type (owned or borrowed). Brand is inferred from this.",
634 "The type of the elements.",
635 "The type of the accumulator.",
636 "The brand, inferred via InferableBrand from FA and the closure's input type."
637 )]
638 #[document_parameters(
640 "The folding function that receives an index, element, and accumulator.",
641 "The initial accumulator value.",
642 "The structure to fold (owned for Val, borrowed for Ref)."
643 )]
644 #[document_returns("The final accumulator value.")]
646 #[document_examples]
647 pub fn fold_right_with_index<'a, FnBrand, FA, A: 'a + Clone, B: 'a, Brand>(
662 func: impl FoldRightWithIndexDispatch<
663 'a,
664 FnBrand,
665 Brand,
666 A,
667 B,
668 FA,
669 <FA as InferableBrand_cdc7cd43dac7585f<'a, Brand, A>>::Marker,
670 >,
671 initial: B,
672 fa: FA,
673 ) -> B
674 where
675 Brand: Kind_cdc7cd43dac7585f + WithIndex,
676 FA: InferableBrand_cdc7cd43dac7585f<'a, Brand, A>, {
677 func.dispatch(initial, fa)
678 }
679
680 #[document_signature]
689 #[document_type_parameters(
691 "The lifetime of the values.",
692 "The brand of the cloneable function to use (must be specified explicitly).",
693 "The container type (owned or borrowed). Brand is inferred from this.",
694 "The type of the elements.",
695 "The type of the accumulator.",
696 "The brand, inferred via InferableBrand from FA and the closure's input type."
697 )]
698 #[document_parameters(
700 "The folding function that receives an index, accumulator, and element.",
701 "The initial accumulator value.",
702 "The structure to fold (owned for Val, borrowed for Ref)."
703 )]
704 #[document_returns("The final accumulator value.")]
706 #[document_examples]
707 pub fn fold_left_with_index<'a, FnBrand, FA, A: 'a + Clone, B: 'a, Brand>(
722 func: impl FoldLeftWithIndexDispatch<
723 'a,
724 FnBrand,
725 Brand,
726 A,
727 B,
728 FA,
729 <FA as InferableBrand_cdc7cd43dac7585f<'a, Brand, A>>::Marker,
730 >,
731 initial: B,
732 fa: FA,
733 ) -> B
734 where
735 Brand: Kind_cdc7cd43dac7585f + WithIndex,
736 FA: InferableBrand_cdc7cd43dac7585f<'a, Brand, A>, {
737 func.dispatch(initial, fa)
738 }
739
740 pub mod explicit {
747 use super::*;
748
749 #[document_signature]
758 #[document_type_parameters(
759 "The lifetime of the values.",
760 "The brand of the cloneable function to use.",
761 "The brand of the foldable structure.",
762 "The type of the elements.",
763 "The type of the accumulator.",
764 "The container type (owned or borrowed), inferred from the argument.",
765 "Dispatch marker type, inferred automatically."
766 )]
767 #[document_parameters(
768 "The folding function that receives an index, accumulator, and element.",
769 "The initial accumulator value.",
770 "The structure to fold (owned for Val, borrowed for Ref)."
771 )]
772 #[document_returns("The final accumulator value.")]
773 #[document_examples]
774 pub fn fold_left_with_index<
789 'a,
790 FnBrand,
791 Brand: Kind_cdc7cd43dac7585f + WithIndex,
792 A: 'a + Clone,
793 B: 'a,
794 FA,
795 Marker,
796 >(
797 func: impl FoldLeftWithIndexDispatch<'a, FnBrand, Brand, A, B, FA, Marker>,
798 initial: B,
799 fa: FA,
800 ) -> B {
801 func.dispatch(initial, fa)
802 }
803
804 #[document_signature]
813 #[document_type_parameters(
814 "The lifetime of the values.",
815 "The brand of the cloneable function to use.",
816 "The brand of the foldable structure.",
817 "The type of the elements.",
818 "The type of the accumulator.",
819 "The container type (owned or borrowed), inferred from the argument.",
820 "Dispatch marker type, inferred automatically."
821 )]
822 #[document_parameters(
823 "The folding function that receives an index, element, and accumulator.",
824 "The initial accumulator value.",
825 "The structure to fold (owned for Val, borrowed for Ref)."
826 )]
827 #[document_returns("The final accumulator value.")]
828 #[document_examples]
829 pub fn fold_right_with_index<
844 'a,
845 FnBrand,
846 Brand: Kind_cdc7cd43dac7585f + WithIndex,
847 A: 'a + Clone,
848 B: 'a,
849 FA,
850 Marker,
851 >(
852 func: impl FoldRightWithIndexDispatch<'a, FnBrand, Brand, A, B, FA, Marker>,
853 initial: B,
854 fa: FA,
855 ) -> B {
856 func.dispatch(initial, fa)
857 }
858
859 #[document_signature]
868 #[document_type_parameters(
869 "The lifetime of the values.",
870 "The brand of the cloneable function to use.",
871 "The brand of the foldable structure.",
872 "The type of the elements.",
873 "The monoid type.",
874 "The container type (owned or borrowed), inferred from the argument.",
875 "Dispatch marker type, inferred automatically."
876 )]
877 #[document_parameters(
878 "The mapping function that receives an index and element.",
879 "The structure to fold (owned for Val, borrowed for Ref)."
880 )]
881 #[document_returns("The combined monoid value.")]
882 #[document_examples]
883 pub fn fold_map_with_index<
897 'a,
898 FnBrand,
899 Brand: Kind_cdc7cd43dac7585f + WithIndex,
900 A: 'a,
901 M: Monoid + 'a,
902 FA,
903 Marker,
904 >(
905 func: impl FoldMapWithIndexDispatch<'a, FnBrand, Brand, A, M, FA, Marker>,
906 fa: FA,
907 ) -> M {
908 func.dispatch(fa)
909 }
910 }
911}
912
913pub use inner::*;