1use super::{Applicative, Foldable, Functor};
14use crate::{Apply, functions::identity, kinds::*};
15use fp_macros::doc_params;
16use fp_macros::doc_type_params;
17use fp_macros::hm_signature;
18
19pub trait Traversable: Functor + Foldable {
23 #[hm_signature(Traversable)]
35 #[doc_type_params(
39 "The lifetime of the elements.",
40 "The type of the elements in the traversable structure.",
41 "The type of the elements in the resulting traversable structure.",
42 "The applicative context.",
43 "The type of the function to apply."
44 )]
45 #[doc_params(
49 "The function to apply to each element, returning a value in an applicative context.",
50 "The traversable structure."
51 )]
52 fn traverse<'a, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
67 func: Func,
68 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
69 ) -> 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>)>)
70 where
71 Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
72 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
73 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
74 {
75 Self::sequence::<B, F>(Self::map::<
76 A,
77 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
78 Func,
79 >(func, ta))
80 }
81
82 #[hm_signature(Traversable)]
89 #[doc_type_params(
93 "The lifetime of the elements.",
94 "The type of the elements in the traversable structure.",
95 "The applicative context."
96 )]
97 #[doc_params("The traversable structure containing values in an applicative context.")]
101 fn sequence<'a, A: 'a + Clone, F: Applicative>(
116 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>)>)
117 ) -> 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>)>)
118 where
119 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
120 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
121 {
122 Self::traverse::<Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>), A, F, _>(
123 identity, ta,
124 )
125 }
126}
127
128#[hm_signature(Traversable)]
135#[doc_type_params(
139 "The lifetime of the elements.",
140 "The brand of the traversable structure.",
141 "The type of the elements in the traversable structure.",
142 "The type of the elements in the resulting traversable structure.",
143 "The applicative context.",
144 "The type of the function to apply."
145)]
146#[doc_params(
150 "The function to apply to each element, returning a value in an applicative context.",
151 "The traversable structure."
152)]
153pub fn traverse<'a, Brand: Traversable, A: 'a + Clone, B: 'a + Clone, F: Applicative, Func>(
168 func: Func,
169 ta: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
170) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>)>)
171where
172 Func: Fn(A) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>) + 'a,
173 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
174 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>): Clone,
175{
176 Brand::traverse::<A, B, F, Func>(func, ta)
177}
178
179#[hm_signature(Traversable)]
186#[doc_type_params(
190 "The lifetime of the elements.",
191 "The brand of the traversable structure.",
192 "The type of the elements in the traversable structure.",
193 "The applicative context."
194)]
195#[doc_params("The traversable structure containing values in an applicative context.")]
199pub fn sequence<'a, Brand: Traversable, A: 'a + Clone, F: Applicative>(
214 ta: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
215) -> Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>)>)
216where
217 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
218 Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>): Clone,
219{
220 Brand::sequence::<A, F>(ta)
221}