JasonShin / fp-core.rs

A library for functional programming in Rust
MIT License
1.34k stars 66 forks source link

Semigroup, tweaks HKT, removes type parameters #36

Closed hemangandhi closed 5 years ago

hemangandhi commented 5 years ago

Specifically, I did the following:

What was wrong with:

JasonShin commented 5 years ago

Bug found in the PR:

Applicative (also Monad) example is failing due to following issue

error[E0277]: cannot add `{integer}` to `std::option::Option<{integer}>`
 --> fp-examples/src/applicative_example.rs:6:47
  |
6 |     let x = Option::of(Some(1)).ap(Some(|x| x + 1));
  |                                               ^ no implementation for `std::option::Option<{integer}> + {integer}`
  |
  = help: the trait `std::ops::Add<{integer}>` is not implemented for `std::option::Option<{integer}>`

I think it's because in below impl of Option

impl<A, F, B> Apply<F, B> for Option<A>
where
    F: FnOnce(A) -> B,
{
    fn ap(self, f: Self::Target2) -> Self::Target {
        self.and_then(|v| {
            f.map(|z| z(v))
        })
    }
}

A somehow forces Option<A> to become a nested option. So even if you self.and_then you get another option back. I feel like we should revert Self::HKT::Current into A and make it easier to fix the issue (also easier to read)

JasonShin commented 5 years ago

Looks good now, I've mentioned follow up tasks in our telegram chat. I am going to work on them ;)

@hemangandhi congrats on your 2nd PR to fp-core! this is a massive one!