1#[fp_macros::document_module]
6mod inner {
7 use {
8 crate::{
9 Apply,
10 brands::{
11 OptionBrand,
12 VecBrand,
13 },
14 classes::{
15 Alt,
16 Applicative,
17 ApplyFirst,
18 ApplySecond,
19 CloneableFn,
20 Compactable,
21 Extend,
22 Filterable,
23 Foldable,
24 Functor,
25 Lift,
26 MonadRec,
27 Monoid,
28 ParCompactable,
29 ParFilterable,
30 ParFoldable,
31 ParFunctor,
32 Plus,
33 Pointed,
34 Semiapplicative,
35 Semigroup,
36 Semimonad,
37 Traversable,
38 Witherable,
39 foldable_with_index::FoldableWithIndex,
40 functor_with_index::FunctorWithIndex,
41 par_foldable_with_index::ParFoldableWithIndex,
42 par_functor_with_index::ParFunctorWithIndex,
43 traversable_with_index::TraversableWithIndex,
44 with_index::WithIndex,
45 },
46 impl_kind,
47 kinds::*,
48 },
49 core::ops::ControlFlow,
50 fp_macros::*,
51 };
52
53 impl_kind! {
54 for VecBrand {
55 type Of<'a, A: 'a>: 'a = Vec<A>;
56 }
57 }
58
59 impl VecBrand {
60 #[document_signature]
64 #[document_type_parameters("The type of the elements in the vector.")]
66 #[document_parameters(
68 "A value to prepend to the vector.",
69 "A vector to prepend the value to."
70 )]
71 #[document_returns(
73 "A new vector consisting of the `head` element prepended to the `tail` vector."
74 )]
75 #[document_examples]
76 pub fn construct<A>(
90 head: A,
91 tail: Vec<A>,
92 ) -> Vec<A>
93 where
94 A: Clone, {
95 [vec![head], tail].concat()
96 }
97
98 #[document_signature]
102 #[document_type_parameters("The type of the elements in the vector.")]
104 #[document_parameters("The vector slice to deconstruct.")]
106 #[document_returns(
108 "An [`Option`] containing a tuple of the head element and the remaining tail vector, or [`None`] if the slice is empty."
109 )]
110 #[document_examples]
112 pub fn deconstruct<A>(slice: &[A]) -> Option<(A, Vec<A>)>
124 where
125 A: Clone, {
126 match slice {
127 [] => None,
128 [head, tail @ ..] => Some((head.clone(), tail.to_vec())),
129 }
130 }
131 }
132
133 impl Functor for VecBrand {
134 #[document_signature]
138 #[document_type_parameters(
140 "The lifetime of the elements.",
141 "The type of the elements in the vector.",
142 "The type of the elements in the resulting vector."
143 )]
144 #[document_parameters("The function to apply to each element.", "The vector to map over.")]
146 #[document_returns("A new vector containing the results of applying the function.")]
148 #[document_examples]
150 fn map<'a, A: 'a, B: 'a>(
160 func: impl Fn(A) -> B + 'a,
161 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
162 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
163 fa.into_iter().map(func).collect()
164 }
165 }
166
167 impl Lift for VecBrand {
168 #[document_signature]
172 #[document_type_parameters(
174 "The lifetime of the elements.",
175 "The type of the elements in the first vector.",
176 "The type of the elements in the second vector.",
177 "The type of the elements in the resulting vector."
178 )]
179 #[document_parameters(
181 "The binary function to apply.",
182 "The first vector.",
183 "The second vector."
184 )]
185 #[document_returns(
187 "A new vector containing the results of applying the function to all pairs of elements."
188 )]
189 #[document_examples]
190 fn lift2<'a, A, B, C>(
203 func: impl Fn(A, B) -> C + 'a,
204 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
205 fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
206 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, C>)
207 where
208 A: Clone + 'a,
209 B: Clone + 'a,
210 C: 'a, {
211 fa.iter().flat_map(|a| fb.iter().map(|b| func(a.clone(), b.clone()))).collect()
212 }
213 }
214
215 impl Pointed for VecBrand {
216 #[document_signature]
220 #[document_type_parameters("The lifetime of the value.", "The type of the value to wrap.")]
222 #[document_parameters("The value to wrap.")]
224 #[document_returns("A vector containing the single value.")]
226 #[document_examples]
228 fn pure<'a, A: 'a>(a: A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
238 vec![a]
239 }
240 }
241
242 impl ApplyFirst for VecBrand {}
243 impl ApplySecond for VecBrand {}
244
245 impl Semiapplicative for VecBrand {
246 #[document_signature]
250 #[document_type_parameters(
252 "The lifetime of the values.",
253 "The brand of the cloneable function wrapper.",
254 "The type of the input values.",
255 "The type of the output values."
256 )]
257 #[document_parameters(
259 "The vector containing the functions.",
260 "The vector containing the values."
261 )]
262 #[document_returns(
264 "A new vector containing the results of applying each function to each value."
265 )]
266 #[document_examples]
267 fn apply<'a, FnBrand: 'a + CloneableFn, A: 'a + Clone, B: 'a>(
282 ff: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, <FnBrand as CloneableFn>::Of<'a, A, B>>),
283 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
284 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
285 ff.iter().flat_map(|f| fa.iter().map(move |a| f(a.clone()))).collect()
286 }
287 }
288
289 impl Semimonad for VecBrand {
290 #[document_signature]
294 #[document_type_parameters(
296 "The lifetime of the elements.",
297 "The type of the elements in the input vector.",
298 "The type of the elements in the output vector."
299 )]
300 #[document_parameters(
302 "The first vector.",
303 "The function to apply to each element, returning a vector."
304 )]
305 #[document_returns("A new vector containing the flattened results.")]
307 #[document_examples]
308 fn bind<'a, A: 'a, B: 'a>(
318 ma: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
319 func: impl Fn(A) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
320 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
321 ma.into_iter().flat_map(func).collect()
322 }
323 }
324
325 impl Alt for VecBrand {
326 #[document_signature]
331 #[document_type_parameters("The lifetime of the elements.", "The type of the elements.")]
333 #[document_parameters("The first vector.", "The second vector.")]
335 #[document_returns("The concatenated vector.")]
337 #[document_examples]
338 fn alt<'a, A: 'a>(
351 fa1: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
352 fa2: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
353 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
354 let mut result = fa1;
355 result.extend(fa2);
356 result
357 }
358 }
359
360 impl Plus for VecBrand {
361 #[document_signature]
363 #[document_type_parameters("The lifetime of the elements.", "The type of the elements.")]
365 #[document_returns("An empty vector.")]
367 #[document_examples]
368 fn empty<'a, A: 'a>() -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
379 Vec::new()
380 }
381 }
382
383 impl Foldable for VecBrand {
384 #[document_signature]
388 #[document_type_parameters(
390 "The lifetime of the elements.",
391 "The brand of the cloneable function to use.",
392 "The type of the elements in the vector.",
393 "The type of the accumulator."
394 )]
395 #[document_parameters("The folding function.", "The initial value.", "The vector to fold.")]
397 #[document_returns("The final accumulator value.")]
399 #[document_examples]
401 fn fold_right<'a, FnBrand, A: 'a + Clone, B: 'a>(
411 func: impl Fn(A, B) -> B + 'a,
412 initial: B,
413 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
414 ) -> B
415 where
416 FnBrand: CloneableFn + 'a, {
417 fa.into_iter().rev().fold(initial, |acc, x| func(x, acc))
418 }
419
420 #[document_signature]
424 #[document_type_parameters(
426 "The lifetime of the elements.",
427 "The brand of the cloneable function to use.",
428 "The type of the elements in the vector.",
429 "The type of the accumulator."
430 )]
431 #[document_parameters(
433 "The function to apply to the accumulator and each element.",
434 "The initial value of the accumulator.",
435 "The vector to fold."
436 )]
437 #[document_returns("The final accumulator value.")]
439 #[document_examples]
440 fn fold_left<'a, FnBrand, A: 'a + Clone, B: 'a>(
450 func: impl Fn(B, A) -> B + 'a,
451 initial: B,
452 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
453 ) -> B
454 where
455 FnBrand: CloneableFn + 'a, {
456 fa.into_iter().fold(initial, func)
457 }
458
459 #[document_signature]
463 #[document_type_parameters(
465 "The lifetime of the elements.",
466 "The brand of the cloneable function to use.",
467 "The type of the elements in the vector.",
468 "The type of the monoid."
469 )]
470 #[document_parameters("The mapping function.", "The vector to fold.")]
472 #[document_returns("The combined monoid value.")]
474 #[document_examples]
476 fn fold_map<'a, FnBrand, A: 'a + Clone, M>(
489 func: impl Fn(A) -> M + 'a,
490 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
491 ) -> M
492 where
493 M: Monoid + 'a,
494 FnBrand: CloneableFn + 'a, {
495 fa.into_iter().map(func).fold(M::empty(), |acc, x| M::append(acc, x))
496 }
497 }
498
499 impl Traversable for VecBrand {
500 #[document_signature]
504 #[document_type_parameters(
506 "The lifetime of the elements.",
507 "The type of the elements in the traversable structure.",
508 "The type of the elements in the resulting traversable structure.",
509 "The applicative context."
510 )]
511 #[document_parameters(
513 "The function to apply to each element, returning a value in an applicative context.",
514 "The vector to traverse."
515 )]
516 #[document_returns("The vector wrapped in the applicative context.")]
518 #[document_examples]
519 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative>(
535 func: impl Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
536 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
537 ) -> 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>)>)
538 where
539 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
540 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone, {
541 let len = ta.len();
542 ta.into_iter().fold(F::pure(Vec::with_capacity(len)), |acc, x| {
543 F::lift2(
544 |mut v, b| {
545 v.push(b);
546 v
547 },
548 acc,
549 func(x),
550 )
551 })
552 }
553
554 #[document_signature]
558 #[document_type_parameters(
560 "The lifetime of the elements.",
561 "The type of the elements in the traversable structure.",
562 "The applicative context."
563 )]
564 #[document_parameters("The vector containing the applicative values.")]
566 #[document_returns("The vector wrapped in the applicative context.")]
568 #[document_examples]
570 fn sequence<'a, A: 'a + Clone, F: Applicative>(
583 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>)>)
584 ) -> 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>)>)
585 where
586 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
587 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone, {
588 let len = ta.len();
589 ta.into_iter().fold(F::pure(Vec::with_capacity(len)), |acc, x| {
590 F::lift2(
591 |mut v, a| {
592 v.push(a);
593 v
594 },
595 acc,
596 x,
597 )
598 })
599 }
600 }
601
602 impl WithIndex for VecBrand {
603 type Index = usize;
604 }
605
606 impl FunctorWithIndex for VecBrand {
607 #[document_signature]
609 #[document_type_parameters(
610 "The lifetime of the elements.",
611 "The type of the elements in the vector.",
612 "The type of the elements in the resulting vector."
613 )]
614 #[document_parameters(
615 "The function to apply to each element and its index.",
616 "The vector to map over."
617 )]
618 #[document_returns("A new vector containing the results of applying the function.")]
619 #[document_examples]
620 fn map_with_index<'a, A: 'a, B: 'a>(
634 f: impl Fn(usize, A) -> B + 'a,
635 fa: Vec<A>,
636 ) -> Vec<B> {
637 fa.into_iter().enumerate().map(|(i, a)| f(i, a)).collect()
638 }
639 }
640
641 impl FoldableWithIndex for VecBrand {
642 #[document_signature]
644 #[document_type_parameters(
645 "The lifetime of the elements.",
646 "The type of the elements in the vector.",
647 "The monoid type."
648 )]
649 #[document_parameters(
650 "The function to apply to each element and its index.",
651 "The vector to fold."
652 )]
653 #[document_returns("The combined monoid value.")]
654 #[document_examples]
655 fn fold_map_with_index<'a, A: 'a + Clone, R: Monoid>(
667 f: impl Fn(usize, A) -> R + 'a,
668 fa: Vec<A>,
669 ) -> R {
670 fa.into_iter()
671 .enumerate()
672 .map(|(i, a)| f(i, a))
673 .fold(R::empty(), |acc, x| R::append(acc, x))
674 }
675 }
676
677 impl TraversableWithIndex for VecBrand {
678 #[document_signature]
680 #[document_type_parameters(
681 "The lifetime of the elements.",
682 "The type of the elements in the vector.",
683 "The type of the elements in the resulting vector.",
684 "The applicative context."
685 )]
686 #[document_parameters(
687 "The function to apply to each element and its index, returning a value in an applicative context.",
688 "The vector to traverse."
689 )]
690 #[document_returns("The vector wrapped in the applicative context.")]
691 #[document_examples]
692 fn traverse_with_index<'a, A: 'a, B: 'a + Clone, M: Applicative>(
710 f: impl Fn(usize, A) -> M::Of<'a, B> + 'a,
711 ta: Vec<A>,
712 ) -> M::Of<'a, Vec<B>> {
713 let len = ta.len();
714 ta.into_iter().enumerate().fold(M::pure(Vec::with_capacity(len)), |acc, (i, x)| {
715 M::lift2(
716 |mut v, b| {
717 v.push(b);
718 v
719 },
720 acc,
721 f(i, x),
722 )
723 })
724 }
725 }
726
727 #[document_type_parameters("The type of the elements in the vector.")]
728 impl<A: Clone> Semigroup for Vec<A> {
729 #[document_signature]
733 #[document_parameters("The first vector.", "The second vector.")]
735 #[document_returns("The concatenated vector.")]
737 #[document_examples]
739 fn append(
746 a: Self,
747 b: Self,
748 ) -> Self {
749 [a, b].concat()
750 }
751 }
752
753 #[document_type_parameters("The type of the elements in the vector.")]
754 impl<A: Clone> Monoid for Vec<A> {
755 #[document_signature]
759 #[document_returns("An empty vector.")]
761 #[document_examples]
763 fn empty() -> Self {
770 Vec::new()
771 }
772 }
773
774 impl VecBrand {
775 #[document_signature]
780 #[document_type_parameters(
782 "The lifetime of the elements.",
783 "The input element type.",
784 "The output element type."
785 )]
786 #[document_parameters(
788 "The function to apply to each element. Must be `Send + Sync`.",
789 "The vector to map over."
790 )]
791 #[document_returns("A new vector containing the mapped elements.")]
793 #[document_examples]
795 pub fn par_map<'a, A: 'a + Send, B: 'a + Send>(
803 f: impl Fn(A) -> B + Send + Sync + 'a,
804 fa: Vec<A>,
805 ) -> Vec<B> {
806 #[cfg(feature = "rayon")]
807 {
808 use rayon::prelude::*;
809 fa.into_par_iter().map(f).collect()
810 }
811 #[cfg(not(feature = "rayon"))]
812 fa.into_iter().map(f).collect()
813 }
814
815 #[document_signature]
820 #[document_type_parameters("The lifetime of the elements.", "The element type.")]
822 #[document_parameters("The vector of options.")]
824 #[document_returns("A new vector containing the unwrapped `Some` values.")]
826 #[document_examples]
828 pub fn par_compact<'a, A: 'a + Send>(fa: Vec<Option<A>>) -> Vec<A> {
836 #[cfg(feature = "rayon")]
837 {
838 use rayon::prelude::*;
839 fa.into_par_iter().flatten().collect()
840 }
841 #[cfg(not(feature = "rayon"))]
842 fa.into_iter().flatten().collect()
843 }
844
845 #[document_signature]
850 #[document_type_parameters(
852 "The lifetime of the elements.",
853 "The error type.",
854 "The success type."
855 )]
856 #[document_parameters("The vector of results.")]
858 #[document_returns(
860 "A pair `(errs, oks)` where `errs` contains the `Err` values and `oks` the `Ok` values."
861 )]
862 #[document_examples]
864 pub fn par_separate<'a, E: 'a + Send, O: 'a + Send>(
874 fa: Vec<Result<O, E>>
875 ) -> (Vec<E>, Vec<O>) {
876 #[cfg(feature = "rayon")]
877 {
878 use rayon::{
879 iter::Either,
880 prelude::*,
881 };
882 fa.into_par_iter().partition_map(|r| match r {
883 Ok(o) => Either::Right(o),
884 Err(e) => Either::Left(e),
885 })
886 }
887 #[cfg(not(feature = "rayon"))]
888 {
889 let mut errs = Vec::new();
890 let mut oks = Vec::new();
891 for result in fa {
892 match result {
893 Ok(o) => oks.push(o),
894 Err(e) => errs.push(e),
895 }
896 }
897 (errs, oks)
898 }
899 }
900
901 #[document_signature]
906 #[document_type_parameters(
908 "The lifetime of the elements.",
909 "The input element type.",
910 "The output element type."
911 )]
912 #[document_parameters(
914 "The function to apply. Must be `Send + Sync`.",
915 "The vector to filter and map."
916 )]
917 #[document_returns("A new vector containing the `Some` results of applying `f`.")]
919 #[document_examples]
921 pub fn par_filter_map<'a, A: 'a + Send, B: 'a + Send>(
932 f: impl Fn(A) -> Option<B> + Send + Sync + 'a,
933 fa: Vec<A>,
934 ) -> Vec<B> {
935 #[cfg(feature = "rayon")]
936 {
937 use rayon::prelude::*;
938 fa.into_par_iter().filter_map(f).collect()
939 }
940 #[cfg(not(feature = "rayon"))]
941 fa.into_iter().filter_map(f).collect()
942 }
943
944 #[document_signature]
949 #[document_type_parameters("The lifetime of the elements.", "The element type.")]
951 #[document_parameters("The predicate. Must be `Send + Sync`.", "The vector to filter.")]
953 #[document_returns("A new vector containing only the elements satisfying `f`.")]
955 #[document_examples]
957 pub fn par_filter<'a, A: 'a + Send>(
965 f: impl Fn(&A) -> bool + Send + Sync + 'a,
966 fa: Vec<A>,
967 ) -> Vec<A> {
968 #[cfg(feature = "rayon")]
969 {
970 use rayon::prelude::*;
971 fa.into_par_iter().filter(|a| f(a)).collect()
972 }
973 #[cfg(not(feature = "rayon"))]
974 fa.into_iter().filter(|a| f(a)).collect()
975 }
976
977 #[document_signature]
982 #[document_type_parameters(
984 "The lifetime of the elements.",
985 "The element type.",
986 "The monoid type."
987 )]
988 #[document_parameters(
990 "The function mapping each element to a monoid value. Must be `Send + Sync`.",
991 "The vector to fold."
992 )]
993 #[document_returns("The combined monoid value.")]
995 #[document_examples]
997 pub fn par_fold_map<'a, A: 'a + Send, M: Monoid + Send + 'a>(
1005 f: impl Fn(A) -> M + Send + Sync + 'a,
1006 fa: Vec<A>,
1007 ) -> M {
1008 #[cfg(feature = "rayon")]
1009 {
1010 use rayon::prelude::*;
1011 fa.into_par_iter().map(f).reduce(M::empty, |acc, m| M::append(acc, m))
1012 }
1013 #[cfg(not(feature = "rayon"))]
1014 fa.into_iter().map(f).fold(M::empty(), |acc, m| M::append(acc, m))
1015 }
1016
1017 #[document_signature]
1022 #[document_type_parameters(
1024 "The lifetime of the elements.",
1025 "The input element type.",
1026 "The output element type."
1027 )]
1028 #[document_parameters(
1030 "The function to apply to each index and element. Must be `Send + Sync`.",
1031 "The vector to map over."
1032 )]
1033 #[document_returns("A new vector containing the mapped elements.")]
1035 #[document_examples]
1037 pub fn par_map_with_index<'a, A: 'a + Send, B: 'a + Send>(
1045 f: impl Fn(usize, A) -> B + Send + Sync + 'a,
1046 fa: Vec<A>,
1047 ) -> Vec<B> {
1048 #[cfg(feature = "rayon")]
1049 {
1050 use rayon::prelude::*;
1051 fa.into_par_iter().enumerate().map(|(i, a)| f(i, a)).collect()
1052 }
1053 #[cfg(not(feature = "rayon"))]
1054 fa.into_iter().enumerate().map(|(i, a)| f(i, a)).collect()
1055 }
1056
1057 #[document_signature]
1062 #[document_type_parameters(
1064 "The lifetime of the elements.",
1065 "The element type.",
1066 "The monoid type."
1067 )]
1068 #[document_parameters(
1070 "The function mapping each index and element to a monoid value. Must be `Send + Sync`.",
1071 "The vector to fold."
1072 )]
1073 #[document_returns("The combined monoid value.")]
1075 #[document_examples]
1077 pub fn par_fold_map_with_index<'a, A: 'a + Send, M: Monoid + Send + 'a>(
1086 f: impl Fn(usize, A) -> M + Send + Sync + 'a,
1087 fa: Vec<A>,
1088 ) -> M {
1089 #[cfg(feature = "rayon")]
1090 {
1091 use rayon::prelude::*;
1092 fa.into_par_iter()
1093 .enumerate()
1094 .map(|(i, a)| f(i, a))
1095 .reduce(M::empty, |acc, m| M::append(acc, m))
1096 }
1097 #[cfg(not(feature = "rayon"))]
1098 fa.into_iter()
1099 .enumerate()
1100 .map(|(i, a)| f(i, a))
1101 .fold(M::empty(), |acc, m| M::append(acc, m))
1102 }
1103 }
1104
1105 impl ParFunctor for VecBrand {
1106 #[document_signature]
1110 #[document_type_parameters(
1112 "The lifetime of the elements.",
1113 "The input element type.",
1114 "The output element type."
1115 )]
1116 #[document_parameters(
1118 "The function to apply to each element. Must be `Send + Sync`.",
1119 "The vector to map over."
1120 )]
1121 #[document_returns("A new vector containing the mapped elements.")]
1123 #[document_examples]
1125 fn par_map<'a, A: 'a + Send, B: 'a + Send>(
1136 f: impl Fn(A) -> B + Send + Sync + 'a,
1137 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1138 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1139 VecBrand::par_map(f, fa)
1140 }
1141 }
1142
1143 impl ParCompactable for VecBrand {
1144 #[document_signature]
1148 #[document_type_parameters("The lifetime of the elements.", "The element type.")]
1150 #[document_parameters("The vector of options.")]
1152 #[document_returns("A new vector containing the unwrapped `Some` values.")]
1154 #[document_examples]
1156 fn par_compact<'a, A: 'a + Send>(
1167 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
1168 'a,
1169 Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1170 >)
1171 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1172 VecBrand::par_compact(fa)
1173 }
1174
1175 #[document_signature]
1179 #[document_type_parameters(
1181 "The lifetime of the elements.",
1182 "The error type.",
1183 "The success type."
1184 )]
1185 #[document_parameters("The vector of results.")]
1187 #[document_returns(
1189 "A pair `(errs, oks)` where `errs` contains the `Err` values and `oks` the `Ok` values."
1190 )]
1191 #[document_examples]
1193 fn par_separate<'a, E: 'a + Send, O: 'a + Send>(
1206 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
1207 ) -> (
1208 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
1209 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
1210 ) {
1211 VecBrand::par_separate(fa)
1212 }
1213 }
1214
1215 impl ParFilterable for VecBrand {
1216 #[document_signature]
1221 #[document_type_parameters(
1223 "The lifetime of the elements.",
1224 "The input element type.",
1225 "The output element type."
1226 )]
1227 #[document_parameters(
1229 "The function to apply. Must be `Send + Sync`.",
1230 "The vector to filter and map."
1231 )]
1232 #[document_returns("A new vector containing the `Some` results of applying `f`.")]
1234 #[document_examples]
1236 fn par_filter_map<'a, A: 'a + Send, B: 'a + Send>(
1250 f: impl Fn(A) -> Option<B> + Send + Sync + 'a,
1251 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1252 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1253 VecBrand::par_filter_map(f, fa)
1254 }
1255
1256 #[document_signature]
1261 #[document_type_parameters("The lifetime of the elements.", "The element type.")]
1263 #[document_parameters("The predicate. Must be `Send + Sync`.", "The vector to filter.")]
1265 #[document_returns("A new vector containing only the elements satisfying `f`.")]
1267 #[document_examples]
1269 fn par_filter<'a, A: 'a + Send>(
1280 f: impl Fn(&A) -> bool + Send + Sync + 'a,
1281 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1282 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1283 VecBrand::par_filter(f, fa)
1284 }
1285 }
1286
1287 impl ParFoldable for VecBrand {
1288 #[document_signature]
1292 #[document_type_parameters(
1294 "The lifetime of the elements.",
1295 "The element type.",
1296 "The monoid type."
1297 )]
1298 #[document_parameters(
1300 "The function mapping each element to a monoid value. Must be `Send + Sync`.",
1301 "The vector to fold."
1302 )]
1303 #[document_returns("The combined monoid value.")]
1305 #[document_examples]
1307 fn par_fold_map<'a, A: 'a + Send, M: Monoid + Send + 'a>(
1318 f: impl Fn(A) -> M + Send + Sync + 'a,
1319 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1320 ) -> M {
1321 VecBrand::par_fold_map(f, fa)
1322 }
1323 }
1324
1325 impl ParFunctorWithIndex for VecBrand {
1326 #[document_signature]
1330 #[document_type_parameters(
1332 "The lifetime of the elements.",
1333 "The input element type.",
1334 "The output element type."
1335 )]
1336 #[document_parameters(
1338 "The function to apply to each index and element. Must be `Send + Sync`.",
1339 "The vector to map over."
1340 )]
1341 #[document_returns("A new vector containing the mapped elements.")]
1343 #[document_examples]
1345 fn par_map_with_index<'a, A: 'a + Send, B: 'a + Send>(
1356 f: impl Fn(usize, A) -> B + Send + Sync + 'a,
1357 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1358 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)
1359 where
1360 usize: Send + Sync + Copy + 'a, {
1361 VecBrand::par_map_with_index(f, fa)
1362 }
1363 }
1364
1365 impl ParFoldableWithIndex for VecBrand {
1366 #[document_signature]
1370 #[document_type_parameters(
1372 "The lifetime of the elements.",
1373 "The element type.",
1374 "The monoid type."
1375 )]
1376 #[document_parameters(
1378 "The function mapping each index and element to a monoid value. Must be `Send + Sync`.",
1379 "The vector to fold."
1380 )]
1381 #[document_returns("The combined monoid value.")]
1383 #[document_examples]
1385 fn par_fold_map_with_index<'a, A: 'a + Send, M: Monoid + Send + 'a>(
1397 f: impl Fn(usize, A) -> M + Send + Sync + 'a,
1398 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1399 ) -> M
1400 where
1401 usize: Send + Sync + Copy + 'a, {
1402 VecBrand::par_fold_map_with_index(f, fa)
1403 }
1404 }
1405
1406 impl Compactable for VecBrand {
1407 #[document_signature]
1411 #[document_type_parameters("The lifetime of the elements.", "The type of the elements.")]
1413 #[document_parameters("The vector of options.")]
1415 #[document_returns("The flattened vector.")]
1417 #[document_examples]
1419 fn compact<'a, A: 'a>(
1431 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
1432 'a,
1433 Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1434 >)
1435 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1436 fa.into_iter().flatten().collect()
1437 }
1438
1439 #[document_signature]
1443 #[document_type_parameters(
1445 "The lifetime of the elements.",
1446 "The type of the error value.",
1447 "The type of the success value."
1448 )]
1449 #[document_parameters("The vector of results.")]
1451 #[document_returns("A pair of vectors.")]
1453 #[document_examples]
1455 fn separate<'a, E: 'a, O: 'a>(
1468 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
1469 ) -> (
1470 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
1471 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
1472 ) {
1473 let mut oks = Vec::new();
1474 let mut errs = Vec::new();
1475 for result in fa {
1476 match result {
1477 Ok(o) => oks.push(o),
1478 Err(e) => errs.push(e),
1479 }
1480 }
1481 (errs, oks)
1482 }
1483 }
1484
1485 impl Filterable for VecBrand {
1486 #[document_signature]
1490 #[document_type_parameters(
1492 "The lifetime of the elements.",
1493 "The type of the input value.",
1494 "The type of the error value.",
1495 "The type of the success value."
1496 )]
1497 #[document_parameters("The function to apply.", "The vector to partition.")]
1499 #[document_returns("A pair of vectors.")]
1501 #[document_examples]
1503 fn partition_map<'a, A: 'a, E: 'a, O: 'a>(
1517 func: impl Fn(A) -> Result<O, E> + 'a,
1518 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1519 ) -> (
1520 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
1521 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
1522 ) {
1523 let mut oks = Vec::new();
1524 let mut errs = Vec::new();
1525 for a in fa {
1526 match func(a) {
1527 Ok(o) => oks.push(o),
1528 Err(e) => errs.push(e),
1529 }
1530 }
1531 (errs, oks)
1532 }
1533
1534 #[document_signature]
1538 #[document_type_parameters("The lifetime of the elements.", "The type of the elements.")]
1540 #[document_parameters("The predicate.", "The vector to partition.")]
1542 #[document_returns("A pair of vectors.")]
1544 #[document_examples]
1546 fn partition<'a, A: 'a + Clone>(
1559 func: impl Fn(A) -> bool + 'a,
1560 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1561 ) -> (
1562 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1563 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1564 ) {
1565 let (satisfied, not_satisfied): (Vec<A>, Vec<A>) =
1566 fa.into_iter().partition(|a| func(a.clone()));
1567 (not_satisfied, satisfied)
1568 }
1569
1570 #[document_signature]
1574 #[document_type_parameters(
1576 "The lifetime of the elements.",
1577 "The type of the input value.",
1578 "The type of the result of applying the function."
1579 )]
1580 #[document_parameters("The function to apply.", "The vector to filter and map.")]
1582 #[document_returns("The filtered and mapped vector.")]
1584 #[document_examples]
1586 fn filter_map<'a, A: 'a, B: 'a>(
1598 func: impl Fn(A) -> Option<B> + 'a,
1599 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1600 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1601 fa.into_iter().filter_map(func).collect()
1602 }
1603
1604 #[document_signature]
1608 #[document_type_parameters("The lifetime of the elements.", "The type of the elements.")]
1610 #[document_parameters("The predicate.", "The vector to filter.")]
1612 #[document_returns("The filtered vector.")]
1614 #[document_examples]
1616 fn filter<'a, A: 'a + Clone>(
1628 func: impl Fn(A) -> bool + 'a,
1629 fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1630 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
1631 fa.into_iter().filter(|a| func(a.clone())).collect()
1632 }
1633 }
1634
1635 impl Witherable for VecBrand {
1636 #[document_signature]
1640 #[document_type_parameters(
1642 "The lifetime of the elements.",
1643 "The applicative context.",
1644 "The type of the input value.",
1645 "The type of the error value.",
1646 "The type of the success value."
1647 )]
1648 #[document_parameters("The function to apply.", "The vector to partition.")]
1650 #[document_returns("The partitioned vector wrapped in the applicative context.")]
1652 #[document_examples]
1654 fn wilt<'a, M: Applicative, A: 'a + Clone, E: 'a + Clone, O: 'a + Clone>(
1669 func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
1670 + 'a,
1671 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1672 ) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
1673 'a,
1674 (
1675 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
1676 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
1677 ),
1678 >)
1679 where
1680 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
1681 Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone, {
1682 ta.into_iter().fold(M::pure((Vec::new(), Vec::new())), |acc, x| {
1683 M::lift2(
1684 |mut pair, res| {
1685 match res {
1686 Ok(o) => pair.1.push(o),
1687 Err(e) => pair.0.push(e),
1688 }
1689 pair
1690 },
1691 acc,
1692 func(x),
1693 )
1694 })
1695 }
1696
1697 #[document_signature]
1701 #[document_type_parameters(
1703 "The lifetime of the values.",
1704 "The applicative context.",
1705 "The type of the elements in the input structure.",
1706 "The type of the result of applying the function."
1707 )]
1708 #[document_parameters(
1710 "The function to apply to each element, returning an `Option` in an applicative context.",
1711 "The vector to filter and map."
1712 )]
1713 #[document_returns("The filtered and mapped vector wrapped in the applicative context.")]
1715 #[document_examples]
1716 fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone>(
1734 func: impl Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>) + 'a,
1735 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1736 ) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
1737 'a,
1738 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
1739 >)
1740 where
1741 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
1742 Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone, {
1743 ta.into_iter().fold(M::pure(Vec::new()), |acc, x| {
1744 M::lift2(
1745 |mut v, opt_b| {
1746 if let Some(b) = opt_b {
1747 v.push(b);
1748 }
1749 v
1750 },
1751 acc,
1752 func(x),
1753 )
1754 })
1755 }
1756 }
1757
1758 impl Extend for VecBrand {
1767 #[document_signature]
1773 #[document_type_parameters(
1775 "The lifetime of the values.",
1776 "The type of the elements in the vector.",
1777 "The result type of the extension function."
1778 )]
1779 #[document_parameters(
1781 "The function that consumes a suffix vector and produces a value.",
1782 "The vector to extend over."
1783 )]
1784 #[document_returns(
1786 "A new vector containing the results of applying the function to each suffix."
1787 )]
1788 #[document_examples]
1790 fn extend<'a, A: 'a + Clone, B: 'a>(
1801 f: impl Fn(Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)) -> B + 'a,
1802 wa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
1803 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1804 (0 .. wa.len()).map(|i| f(wa.get(i ..).unwrap_or_default().to_vec())).collect()
1805 }
1806 }
1807
1808 impl MonadRec for VecBrand {
1809 #[document_signature]
1816 #[document_type_parameters(
1818 "The lifetime of the computation.",
1819 "The type of the initial value and loop state.",
1820 "The type of the result."
1821 )]
1822 #[document_parameters("The step function.", "The initial value.")]
1824 #[document_returns("A vector of all completed results.")]
1826 #[document_examples]
1828 fn tail_rec_m<'a, A: 'a, B: 'a>(
1854 func: impl Fn(
1855 A,
1856 )
1857 -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, ControlFlow<B, A>>)
1858 + 'a,
1859 initial: A,
1860 ) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) {
1861 let mut done: Vec<B> = Vec::new();
1862 let mut pending: Vec<A> = vec![initial];
1863 while !pending.is_empty() {
1864 let mut next_pending: Vec<A> = Vec::new();
1865 for a in pending {
1866 for step in func(a) {
1867 match step {
1868 ControlFlow::Continue(next) => next_pending.push(next),
1869 ControlFlow::Break(b) => done.push(b),
1870 }
1871 }
1872 }
1873 pending = next_pending;
1874 }
1875 done
1876 }
1877 }
1878}
1879
1880#[cfg(test)]
1881mod tests {
1882
1883 use {
1884 crate::{
1885 brands::*,
1886 classes::CloneableFn,
1887 functions::*,
1888 },
1889 quickcheck_macros::quickcheck,
1890 };
1891
1892 #[quickcheck]
1896 fn functor_identity(x: Vec<i32>) -> bool {
1897 map::<VecBrand, _, _>(identity, x.clone()) == x
1898 }
1899
1900 #[quickcheck]
1902 fn functor_composition(x: Vec<i32>) -> bool {
1903 let f = |x: i32| x.wrapping_add(1);
1904 let g = |x: i32| x.wrapping_mul(2);
1905 map::<VecBrand, _, _>(compose(f, g), x.clone())
1906 == map::<VecBrand, _, _>(f, map::<VecBrand, _, _>(g, x))
1907 }
1908
1909 #[quickcheck]
1913 fn applicative_identity(v: Vec<i32>) -> bool {
1914 apply::<RcFnBrand, VecBrand, _, _>(
1915 pure::<VecBrand, _>(<RcFnBrand as CloneableFn>::new(identity)),
1916 v.clone(),
1917 ) == v
1918 }
1919
1920 #[quickcheck]
1922 fn applicative_homomorphism(x: i32) -> bool {
1923 let f = |x: i32| x.wrapping_mul(2);
1924 apply::<RcFnBrand, VecBrand, _, _>(
1925 pure::<VecBrand, _>(<RcFnBrand as CloneableFn>::new(f)),
1926 pure::<VecBrand, _>(x),
1927 ) == pure::<VecBrand, _>(f(x))
1928 }
1929
1930 #[quickcheck]
1932 fn applicative_composition(
1933 w: Vec<i32>,
1934 u_seeds: Vec<i32>,
1935 v_seeds: Vec<i32>,
1936 ) -> bool {
1937 let u_fns: Vec<_> = u_seeds
1938 .iter()
1939 .map(|&i| <RcFnBrand as CloneableFn>::new(move |x: i32| x.wrapping_add(i)))
1940 .collect();
1941 let v_fns: Vec<_> = v_seeds
1942 .iter()
1943 .map(|&i| <RcFnBrand as CloneableFn>::new(move |x: i32| x.wrapping_mul(i)))
1944 .collect();
1945
1946 let vw = apply::<RcFnBrand, VecBrand, _, _>(v_fns.clone(), w.clone());
1948 let rhs = apply::<RcFnBrand, VecBrand, _, _>(u_fns.clone(), vw);
1949
1950 let uv_fns: Vec<_> = u_fns
1954 .iter()
1955 .flat_map(|uf| {
1956 v_fns.iter().map(move |vf| {
1957 let uf = uf.clone();
1958 let vf = vf.clone();
1959 <RcFnBrand as CloneableFn>::new(move |x| uf(vf(x)))
1960 })
1961 })
1962 .collect();
1963
1964 let lhs = apply::<RcFnBrand, VecBrand, _, _>(uv_fns, w);
1965
1966 lhs == rhs
1967 }
1968
1969 #[quickcheck]
1971 fn applicative_interchange(y: i32) -> bool {
1972 let f = |x: i32| x.wrapping_mul(2);
1974 let u = vec![<RcFnBrand as CloneableFn>::new(f)];
1975
1976 let lhs = apply::<RcFnBrand, VecBrand, _, _>(u.clone(), pure::<VecBrand, _>(y));
1977
1978 let rhs_fn =
1979 <RcFnBrand as CloneableFn>::new(move |f: std::rc::Rc<dyn Fn(i32) -> i32>| f(y));
1980 let rhs = apply::<RcFnBrand, VecBrand, _, _>(pure::<VecBrand, _>(rhs_fn), u);
1981
1982 lhs == rhs
1983 }
1984
1985 #[quickcheck]
1989 fn semigroup_associativity(
1990 a: Vec<i32>,
1991 b: Vec<i32>,
1992 c: Vec<i32>,
1993 ) -> bool {
1994 append(a.clone(), append(b.clone(), c.clone())) == append(append(a, b), c)
1995 }
1996
1997 #[quickcheck]
2001 fn monoid_left_identity(a: Vec<i32>) -> bool {
2002 append(empty::<Vec<i32>>(), a.clone()) == a
2003 }
2004
2005 #[quickcheck]
2007 fn monoid_right_identity(a: Vec<i32>) -> bool {
2008 append(a.clone(), empty::<Vec<i32>>()) == a
2009 }
2010
2011 #[quickcheck]
2015 fn monad_left_identity(a: i32) -> bool {
2016 let f = |x: i32| vec![x.wrapping_mul(2)];
2017 bind::<VecBrand, _, _>(pure::<VecBrand, _>(a), f) == f(a)
2018 }
2019
2020 #[quickcheck]
2022 fn monad_right_identity(m: Vec<i32>) -> bool {
2023 bind::<VecBrand, _, _>(m.clone(), pure::<VecBrand, _>) == m
2024 }
2025
2026 #[quickcheck]
2028 fn monad_associativity(m: Vec<i32>) -> bool {
2029 let f = |x: i32| vec![x.wrapping_mul(2)];
2030 let g = |x: i32| vec![x.wrapping_add(1)];
2031 bind::<VecBrand, _, _>(bind::<VecBrand, _, _>(m.clone(), f), g)
2032 == bind::<VecBrand, _, _>(m, |x| bind::<VecBrand, _, _>(f(x), g))
2033 }
2034
2035 #[test]
2039 fn map_empty() {
2040 assert_eq!(map::<VecBrand, _, _>(|x: i32| x + 1, vec![] as Vec<i32>), vec![] as Vec<i32>);
2041 }
2042
2043 #[test]
2045 fn bind_empty() {
2046 assert_eq!(
2047 bind::<VecBrand, _, _>(vec![] as Vec<i32>, |x: i32| vec![x + 1]),
2048 vec![] as Vec<i32>
2049 );
2050 }
2051
2052 #[test]
2054 fn bind_returning_empty() {
2055 assert_eq!(
2056 bind::<VecBrand, _, _>(vec![1, 2, 3], |_| vec![] as Vec<i32>),
2057 vec![] as Vec<i32>
2058 );
2059 }
2060
2061 #[test]
2063 fn fold_right_empty() {
2064 assert_eq!(
2065 crate::classes::foldable::fold_right::<RcFnBrand, VecBrand, _, _>(
2066 |x: i32, acc| x + acc,
2067 0,
2068 vec![]
2069 ),
2070 0
2071 );
2072 }
2073
2074 #[test]
2076 fn fold_left_empty() {
2077 assert_eq!(
2078 crate::classes::foldable::fold_left::<RcFnBrand, VecBrand, _, _>(
2079 |acc, x: i32| acc + x,
2080 0,
2081 vec![]
2082 ),
2083 0
2084 );
2085 }
2086
2087 #[test]
2089 fn traverse_empty() {
2090 use crate::brands::OptionBrand;
2091 assert_eq!(
2092 crate::classes::traversable::traverse::<VecBrand, _, _, OptionBrand>(
2093 |x: i32| Some(x + 1),
2094 vec![]
2095 ),
2096 Some(vec![])
2097 );
2098 }
2099
2100 #[test]
2102 fn traverse_returning_empty() {
2103 use crate::brands::OptionBrand;
2104 assert_eq!(
2105 crate::classes::traversable::traverse::<VecBrand, _, _, OptionBrand>(
2106 |_: i32| None::<i32>,
2107 vec![1, 2, 3]
2108 ),
2109 None
2110 );
2111 }
2112
2113 #[test]
2115 fn construct_empty_tail() {
2116 assert_eq!(VecBrand::construct(1, vec![]), vec![1]);
2117 }
2118
2119 #[test]
2121 fn deconstruct_empty() {
2122 assert_eq!(VecBrand::deconstruct::<i32>(&[]), None);
2123 }
2124
2125 #[test]
2129 fn par_map_basic() {
2130 let v = vec![1, 2, 3];
2131 let result: Vec<i32> = par_map::<VecBrand, _, _>(|x: i32| x * 2, v);
2132 assert_eq!(result, vec![2, 4, 6]);
2133 }
2134
2135 #[test]
2137 fn par_filter_basic() {
2138 let v = vec![1, 2, 3, 4, 5];
2139 let result: Vec<i32> = par_filter::<VecBrand, _>(|x: &i32| x % 2 == 0, v);
2140 assert_eq!(result, vec![2, 4]);
2141 }
2142
2143 #[test]
2145 fn par_filter_map_basic() {
2146 let v = vec![1, 2, 3, 4, 5];
2147 let result: Vec<i32> = par_filter_map::<VecBrand, _, _>(
2148 |x: i32| if x % 2 == 0 { Some(x * 10) } else { None },
2149 v,
2150 );
2151 assert_eq!(result, vec![20, 40]);
2152 }
2153
2154 #[test]
2156 fn par_compact_basic() {
2157 let v = vec![Some(1), None, Some(3), None, Some(5)];
2158 let result: Vec<i32> = par_compact::<VecBrand, _>(v);
2159 assert_eq!(result, vec![1, 3, 5]);
2160 }
2161
2162 #[test]
2164 fn par_separate_basic() {
2165 let v: Vec<Result<i32, &str>> = vec![Ok(1), Err("a"), Ok(3), Err("b")];
2166 let (errs, oks): (Vec<&str>, Vec<i32>) = par_separate::<VecBrand, _, _>(v);
2167 assert_eq!(errs, vec!["a", "b"]);
2168 assert_eq!(oks, vec![1, 3]);
2169 }
2170
2171 #[test]
2173 fn par_map_with_index_basic() {
2174 let v = vec![10, 20, 30];
2175 let result: Vec<i32> = par_map_with_index::<VecBrand, _, _>(|i, x: i32| x + i as i32, v);
2176 assert_eq!(result, vec![10, 21, 32]);
2177 }
2178
2179 #[test]
2181 fn par_fold_map_empty() {
2182 let v: Vec<i32> = vec![];
2183 assert_eq!(par_fold_map::<VecBrand, _, _>(|x: i32| x.to_string(), v), "".to_string());
2184 }
2185
2186 #[test]
2188 fn par_fold_map_multiple() {
2189 let v = vec![1, 2, 3];
2190 assert_eq!(par_fold_map::<VecBrand, _, _>(|x: i32| x.to_string(), v), "123".to_string());
2191 }
2192
2193 #[test]
2195 fn par_fold_map_with_index_basic() {
2196 let v = vec![10, 20, 30];
2197 let result: String =
2198 par_fold_map_with_index::<VecBrand, _, _>(|i, x: i32| format!("{i}:{x}"), v);
2199 assert_eq!(result, "0:101:202:30");
2200 }
2201
2202 #[quickcheck]
2206 fn filterable_filter_map_identity(x: Vec<Option<i32>>) -> bool {
2207 filter_map::<VecBrand, _, _>(identity, x.clone()) == compact::<VecBrand, _>(x)
2208 }
2209
2210 #[quickcheck]
2212 fn filterable_filter_map_just(x: Vec<i32>) -> bool {
2213 filter_map::<VecBrand, _, _>(Some, x.clone()) == x
2214 }
2215
2216 #[quickcheck]
2218 fn filterable_filter_map_composition(x: Vec<i32>) -> bool {
2219 let r = |i: i32| if i % 2 == 0 { Some(i) } else { None };
2220 let l = |i: i32| if i > 5 { Some(i) } else { None };
2221 let composed = |i| bind::<OptionBrand, _, _>(r(i), l);
2222
2223 filter_map::<VecBrand, _, _>(composed, x.clone())
2224 == filter_map::<VecBrand, _, _>(l, filter_map::<VecBrand, _, _>(r, x))
2225 }
2226
2227 #[quickcheck]
2229 fn filterable_filter_consistency(x: Vec<i32>) -> bool {
2230 let p = |i: i32| i % 2 == 0;
2231 let maybe_bool = |i| if p(i) { Some(i) } else { None };
2232
2233 filter::<VecBrand, _>(p, x.clone()) == filter_map::<VecBrand, _, _>(maybe_bool, x)
2234 }
2235
2236 #[quickcheck]
2238 fn filterable_partition_map_identity(x: Vec<Result<i32, i32>>) -> bool {
2239 partition_map::<VecBrand, _, _, _>(identity, x.clone()) == separate::<VecBrand, _, _>(x)
2240 }
2241
2242 #[quickcheck]
2244 fn filterable_partition_map_right_identity(x: Vec<i32>) -> bool {
2245 let (_, oks) = partition_map::<VecBrand, _, _, _>(Ok::<_, i32>, x.clone());
2246 oks == x
2247 }
2248
2249 #[quickcheck]
2251 fn filterable_partition_map_left_identity(x: Vec<i32>) -> bool {
2252 let (errs, _) = partition_map::<VecBrand, _, _, _>(Err::<i32, _>, x.clone());
2253 errs == x
2254 }
2255
2256 #[quickcheck]
2258 fn filterable_partition_consistency(x: Vec<i32>) -> bool {
2259 let p = |i: i32| i % 2 == 0;
2260 let either_bool = |i| if p(i) { Ok(i) } else { Err(i) };
2261
2262 let (not_satisfied, satisfied) = partition::<VecBrand, _>(p, x.clone());
2263 let (errs, oks) = partition_map::<VecBrand, _, _, _>(either_bool, x);
2264
2265 satisfied == oks && not_satisfied == errs
2266 }
2267
2268 #[quickcheck]
2272 fn witherable_identity(x: Vec<i32>) -> bool {
2273 wither::<VecBrand, OptionBrand, _, _>(|i| Some(Some(i)), x.clone()) == Some(x)
2274 }
2275
2276 #[quickcheck]
2278 fn witherable_wilt_consistency(x: Vec<i32>) -> bool {
2279 let p = |i: i32| Some(if i % 2 == 0 { Ok(i) } else { Err(i) });
2280
2281 let lhs = wilt::<VecBrand, OptionBrand, _, _, _>(p, x.clone());
2282 let rhs = crate::classes::functor::map::<OptionBrand, _, _>(
2283 separate::<VecBrand, _, _>,
2284 traverse::<VecBrand, _, _, OptionBrand>(p, x),
2285 );
2286
2287 lhs == rhs
2288 }
2289
2290 #[quickcheck]
2292 fn witherable_wither_consistency(x: Vec<i32>) -> bool {
2293 let p = |i: i32| Some(if i % 2 == 0 { Some(i) } else { None });
2294
2295 let lhs = wither::<VecBrand, OptionBrand, _, _>(p, x.clone());
2296 let rhs = crate::classes::functor::map::<OptionBrand, _, _>(
2297 compact::<VecBrand, _>,
2298 traverse::<VecBrand, _, _, OptionBrand>(p, x),
2299 );
2300
2301 lhs == rhs
2302 }
2303
2304 #[quickcheck]
2308 fn alt_associativity(
2309 x: Vec<i32>,
2310 y: Vec<i32>,
2311 z: Vec<i32>,
2312 ) -> bool {
2313 alt::<VecBrand, _>(alt::<VecBrand, _>(x.clone(), y.clone()), z.clone())
2314 == alt::<VecBrand, _>(x, alt::<VecBrand, _>(y, z))
2315 }
2316
2317 #[quickcheck]
2319 fn alt_distributivity(
2320 x: Vec<i32>,
2321 y: Vec<i32>,
2322 ) -> bool {
2323 let f = |i: i32| i.wrapping_mul(2).wrapping_add(1);
2324 map::<VecBrand, _, _>(f, alt::<VecBrand, _>(x.clone(), y.clone()))
2325 == alt::<VecBrand, _>(map::<VecBrand, _, _>(f, x), map::<VecBrand, _, _>(f, y))
2326 }
2327
2328 #[quickcheck]
2332 fn plus_left_identity(x: Vec<i32>) -> bool {
2333 alt::<VecBrand, _>(plus_empty::<VecBrand, i32>(), x.clone()) == x
2334 }
2335
2336 #[quickcheck]
2338 fn plus_right_identity(x: Vec<i32>) -> bool {
2339 alt::<VecBrand, _>(x.clone(), plus_empty::<VecBrand, i32>()) == x
2340 }
2341
2342 #[test]
2344 fn plus_annihilation() {
2345 let f = |i: i32| i.wrapping_mul(2);
2346 assert_eq!(
2347 map::<VecBrand, _, _>(f, plus_empty::<VecBrand, i32>()),
2348 plus_empty::<VecBrand, i32>(),
2349 );
2350 }
2351
2352 #[quickcheck]
2356 fn compactable_functor_identity(fa: Vec<i32>) -> bool {
2357 compact::<VecBrand, _>(map::<VecBrand, _, _>(Some, fa.clone())) == fa
2358 }
2359
2360 #[test]
2362 fn compactable_plus_annihilation_empty() {
2363 assert_eq!(
2364 compact::<VecBrand, _>(plus_empty::<VecBrand, Option<i32>>()),
2365 plus_empty::<VecBrand, i32>(),
2366 );
2367 }
2368
2369 #[quickcheck]
2371 fn compactable_plus_annihilation_map(xs: Vec<i32>) -> bool {
2372 compact::<VecBrand, _>(map::<VecBrand, _, _>(|_: i32| None::<i32>, xs))
2373 == plus_empty::<VecBrand, i32>()
2374 }
2375
2376 #[test]
2380 fn compact_empty() {
2381 assert_eq!(compact::<VecBrand, i32>(vec![] as Vec<Option<i32>>), vec![] as Vec<i32>);
2382 }
2383
2384 #[test]
2386 fn compact_with_none() {
2387 assert_eq!(compact::<VecBrand, i32>(vec![Some(1), None, Some(2)]), vec![1, 2]);
2388 }
2389
2390 #[test]
2392 fn separate_empty() {
2393 let (errs, oks) = separate::<VecBrand, i32, i32>(vec![] as Vec<Result<i32, i32>>);
2394 assert_eq!(oks, vec![] as Vec<i32>);
2395 assert_eq!(errs, vec![] as Vec<i32>);
2396 }
2397
2398 #[test]
2400 fn separate_mixed() {
2401 let (errs, oks) = separate::<VecBrand, i32, i32>(vec![Ok(1), Err(2), Ok(3)]);
2402 assert_eq!(oks, vec![1, 3]);
2403 assert_eq!(errs, vec![2]);
2404 }
2405
2406 #[test]
2408 fn partition_map_empty() {
2409 let (errs, oks) =
2410 partition_map::<VecBrand, i32, i32, _>(|x: i32| Ok::<i32, i32>(x), vec![]);
2411 assert_eq!(oks, vec![] as Vec<i32>);
2412 assert_eq!(errs, vec![] as Vec<i32>);
2413 }
2414
2415 #[test]
2417 fn partition_empty() {
2418 let (not_satisfied, satisfied) = partition::<VecBrand, i32>(|x: i32| x > 0, vec![]);
2419 assert_eq!(satisfied, vec![] as Vec<i32>);
2420 assert_eq!(not_satisfied, vec![] as Vec<i32>);
2421 }
2422
2423 #[test]
2425 fn filter_map_empty() {
2426 assert_eq!(filter_map::<VecBrand, i32, _>(|x: i32| Some(x), vec![]), vec![] as Vec<i32>);
2427 }
2428
2429 #[test]
2431 fn filter_empty() {
2432 assert_eq!(filter::<VecBrand, i32>(|x: i32| x > 0, vec![]), vec![] as Vec<i32>);
2433 }
2434
2435 #[test]
2437 fn wilt_empty() {
2438 let res = wilt::<VecBrand, OptionBrand, _, _, _>(|x: i32| Some(Ok::<i32, i32>(x)), vec![]);
2439 assert_eq!(res, Some((vec![], vec![])));
2440 }
2441
2442 #[test]
2444 fn wither_empty() {
2445 let res = wither::<VecBrand, OptionBrand, _, _>(|x: i32| Some(Some(x)), vec![]);
2446 assert_eq!(res, Some(vec![]));
2447 }
2448
2449 #[test]
2453 fn test_large_vector_par_fold_map() {
2454 use crate::types::Additive;
2455
2456 let xs: Vec<i32> = (0 .. 100000).collect();
2457 let res = par_fold_map::<VecBrand, _, _>(|x: i32| Additive(x as i64), xs);
2458 assert_eq!(res, Additive(4999950000));
2459 }
2460
2461 #[quickcheck]
2463 fn prop_par_map_equals_map(xs: Vec<i32>) -> bool {
2464 let f = |x: i32| x.wrapping_add(1);
2465 let seq_res = map::<VecBrand, _, _>(f, xs.clone());
2466 let par_res = par_map::<VecBrand, _, _>(f, xs);
2467 seq_res == par_res
2468 }
2469
2470 #[quickcheck]
2472 fn prop_par_fold_map_equals_fold_map(xs: Vec<i32>) -> bool {
2473 use crate::types::Additive;
2474
2475 let f = |x: i32| Additive(x as i64);
2476 let seq_res = crate::classes::foldable::fold_map::<crate::brands::RcFnBrand, VecBrand, _, _>(
2477 f,
2478 xs.clone(),
2479 );
2480 let par_res = par_fold_map::<VecBrand, _, _>(f, xs);
2481 seq_res == par_res
2482 }
2483
2484 #[quickcheck]
2486 fn prop_par_fold_map_empty_is_empty(xs: Vec<i32>) -> bool {
2487 use crate::types::Additive;
2488
2489 if !xs.is_empty() {
2490 return true;
2491 }
2492 let par_res = par_fold_map::<VecBrand, _, _>(|x: i32| Additive(x as i64), xs);
2493 par_res == empty::<Additive<i64>>()
2494 }
2495
2496 #[quickcheck]
2500 fn monad_rec_identity(x: i32) -> bool {
2501 use {
2502 crate::classes::monad_rec::tail_rec_m,
2503 core::ops::ControlFlow,
2504 };
2505 tail_rec_m::<VecBrand, _, _>(|a| vec![ControlFlow::Break(a)], x) == vec![x]
2506 }
2507
2508 #[test]
2510 fn monad_rec_linear() {
2511 use {
2512 crate::classes::monad_rec::tail_rec_m,
2513 core::ops::ControlFlow,
2514 };
2515 let result = tail_rec_m::<VecBrand, _, _>(
2517 |n| {
2518 if n < 5 { vec![ControlFlow::Continue(n + 1)] } else { vec![ControlFlow::Break(n)] }
2519 },
2520 0,
2521 );
2522 assert_eq!(result, vec![5]);
2523 }
2524
2525 #[test]
2527 fn monad_rec_branching() {
2528 use {
2529 crate::classes::monad_rec::tail_rec_m,
2530 core::ops::ControlFlow,
2531 };
2532 let result = tail_rec_m::<VecBrand, _, _>(
2534 |n: i32| {
2535 if n < 2 {
2536 vec![ControlFlow::Continue(n + 1), ControlFlow::Break(n * 100)]
2537 } else {
2538 vec![ControlFlow::Break(n * 100)]
2539 }
2540 },
2541 0,
2542 );
2543 assert_eq!(result, vec![0, 100, 200]);
2547 }
2548
2549 #[test]
2551 fn monad_rec_empty() {
2552 use {
2553 crate::classes::monad_rec::tail_rec_m,
2554 core::ops::ControlFlow,
2555 };
2556 let result: Vec<i32> =
2557 tail_rec_m::<VecBrand, _, _>(|_n| Vec::<ControlFlow<i32, i32>>::new(), 0);
2558 assert_eq!(result, Vec::<i32>::new());
2559 }
2560
2561 #[test]
2565 fn extend_sum_of_suffixes() {
2566 use crate::classes::extend::extend;
2567 let result = extend::<VecBrand, _, _>(|v: Vec<i32>| v.iter().sum::<i32>(), vec![1, 2, 3]);
2568 assert_eq!(result, vec![6, 5, 3]);
2569 }
2570
2571 #[quickcheck]
2573 fn extend_associativity(w: Vec<i32>) -> bool {
2574 use crate::classes::extend::extend;
2575 let g = |v: Vec<i32>| v.iter().fold(0i32, |a, b| a.wrapping_mul(2).wrapping_add(*b));
2576 let f = |v: Vec<i32>| v.iter().fold(0i32, |a, b| a.wrapping_add(b.wrapping_add(1)));
2577 let lhs = extend::<VecBrand, _, _>(f, extend::<VecBrand, _, _>(g, w.clone()));
2578 let rhs = extend::<VecBrand, _, _>(|w: Vec<i32>| f(extend::<VecBrand, _, _>(g, w)), w);
2579 lhs == rhs
2580 }
2581
2582 #[test]
2584 fn extend_duplicate_suffixes() {
2585 use crate::classes::extend::duplicate;
2586 let result = duplicate::<VecBrand, _>(vec![1, 2, 3]);
2587 assert_eq!(result, vec![vec![1, 2, 3], vec![2, 3], vec![3]]);
2588 }
2589
2590 #[test]
2592 fn extend_empty() {
2593 use crate::classes::extend::extend;
2594 let result =
2595 extend::<VecBrand, _, _>(|v: Vec<i32>| v.iter().sum::<i32>(), Vec::<i32>::new());
2596 assert_eq!(result, Vec::<i32>::new());
2597 }
2598
2599 #[test]
2601 fn extend_singleton() {
2602 use crate::classes::extend::extend;
2603 let result = extend::<VecBrand, _, _>(|v: Vec<i32>| v.iter().sum::<i32>(), vec![42]);
2604 assert_eq!(result, vec![42]);
2605 }
2606}