Skip to content

Commit

Permalink
refactor(monoid): removing semigroup in favour of using add from std
Browse files Browse the repository at this point in the history
  • Loading branch information
JasonShin committed Aug 9, 2019
1 parent dfa3ac2 commit a4a86cc
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 30 deletions.
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1121,29 +1121,22 @@ An object that has a `combine` function that combines it with another object of

It must obey following rules to be `Semigroup`

1. `a.combine(b).combine(c)` is equivalent to `a.combine(b.combine(c))` (associativity)
1. `a.add(b).add(c)` is equivalent to `a.add(b.add(c))` (associativity)

```rust
use itertools::concat;
use std::ops::Add;

trait Semigroup {
fn combine(&self, b: &Self) -> Self;
}

impl Semigroup for Vec<i32> {
fn combine(&self, b: &Self) -> Vec<i32> {
concat(vec![self.clone(), b.clone()])
}
pub trait Semigroup<M>: Add<M> {
}

assert_eq!(
vec![1, 2].combine(&vec![3, 4]),
vec![1, 2].add(&vec![3, 4]),
vec![1, 2, 3, 4],
); // passes

assert_eq!(
a.combine(&b).combine(&c),
a.combine(&b.combine(&c)),
a.add(&b).add(&c),
a.add(&b.add(&c)),
); // passes
```

Expand Down
1 change: 1 addition & 0 deletions fp-core/src/foldable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::hkt::HKT;
use crate::monoid::Monoid;

pub trait Foldable<A, B>: HKT<A, B> {
fn reduce<F>(b: B, ba: F) -> <Self as HKT<A, B>>::Target
Expand Down
1 change: 0 additions & 1 deletion fp-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ pub mod lens;
pub mod monad;
pub mod monoid;
pub mod pure;
pub mod semigroup;
pub mod setoid;
7 changes: 2 additions & 5 deletions fp-core/src/monoid.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use crate::applicative::Applicative;
use crate::empty::Empty;
use std::ops::Add;

trait Monoid<A, F, B>: Empty<A> + Applicative<A, F, B>
where
F: FnOnce(A) -> B,
{
}
pub trait Monoid<M>: Empty<M> + Add<M> {}
11 changes: 0 additions & 11 deletions fp-core/src/semigroup.rs

This file was deleted.

4 changes: 4 additions & 0 deletions fp-examples/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ mod semigroup_example;
mod setoid_example;
mod side_effects_example;
mod type_signature_example;

fn main() {
println!("Welcome to fp-core!");
}
4 changes: 4 additions & 0 deletions fp-examples/src/semigroup_example.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/*
// Note that below are just example code. We no longer use manual semigroup implementation.
// Instead we favour Add, Mul, Sub and etc from std.
use fp_core::semigroup::*;
#[test]
Expand All @@ -8,3 +11,4 @@ fn semigroup_test() {
assert_eq!(vec![1, 2].combine(&vec![3, 4]), vec![1, 2, 3, 4],);
assert_eq!(a.combine(&b).combine(&c), a.combine(&b.combine(&c)),);
}
*/

0 comments on commit a4a86cc

Please sign in to comment.