1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//! Data structures that can be compacted by filtering out [`None`] or separated by splitting [`Result`] values.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! brands::*,
//! functions::explicit::*,
//! };
//!
//! let x = Some(Some(5));
//! let y = compact::<OptionBrand, _, _, _>(x);
//! assert_eq!(y, Some(5));
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::{
brands::*,
kinds::*,
},
fp_macros::*,
};
/// A type class for data structures that can be compacted and separated.
///
/// `Compactable` allows for:
/// * `compact`: Filtering out [`None`] values and unwrapping [`Some`] values from a structure of [`Option`]s.
/// * `separate`: Splitting a structure of [`Result`]s into a pair of structures, one containing the [`Err`] values and the other containing the [`Ok`] values.
///
/// ### Laws
///
/// To be `Compactable` alone, no laws must be satisfied other than the type signature.
///
/// If the data type is also a [`Functor`](crate::classes::Functor):
/// * Identity: `compact(map(Some, fa)) = fa`.
///
/// If the data type is also [`Plus`](crate::classes::Plus):
/// * Annihilation (empty): `compact(empty) = empty`.
/// * Annihilation (map): `compact(map(|_| None, xs)) = empty`.
#[document_examples]
///
/// Compactable laws for [`Option`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::{
/// explicit::{
/// compact,
/// map,
/// },
/// *,
/// },
/// };
///
/// // Functor Identity: compact(map(Some, fa)) = fa
/// assert_eq!(
/// compact::<OptionBrand, _, _, _>(map::<OptionBrand, _, _, _, _>(Some, Some(5))),
/// Some(5),
/// );
/// assert_eq!(
/// compact::<OptionBrand, _, _, _>(map::<OptionBrand, _, _, _, _>(Some, None::<i32>)),
/// None,
/// );
///
/// // Plus Annihilation (empty): compact(empty) = empty
/// assert_eq!(
/// compact::<OptionBrand, _, _, _>(plus_empty::<OptionBrand, Option<i32>>()),
/// plus_empty::<OptionBrand, i32>(),
/// );
///
/// // Plus Annihilation (map): compact(map(|_| None, xs)) = empty
/// assert_eq!(
/// compact::<OptionBrand, _, _, _>(map::<OptionBrand, _, _, _, _>(
/// |_: i32| None::<i32>,
/// Some(5)
/// )),
/// plus_empty::<OptionBrand, i32>(),
/// );
/// ```
///
/// Compactable laws for [`Vec`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::{
/// explicit::{
/// compact,
/// map,
/// },
/// *,
/// },
/// };
///
/// // Functor Identity: compact(map(Some, fa)) = fa
/// assert_eq!(
/// compact::<VecBrand, _, _, _>(map::<VecBrand, _, _, _, _>(Some, vec![1, 2, 3])),
/// vec![1, 2, 3],
/// );
///
/// // Plus Annihilation (empty): compact(empty) = empty
/// assert_eq!(
/// compact::<VecBrand, _, _, _>(plus_empty::<VecBrand, Option<i32>>()),
/// plus_empty::<VecBrand, i32>(),
/// );
///
/// // Plus Annihilation (map): compact(map(|_| None, xs)) = empty
/// assert_eq!(
/// compact::<VecBrand, _, _, _>(map::<VecBrand, _, _, _, _>(
/// |_: i32| None::<i32>,
/// vec![1, 2, 3]
/// )),
/// plus_empty::<VecBrand, i32>(),
/// );
/// ```
#[kind(type Of<'a, A: 'a>: 'a;)]
pub trait Compactable {
/// Compacts a data structure of [`Option`]s, discarding [`None`] values and keeping [`Some`] values.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The type of the elements in the [`Option`]."
)]
///
#[document_parameters("The data structure containing [`Option`] values.")]
///
#[document_returns(
"A new data structure containing only the values from the [`Some`] variants."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(Some(5));
/// let y = compact::<OptionBrand, _, _, _>(x);
/// assert_eq!(y, Some(5));
///
/// let z = Some(None::<i32>);
/// let w = compact::<OptionBrand, _, _, _>(z);
/// assert_eq!(w, None);
/// ```
fn compact<'a, A: 'a>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
>)
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>);
/// Separates a data structure of [`Result`]s into two data structures: one containing the [`Err`] values and one containing the [`Ok`] values.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The type of the error values.",
"The type of the success values."
)]
///
#[document_parameters("The data structure containing [`Result`] values.")]
///
#[document_returns(
"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x: Option<Result<i32, &str>> = Some(Ok(5));
/// let (errs, oks) = separate::<OptionBrand, _, _, _, _>(x);
/// assert_eq!(oks, Some(5));
/// assert_eq!(errs, None);
///
/// let y: Option<Result<i32, &str>> = Some(Err("error"));
/// let (errs2, oks2) = separate::<OptionBrand, _, _, _, _>(y);
/// assert_eq!(oks2, None);
/// assert_eq!(errs2, Some("error"));
/// ```
fn separate<'a, E: 'a, O: 'a>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
) -> (
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
);
}
/// Compacts a data structure of [`Option`]s, discarding [`None`] values and keeping [`Some`] values.
///
/// Free function version that dispatches to [the type class' associated function][`Compactable::compact`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the compactable structure.",
"The type of the elements in the [`Option`]."
)]
///
#[document_parameters("The data structure containing [`Option`] values.")]
///
#[document_returns(
"A new data structure containing only the values from the [`Some`] variants."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(Some(5));
/// let y = compact::<OptionBrand, _, _, _>(x);
/// assert_eq!(y, Some(5));
/// ```
pub fn compact<'a, Brand: Compactable, A: 'a>(
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<
'a,
Apply!(<OptionBrand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
>)
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Brand::compact(fa)
}
/// Separates a data structure of [`Result`]s into two data structures: one containing the [`Err`] values and one containing the [`Ok`] values.
///
/// Free function version that dispatches to [the type class' associated function][`Compactable::separate`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the elements.",
"The brand of the compactable structure.",
"The type of the error values.",
"The type of the success values."
)]
///
#[document_parameters("The data structure containing [`Result`] values.")]
///
#[document_returns(
"A pair of data structures: the first containing the [`Err`] values, and the second containing the [`Ok`] values."
)]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x: Option<Result<i32, &str>> = Some(Ok(5));
/// let (errs, oks) = separate::<OptionBrand, _, _, _, _>(x);
/// assert_eq!(oks, Some(5));
/// assert_eq!(errs, None);
/// ```
pub fn separate<'a, Brand: Compactable, E: 'a, O: 'a>(
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, Result<O, E>>)
) -> (
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, E>),
Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, O>),
) {
Brand::separate::<E, O>(fa)
}
}
pub use inner::*;