rust_fp_categories/
semigroup.rs

1pub trait Semigroup {
2    fn combine(self, other: Self) -> Self;
3}
4
5macro_rules! semigroup_numeric_impl {
6    ($($t:ty)*) => ($(
7        impl Semigroup for $t {
8            fn combine(self, other: Self) -> Self {
9                self + other
10            }
11        }
12    )*)
13}
14
15semigroup_numeric_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
16
17impl<T: Clone> Semigroup for Vec<T> {
18    fn combine(self, other: Self) -> Self {
19        let mut concat: Vec<T> = self.to_vec();
20        concat.extend_from_slice(&other);
21        concat
22    }
23}
24
25impl Semigroup for String {
26    fn combine(self, other: Self) -> Self {
27        format!("{}{}", self, other)
28    }
29}