Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

Alternative typeclasses when parameter constrained #33

Closed
rjmk opened this issue Oct 5, 2016 · 11 comments
Closed

Alternative typeclasses when parameter constrained #33

rjmk opened this issue Oct 5, 2016 · 11 comments

Comments

@rjmk
Copy link

rjmk commented Oct 5, 2016

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
Copy link
Owner

gcanti commented Oct 5, 2016

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
Copy link
Author

rjmk commented Oct 5, 2016

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

@rjmk rjmk closed this as completed Oct 5, 2016
@gcanti
Copy link
Owner

gcanti commented Oct 5, 2016

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

@rjmk
Copy link
Author

rjmk commented Oct 5, 2016

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

@rjmk
Copy link
Author

rjmk commented Oct 6, 2016

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 added a commit that referenced this issue Oct 6, 2016
@gcanti
Copy link
Owner

gcanti commented Oct 6, 2016

Sure, in the meanwhile I pushed #34

@rjmk
Copy link
Author

rjmk commented Oct 6, 2016

🔥

@rjmk
Copy link
Author

rjmk commented Oct 6, 2016

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
Copy link
Owner

gcanti commented Oct 6, 2016

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
Copy link
Owner

gcanti commented Oct 6, 2016

Tracking chainRec here #35

gcanti added a commit that referenced this issue Oct 6, 2016
* Add Pointed and Copointed functor, fix #15

* expose concat APIs, fix #33
@gcanti
Copy link
Owner

gcanti commented Oct 12, 2016

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants