1use crate::{
14 Apply,
15 classes::{Applicative, Filterable, Traversable},
16 kinds::*,
17 types::Pair,
18};
19use fp_macros::doc_params;
20use fp_macros::doc_type_params;
21use fp_macros::hm_signature;
22
23pub trait Witherable: Filterable + Traversable {
35 #[hm_signature(Witherable)]
42 #[doc_type_params(
46 "The lifetime of the elements.",
47 "The applicative context.",
48 "The type of the elements in the input structure.",
49 "The type of the success values.",
50 "The type of the error values.",
51 "The type of the function to apply."
52 )]
53 #[doc_params(
57 "The function to apply to each element, returning a [`Result`] in an applicative context.",
58 "The data structure to partition."
59 )]
60 fn wilt<'a, M: Applicative, A: 'a + Clone, O: 'a + Clone, E: 'a + Clone, Func>(
75 func: Func,
76 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
77 ) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
78 'a,
79 Pair<
80 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
81 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
82 >,
83 >)
84 where
85 Func: Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>) + 'a,
86 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
87 Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
88 {
89 M::map(|res| Self::separate(res), Self::traverse::<A, Result<O, E>, M, Func>(func, ta))
90 }
91
92 #[hm_signature(Witherable)]
99 #[doc_type_params(
103 "The lifetime of the elements.",
104 "The applicative context.",
105 "The type of the elements in the input structure.",
106 "The type of the elements in the output structure.",
107 "The type of the function to apply."
108 )]
109 #[doc_params(
113 "The function to apply to each element, returning an [`Option`] in an applicative context.",
114 "The data structure to filter and map."
115 )]
116 fn wither<'a, M: Applicative, A: 'a + Clone, B: 'a + Clone, Func>(
131 func: Func,
132 ta: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
133 ) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
134 'a,
135 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
136 >)
137 where
138 Func: Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>) + 'a,
139 Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
140 Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
141 {
142 M::map(|opt| Self::compact(opt), Self::traverse::<A, Option<B>, M, Func>(func, ta))
143 }
144}
145
146#[hm_signature(Witherable)]
153#[doc_type_params(
157 "The lifetime of the elements.",
158 "The brand of the witherable structure.",
159 "The applicative context.",
160 "The type of the elements in the input structure.",
161 "The type of the success values.",
162 "The type of the error values.",
163 "The type of the function to apply."
164)]
165#[doc_params(
169 "The function to apply to each element, returning a [`Result`] in an applicative context.",
170 "The data structure to partition."
171)]
172pub fn wilt<'a, F: Witherable, M: Applicative, A: 'a + Clone, O: 'a + Clone, E: 'a + Clone, Func>(
187 func: Func,
188 ta: Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
189) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
190 'a,
191 Pair<
192 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
193 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
194 >,
195>)
196where
197 Func: Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>) + 'a,
198 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
199 Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>): Clone,
200{
201 F::wilt::<M, A, O, E, Func>(func, ta)
202}
203
204#[hm_signature(Witherable)]
211#[doc_type_params(
215 "The lifetime of the elements.",
216 "The brand of the witherable structure.",
217 "The applicative context.",
218 "The type of the elements in the input structure.",
219 "The type of the elements in the output structure.",
220 "The type of the function to apply."
221)]
222#[doc_params(
226 "The function to apply to each element, returning an [`Option`] in an applicative context.",
227 "The data structure to filter and map."
228)]
229pub fn wither<'a, F: Witherable, M: Applicative, A: 'a + Clone, B: 'a + Clone, Func>(
244 func: Func,
245 ta: Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
246) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
247 'a,
248 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
249>)
250where
251 Func: Fn(A) -> Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>) + 'a,
252 Apply!(<F as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
253 Apply!(<M as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Option<B>>): Clone,
254{
255 F::wither::<M, A, B, Func>(func, ta)
256}