Skip to main content

fp_library/types/
first.rs

1//! A newtype wrapper whose [`Semigroup`](crate::classes::Semigroup) instance always keeps the first
2//! (leftmost) value.
3//!
4//! ### Examples
5//!
6//! ```
7//! use fp_library::{
8//! 	functions::*,
9//! 	types::First,
10//! };
11//!
12//! assert_eq!(append(First(1), First(2)), First(1));
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 first value.
23	///
24	/// `append(First(a), First(b))` returns `First(a)`, discarding `b`.
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::First,
33	/// };
34	///
35	/// assert_eq!(append(First("hello"), First("world")), First("hello"));
36	/// ```
37	#[derive(Clone, Copy, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
38	pub struct First<A>(
39		/// The wrapped value.
40		pub A,
41	);
42
43	#[document_type_parameters("The wrapped type.")]
44	impl<A> Semigroup for First<A> {
45		/// Returns the first (leftmost) value, discarding the second.
46		#[document_signature]
47		///
48		#[document_parameters("The first value (kept).", "The second value (discarded).")]
49		///
50		#[document_returns("The first value.")]
51		#[document_examples]
52		///
53		/// ```
54		/// use fp_library::{
55		/// 	functions::*,
56		/// 	types::First,
57		/// };
58		///
59		/// assert_eq!(append(First(1), First(2)), First(1));
60		/// ```
61		fn append(
62			a: Self,
63			_b: Self,
64		) -> Self {
65			a
66		}
67	}
68}
69
70pub use inner::*;
71
72#[cfg(test)]
73mod tests {
74	use {
75		super::*,
76		crate::functions::*,
77		quickcheck_macros::quickcheck,
78	};
79
80	#[quickcheck]
81	fn semigroup_associativity(
82		a: i32,
83		b: i32,
84		c: i32,
85	) -> bool {
86		let x = First(a);
87		let y = First(b);
88		let z = First(c);
89		append(x, append(y, z)) == append(append(x, y), z)
90	}
91}