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