1#[fp_macros::document_module]
27
28pub(crate) mod inner {
29 use {
30 crate::{
31 classes::{
32 Foldable,
33 LiftFn,
34 Monoid,
35 RefFoldable,
36 },
37 dispatch::{
38 Ref,
39 Val,
40 },
41 kinds::*,
42 },
43 fp_macros::*,
44 };
45
46 #[document_type_parameters(
54 "The lifetime of the values.",
55 "The brand of the cloneable function to use.",
56 "The brand of the foldable structure.",
57 "The type of the elements.",
58 "The type of the accumulator.",
59 "The container type (owned or borrowed), inferred from the argument.",
60 "Dispatch marker type, inferred automatically."
61 )]
62 #[document_parameters("The closure implementing this dispatch.")]
63 pub trait FoldRightDispatch<'a, FnBrand, Brand: Kind_cdc7cd43dac7585f, A: 'a, B: 'a, FA, Marker>
64 {
65 #[document_signature]
67 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
68 #[document_returns("The final accumulator value.")]
69 #[document_examples]
70 fn dispatch(
80 self,
81 initial: B,
82 fa: FA,
83 ) -> B;
84 }
85
86 #[document_type_parameters(
88 "The lifetime.",
89 "The cloneable function brand.",
90 "The foldable brand.",
91 "The element type.",
92 "The accumulator type.",
93 "The closure type."
94 )]
95 #[document_parameters("The closure.")]
96 impl<'a, FnBrand, Brand, A, B, F>
97 FoldRightDispatch<
98 'a,
99 FnBrand,
100 Brand,
101 A,
102 B,
103 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
104 Val,
105 > for F
106 where
107 Brand: Foldable,
108 FnBrand: LiftFn + 'a,
109 A: 'a + Clone,
110 B: 'a,
111 F: Fn(A, B) -> B + 'a,
112 {
113 #[document_signature]
114 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
115 #[document_returns("The final accumulator value.")]
116 #[document_examples]
117 fn dispatch(
127 self,
128 initial: B,
129 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
130 ) -> B {
131 Brand::fold_right::<FnBrand, A, B>(self, initial, fa)
132 }
133 }
134
135 #[document_type_parameters(
139 "The lifetime.",
140 "The borrow lifetime.",
141 "The cloneable function brand.",
142 "The foldable brand.",
143 "The element type.",
144 "The accumulator type.",
145 "The closure type."
146 )]
147 #[document_parameters("The closure.")]
148 impl<'a, 'b, FnBrand, Brand, A, B, F>
149 FoldRightDispatch<
150 'a,
151 FnBrand,
152 Brand,
153 A,
154 B,
155 &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
156 Ref,
157 > for F
158 where
159 Brand: RefFoldable,
160 FnBrand: LiftFn + 'a,
161 A: 'a + Clone,
162 B: 'a,
163 F: Fn(&A, B) -> B + 'a,
164 {
165 #[document_signature]
166 #[document_parameters(
167 "The initial accumulator value.",
168 "A reference to the structure to fold."
169 )]
170 #[document_returns("The final accumulator value.")]
171 #[document_examples]
172 fn dispatch(
185 self,
186 initial: B,
187 fa: &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
188 ) -> B {
189 Brand::ref_fold_right::<FnBrand, A, B>(self, initial, fa)
190 }
191 }
192
193 #[document_type_parameters(
201 "The lifetime of the values.",
202 "The brand of the cloneable function to use.",
203 "The brand of the foldable structure.",
204 "The type of the elements.",
205 "The type of the accumulator.",
206 "The container type (owned or borrowed), inferred from the argument.",
207 "Dispatch marker type, inferred automatically."
208 )]
209 #[document_parameters("The closure implementing this dispatch.")]
210 pub trait FoldLeftDispatch<'a, FnBrand, Brand: Kind_cdc7cd43dac7585f, A: 'a, B: 'a, FA, Marker>
211 {
212 #[document_signature]
214 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
215 #[document_returns("The final accumulator value.")]
216 #[document_examples]
217 fn dispatch(
227 self,
228 initial: B,
229 fa: FA,
230 ) -> B;
231 }
232
233 #[document_type_parameters(
235 "The lifetime.",
236 "The cloneable function brand.",
237 "The foldable brand.",
238 "The element type.",
239 "The accumulator type.",
240 "The closure type."
241 )]
242 #[document_parameters("The closure.")]
243 impl<'a, FnBrand, Brand, A, B, F>
244 FoldLeftDispatch<
245 'a,
246 FnBrand,
247 Brand,
248 A,
249 B,
250 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
251 Val,
252 > for F
253 where
254 Brand: Foldable,
255 FnBrand: LiftFn + 'a,
256 A: 'a + Clone,
257 B: 'a,
258 F: Fn(B, A) -> B + 'a,
259 {
260 #[document_signature]
261 #[document_parameters("The initial accumulator value.", "The structure to fold.")]
262 #[document_returns("The final accumulator value.")]
263 #[document_examples]
264 fn dispatch(
274 self,
275 initial: B,
276 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
277 ) -> B {
278 Brand::fold_left::<FnBrand, A, B>(self, initial, fa)
279 }
280 }
281
282 #[document_type_parameters(
286 "The lifetime.",
287 "The borrow lifetime.",
288 "The cloneable function brand.",
289 "The foldable brand.",
290 "The element type.",
291 "The accumulator type.",
292 "The closure type."
293 )]
294 #[document_parameters("The closure.")]
295 impl<'a, 'b, FnBrand, Brand, A, B, F>
296 FoldLeftDispatch<
297 'a,
298 FnBrand,
299 Brand,
300 A,
301 B,
302 &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
303 Ref,
304 > for F
305 where
306 Brand: RefFoldable,
307 FnBrand: LiftFn + 'a,
308 A: 'a + Clone,
309 B: 'a,
310 F: Fn(B, &A) -> B + 'a,
311 {
312 #[document_signature]
313 #[document_parameters(
314 "The initial accumulator value.",
315 "A reference to the structure to fold."
316 )]
317 #[document_returns("The final accumulator value.")]
318 #[document_examples]
319 fn dispatch(
332 self,
333 initial: B,
334 fa: &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
335 ) -> B {
336 Brand::ref_fold_left::<FnBrand, A, B>(self, initial, fa)
337 }
338 }
339
340 #[document_type_parameters(
348 "The lifetime of the values.",
349 "The brand of the cloneable function to use.",
350 "The brand of the foldable structure.",
351 "The type of the elements.",
352 "The monoid type.",
353 "The container type (owned or borrowed), inferred from the argument.",
354 "Dispatch marker type, inferred automatically."
355 )]
356 #[document_parameters("The closure implementing this dispatch.")]
357 pub trait FoldMapDispatch<'a, FnBrand, Brand: Kind_cdc7cd43dac7585f, A: 'a, M, FA, Marker> {
358 #[document_signature]
360 #[document_parameters("The structure to fold.")]
361 #[document_returns("The combined monoid value.")]
362 #[document_examples]
363 fn dispatch(
373 self,
374 fa: FA,
375 ) -> M;
376 }
377
378 #[document_type_parameters(
380 "The lifetime.",
381 "The cloneable function brand.",
382 "The foldable brand.",
383 "The element type.",
384 "The monoid type.",
385 "The closure type."
386 )]
387 #[document_parameters("The closure.")]
388 impl<'a, FnBrand, Brand, A, M, F>
389 FoldMapDispatch<
390 'a,
391 FnBrand,
392 Brand,
393 A,
394 M,
395 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
396 Val,
397 > for F
398 where
399 Brand: Foldable,
400 FnBrand: LiftFn + 'a,
401 A: 'a + Clone,
402 M: Monoid + 'a,
403 F: Fn(A) -> M + 'a,
404 {
405 #[document_signature]
406 #[document_parameters("The structure to fold.")]
407 #[document_returns("The combined monoid value.")]
408 #[document_examples]
409 fn dispatch(
419 self,
420 fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
421 ) -> M {
422 Brand::fold_map::<FnBrand, A, M>(self, fa)
423 }
424 }
425
426 #[document_type_parameters(
430 "The lifetime.",
431 "The borrow lifetime.",
432 "The cloneable function brand.",
433 "The foldable brand.",
434 "The element type.",
435 "The monoid type.",
436 "The closure type."
437 )]
438 #[document_parameters("The closure.")]
439 impl<'a, 'b, FnBrand, Brand, A, M, F>
440 FoldMapDispatch<
441 'a,
442 FnBrand,
443 Brand,
444 A,
445 M,
446 &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
447 Ref,
448 > for F
449 where
450 Brand: RefFoldable,
451 FnBrand: LiftFn + 'a,
452 A: Clone + 'a,
453 M: Monoid + 'a,
454 F: Fn(&A) -> M + 'a,
455 {
456 #[document_signature]
457 #[document_parameters("A reference to the structure to fold.")]
458 #[document_returns("The combined monoid value.")]
459 #[document_examples]
460 fn dispatch(
473 self,
474 fa: &'b Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
475 ) -> M {
476 Brand::ref_fold_map::<FnBrand, A, M>(self, fa)
477 }
478 }
479
480 #[document_signature]
491 #[document_type_parameters(
493 "The lifetime of the values.",
494 "The brand of the cloneable function to use (must be specified explicitly).",
495 "The container type (owned or borrowed). Brand is inferred from this.",
496 "The type of the elements.",
497 "The type of the accumulator.",
498 "Dispatch marker type, inferred automatically."
499 )]
500 #[document_parameters(
502 "The folding function.",
503 "The initial accumulator value.",
504 "The structure to fold (owned for Val, borrowed for Ref)."
505 )]
506 #[document_returns("The final accumulator value.")]
508 #[document_examples]
509 pub fn fold_right<'a, FnBrand, FA, A: 'a + Clone, B: 'a, Marker>(
520 func: impl FoldRightDispatch<
521 'a,
522 FnBrand,
523 <FA as InferableBrand_cdc7cd43dac7585f>::Brand,
524 A,
525 B,
526 FA,
527 Marker,
528 >,
529 initial: B,
530 fa: FA,
531 ) -> B
532 where
533 FA: InferableBrand_cdc7cd43dac7585f, {
534 func.dispatch(initial, fa)
535 }
536
537 #[document_signature]
546 #[document_type_parameters(
548 "The lifetime of the values.",
549 "The brand of the cloneable function to use (must be specified explicitly).",
550 "The container type (owned or borrowed). Brand is inferred from this.",
551 "The type of the elements.",
552 "The type of the accumulator.",
553 "Dispatch marker type, inferred automatically."
554 )]
555 #[document_parameters(
557 "The folding function.",
558 "The initial accumulator value.",
559 "The structure to fold (owned for Val, borrowed for Ref)."
560 )]
561 #[document_returns("The final accumulator value.")]
563 #[document_examples]
564 pub fn fold_left<'a, FnBrand, FA, A: 'a + Clone, B: 'a, Marker>(
575 func: impl FoldLeftDispatch<
576 'a,
577 FnBrand,
578 <FA as InferableBrand_cdc7cd43dac7585f>::Brand,
579 A,
580 B,
581 FA,
582 Marker,
583 >,
584 initial: B,
585 fa: FA,
586 ) -> B
587 where
588 FA: InferableBrand_cdc7cd43dac7585f, {
589 func.dispatch(initial, fa)
590 }
591
592 #[document_signature]
601 #[document_type_parameters(
603 "The lifetime of the values.",
604 "The brand of the cloneable function to use (must be specified explicitly).",
605 "The container type (owned or borrowed). Brand is inferred from this.",
606 "The type of the elements.",
607 "The monoid type.",
608 "Dispatch marker type, inferred automatically."
609 )]
610 #[document_parameters(
612 "The mapping function.",
613 "The structure to fold (owned for Val, borrowed for Ref)."
614 )]
615 #[document_returns("The combined monoid value.")]
617 #[document_examples]
618 pub fn fold_map<'a, FnBrand, FA, A: 'a, M: Monoid + 'a, Marker>(
629 func: impl FoldMapDispatch<
630 'a,
631 FnBrand,
632 <FA as InferableBrand_cdc7cd43dac7585f>::Brand,
633 A,
634 M,
635 FA,
636 Marker,
637 >,
638 fa: FA,
639 ) -> M
640 where
641 FA: InferableBrand_cdc7cd43dac7585f, {
642 func.dispatch(fa)
643 }
644
645 pub mod explicit {
652 use super::*;
653
654 #[document_signature]
662 #[document_type_parameters(
663 "The lifetime of the values.",
664 "The brand of the cloneable function to use.",
665 "The brand of the foldable structure.",
666 "The type of the elements.",
667 "The type of the accumulator.",
668 "The container type (owned or borrowed), inferred from the argument.",
669 "Dispatch marker type, inferred automatically."
670 )]
671 #[document_parameters(
672 "The folding function.",
673 "The initial accumulator value.",
674 "The structure to fold (owned for Val, borrowed for Ref)."
675 )]
676 #[document_returns("The final accumulator value.")]
677 #[document_examples]
678 pub fn fold_right<
697 'a,
698 FnBrand,
699 Brand: Kind_cdc7cd43dac7585f,
700 A: 'a + Clone,
701 B: 'a,
702 FA,
703 Marker,
704 >(
705 func: impl FoldRightDispatch<'a, FnBrand, Brand, A, B, FA, Marker>,
706 initial: B,
707 fa: FA,
708 ) -> B {
709 func.dispatch(initial, fa)
710 }
711
712 #[document_signature]
720 #[document_type_parameters(
721 "The lifetime of the values.",
722 "The brand of the cloneable function to use.",
723 "The brand of the foldable structure.",
724 "The type of the elements.",
725 "The type of the accumulator.",
726 "The container type (owned or borrowed), inferred from the argument.",
727 "Dispatch marker type, inferred automatically."
728 )]
729 #[document_parameters(
730 "The folding function.",
731 "The initial accumulator value.",
732 "The structure to fold (owned for Val, borrowed for Ref)."
733 )]
734 #[document_returns("The final accumulator value.")]
735 #[document_examples]
736 pub fn fold_left<
755 'a,
756 FnBrand,
757 Brand: Kind_cdc7cd43dac7585f,
758 A: 'a + Clone,
759 B: 'a,
760 FA,
761 Marker,
762 >(
763 func: impl FoldLeftDispatch<'a, FnBrand, Brand, A, B, FA, Marker>,
764 initial: B,
765 fa: FA,
766 ) -> B {
767 func.dispatch(initial, fa)
768 }
769
770 #[document_signature]
778 #[document_type_parameters(
779 "The lifetime of the values.",
780 "The brand of the cloneable function to use.",
781 "The brand of the foldable structure.",
782 "The type of the elements.",
783 "The monoid type.",
784 "The container type (owned or borrowed), inferred from the argument.",
785 "Dispatch marker type, inferred automatically."
786 )]
787 #[document_parameters(
788 "The mapping function.",
789 "The structure to fold (owned for Val, borrowed for Ref)."
790 )]
791 #[document_returns("The combined monoid value.")]
792 #[document_examples]
793 pub fn fold_map<
812 'a,
813 FnBrand,
814 Brand: Kind_cdc7cd43dac7585f,
815 A: 'a,
816 M: Monoid + 'a,
817 FA,
818 Marker,
819 >(
820 func: impl FoldMapDispatch<'a, FnBrand, Brand, A, M, FA, Marker>,
821 fa: FA,
822 ) -> M {
823 func.dispatch(fa)
824 }
825 }
826}
827
828pub use inner::*;