partial_functional/monoid/
mod.rs1mod last;
2mod first;
3mod sum;
4mod product;
5mod any;
6mod all;
7mod min;
8mod max;
9
10pub use self::{
11 last::Last,
12 first::First,
13 sum::Sum,
14 product::Product,
15 any::Any,
16 all::All,
17 min::Min,
18 max::Max,
19};
20
21use crate::semigroup::Semigroup;
22
23pub trait Monoid: Semigroup {
24 fn empty() -> Self;
25}
26
27impl<T: Semigroup + Default> Monoid for T {
28 fn empty() -> Self {
29 Default::default()
30 }
31}
32
33#[cfg(test)]
34mod tests {
35 use super::*;
36
37 use super::last::Last;
38 use super::sum::Sum;
39
40 #[test]
41 fn combine_with_into() {
42 let x = Last::empty()
43 .combine(53.into())
44 .combine(None.into())
45 .combine(42.into());
46
47 assert_eq!(x.0, Some(42));
48 }
49
50 #[test]
51 fn option_combine_macro() {
52 let sum: Option<Sum<i32>> = crate::combine!(
53 None,
54 Sum::from(10),
55 None,
56 Sum::from(5),
57 Sum::from(7),
58 None,
59 Sum::from(42),
60 None,
61 );
62
63 assert_eq!(sum.unwrap(), 64);
64 }
65
66 #[test]
67 fn combine_macro() {
68 let x = crate::combine! {
69 Last::from(53), None, 42, {let b = None; b},
70 };
71
72 assert_eq!(x.0, Some(42));
73 }
74
75 #[test]
76 fn last_to_option_conversion() {
77 let last = Last::from(42);
78 let res: Option<i32> = last.into();
79
80 assert_eq!(res, Some(42));
81 }
82}