fp_library/types/last.rs
1//! A newtype wrapper whose [`Semigroup`](crate::classes::Semigroup) instance always keeps the last
2//! (rightmost) value.
3//!
4//! ### Examples
5//!
6//! ```
7//! use fp_library::{
8//! functions::*,
9//! types::Last,
10//! };
11//!
12//! assert_eq!(append(Last(1), Last(2)), Last(2));
13//! ```
14
15#[fp_macros::document_module]
16mod inner {
17 use {
18 crate::classes::*,
19 fp_macros::*,
20 };
21
22 /// A newtype wrapper whose [`Semigroup`] instance always keeps the last value.
23 ///
24 /// `append(Last(a), Last(b))` returns `Last(b)`, discarding `a`.
25 ///
26 /// There is no [`Monoid`] instance because there is no identity element.
27 #[document_examples]
28 ///
29 /// ```
30 /// use fp_library::{
31 /// functions::*,
32 /// types::Last,
33 /// };
34 ///
35 /// assert_eq!(append(Last("hello"), Last("world")), Last("world"));
36 /// ```
37 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
38 #[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
39 pub struct Last<A>(
40 /// The wrapped value.
41 pub A,
42 );
43
44 #[document_type_parameters("The wrapped type.")]
45 impl<A> Semigroup for Last<A> {
46 /// Returns the last (rightmost) value, discarding the first.
47 #[document_signature]
48 ///
49 #[document_parameters("The first value (discarded).", "The second value (kept).")]
50 ///
51 #[document_returns("The second value.")]
52 #[document_examples]
53 ///
54 /// ```
55 /// use fp_library::{
56 /// functions::*,
57 /// types::Last,
58 /// };
59 ///
60 /// assert_eq!(append(Last(1), Last(2)), Last(2));
61 /// ```
62 fn append(
63 _a: Self,
64 b: Self,
65 ) -> Self {
66 b
67 }
68 }
69}
70
71pub use inner::*;
72
73#[cfg(test)]
74mod tests {
75 use {
76 super::*,
77 crate::functions::*,
78 quickcheck_macros::quickcheck,
79 };
80
81 #[quickcheck]
82 fn semigroup_associativity(
83 a: i32,
84 b: i32,
85 c: i32,
86 ) -> bool {
87 let x = Last(a);
88 let y = Last(b);
89 let z = Last(c);
90 append(x, append(y, z)) == append(append(x, y), z)
91 }
92}