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
use cargo_snippet::snippet;

/// 単位元が定義される `T -> T -> T`型の演算
#[snippet("monoid")]
pub trait Monoid: Sized {
    fn identity() -> Self;

    fn op(x: &Self, y: &Self) -> Self;

    fn fold(v: &[Self]) -> Self {
        v.iter().fold(Self::identity(), |a, b| Self::op(&a, b))
    }
}

/// Monoidトレイトの自動実装マクロ
///
/// 一行目にモノイド名、
/// 二行目に単位元
/// 三行目に`2引数を取って、同型の演算結果を返すクロージャを渡す
///
/// ```example
/// monoid_def! {
///     Max<usize>,
///     std::usize::MIN,
///     |x: usize, y: usize| x.max(y)
/// }
/// ```
///
#[snippet("monoid")]
#[macro_export]
macro_rules! monoid_def {
    {
        $M:ident<$t:ty>,
        $id:expr,
        $me:expr
    } => {
        #[derive(Debug, Clone, Copy)]
        pub struct $M($t);

        impl Monoid for $M {
            fn identity() -> Self {
                $M($id)
            }

            fn op(x: &Self, y: &Self) -> Self {
                let f = $me;
                $M(f(x.0, y.0))
            }
        }

        impl Into<$M> for $t {
            fn into(self) -> $M {
                $M(self)
            }
        }
    };
}

// monoid_def! {
//     Max<usize>,
//     std::usize::MIN,
//     |a: usize, b: usize| a.max(b)
// }