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
//! Semigroup and Monoid to use with Writer

pub trait Semigroup {

   fn mappend(self, other: &mut Self) -> Self;
}

pub trait Monoid: Semigroup {
   fn mempty() -> Self;
}

//--------------------------------------------

impl Semigroup for String {

  fn mappend( mut self, other: &mut Self) -> Self {
   self.push_str( other);
   self
  }
}

impl Monoid for String {
  fn mempty() -> Self { Self::new()}
}

//--------------------------------------------

use std::clone::Clone;

impl<T: Clone> Semigroup for Vec<T> {

  fn mappend( mut self, other: &mut Self) -> Self {
   self.append( other);
   self
  }
}

impl<T: Clone> Monoid for Vec<T> {
  fn mempty() -> Self { Self::new()}
}