gcanti / flow-static-land

[DEPRECATED, please check out fp-ts] Implementation of common algebraic types in JavaScript + Flow
MIT License
408 stars 22 forks source link

Alternative typeclasses when parameter constrained #33

Closed rjmk closed 7 years ago

rjmk commented 7 years ago

Related to #24.

As well as the getSemigroup method, one can also

export function concat<A>(semigroup: Semigroup<A>): (fx: Maybe<A>, fy: Maybe<A>) => Maybe<A> {
  return function concat(fx, fy) {
    const x = prj(fx)
    const y = prj(fy)
    if (x == null) {
      return fy
    }
    if (y == null) {
      return fx
    }
    return of(semigroup.concat(x, y))
  }
}

And type check with (<A>(a: Semigroup<A>) => ({ concat: concat(a) }: Semigroup(<Maybe<A>>))

I slightly prefer this API, but was wondering if you had strong feelings about it / reasons to avoid it

gcanti commented 7 years ago

Maybe export both APIs?

export function concat<A>(semigroup: Semigroup<A>): (fx: Maybe<A>, fy: Maybe<A>) => Maybe<A> {
  return function concat(fx, fy) {
    const x = prj(fx)
    const y = prj(fy)
    if (x == null) {
      return fy
    }
    if (y == null) {
      return fx
    }
    return of(semigroup.concat(x, y))
  }
}

export function getSemigroup<A>(semigroup: Semigroup<A>): Semigroup<Maybe<A>> {
  return {
    concat: concat(semigroup)
  }
}

export function getMonoid<A>(semigroup: Semigroup<A>): Monoid<Maybe<A>> {
  return {
    empty,
    concat: concat(semigroup)
  }
}
rjmk commented 7 years ago

That's really nice (I love the typechecking you get from getSemigroup)

gcanti commented 7 years ago

Are there other APIs which we should export in your opinion? For example concat in Either

rjmk commented 7 years ago

I think it makes sense to aim for parity with the Haskell & Purescript preludes

rjmk commented 7 years ago

Sorry for the terseness yesterday! I'll make some PRs (if they'd be well received) for some further instances in the coming days.

Also, how about chainRec? The stack safety can't be typechecked AFAIK, but the signature at least could be

gcanti commented 7 years ago

Sure, in the meanwhile I pushed https://github.com/gcanti/flow-static-land/pull/34

rjmk commented 7 years ago

:fire:

rjmk commented 7 years ago

What do you think about multiple instances of semigroup for Either? There's the one with no constraints as well:

instance Semigroup (Either a b) where
  Left _ <> b = b
  a <> _ = a
gcanti commented 7 years ago

Ah yes, we are free to implement several instances of Semigroup, it's the beauty of static-land. We have to just find suitable names :)

gcanti commented 7 years ago

Tracking chainRec here https://github.com/gcanti/flow-static-land/issues/35

gcanti commented 7 years ago

@rjmk FYI released in https://github.com/gcanti/flow-static-land/releases/tag/0.2.2