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
//! Sequencing of two computations while keeping the result of the first.
//!
//! ### Examples
//!
//! ```
//! use fp_library::{
//! brands::*,
//! functions::explicit::*,
//! };
//!
//! let x = Some(5);
//! let y = Some(10);
//! let z = apply_first::<OptionBrand, _, _, _, _>(x, y);
//! assert_eq!(z, Some(5));
//! ```
#[fp_macros::document_module]
mod inner {
use {
crate::{
classes::*,
kinds::*,
},
fp_macros::*,
};
/// A type class for types that support combining two contexts, keeping the first value.
///
/// `ApplyFirst` provides the ability to sequence two computations but discard
/// the result of the second computation, keeping only the result of the first.
pub trait ApplyFirst: Lift {
/// Combines two contexts, keeping the value from the first context.
///
/// This function sequences two computations and discards the result of the second computation, keeping only the result of the first.
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The type of the value in the first context.",
"The type of the value in the second context."
)]
///
#[document_parameters("The first context.", "The second context.")]
///
#[document_returns("The first context.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = Some(10);
/// let z = apply_first::<OptionBrand, _, _, _, _>(x, y);
/// assert_eq!(z, Some(5));
/// ```
fn apply_first<'a, A: 'a + Clone, B: 'a + Clone>(
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
fb: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
) -> Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Self::lift2(|a, _| a, fa, fb)
}
}
/// Combines two contexts, keeping the value from the first context.
///
/// Free function version that dispatches to [the type class' associated function][`ApplyFirst::apply_first`].
#[document_signature]
///
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the context.",
"The type of the value in the first context.",
"The type of the value in the second context."
)]
///
#[document_parameters("The first context.", "The second context.")]
///
#[document_returns("The first context.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::explicit::*,
/// };
///
/// let x = Some(5);
/// let y = Some(10);
/// let z = apply_first::<OptionBrand, _, _, _, _>(x, y);
/// assert_eq!(z, Some(5));
/// ```
pub fn apply_first<'a, Brand: ApplyFirst, A: 'a + Clone, B: 'a + Clone>(
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
fb: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, B>),
) -> Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>) {
Brand::apply_first(fa, fb)
}
}
pub use inner::*;