Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

instances for std Either. closes #117 #125

Merged
merged 7 commits into from
Feb 10, 2015
Merged

Conversation

wedens
Copy link
Contributor

@wedens wedens commented Feb 8, 2015

No description provided.

@stew
Copy link
Contributor

stew commented Feb 8, 2015

looking good. I think we also have MonadCombine when the left is a monoid (we need the monoidal zero on the left for filter)

@wedens
Copy link
Contributor Author

wedens commented Feb 8, 2015

I don't know how to implement MonadFilter without requiring A (left) to always have Monoid. For MonadCombine, A should always have Monoid and B should always have Semigroup. Any suggestions?

@wedens
Copy link
Contributor Author

wedens commented Feb 8, 2015

Or also don't implements MonadCombine btw

@wedens
Copy link
Contributor Author

wedens commented Feb 8, 2015

I tried to write something, but I'm not sure it's correct.

@ceedubs
Copy link
Contributor

ceedubs commented Feb 8, 2015

@stew (and anyone else who wants to weigh in: Is the MonadCombine that requires the left's Monoid[A] useful? It seems to me like it wouldn't be something that people would really want to use in practice.

If we do keep it, I think we can change Monad[Either[A, ?]] with MonadCombine[Either[A, ?]] to just MonadCombine[Either[A, ?]] since MonadCombine extends Monad.

@stew
Copy link
Contributor

stew commented Feb 8, 2015

hmm, maybe not MonadCombine, but MonadFilter for using them in for comprehensions?

@xuwei-k
Copy link
Contributor

xuwei-k commented Feb 8, 2015

First, what is the laws of MonadFilter? cats.MonadFilter is scalaz.MonadPlus?

@ceedubs
Copy link
Contributor

ceedubs commented Feb 8, 2015

@stew so something like this?

val errorOrInt: Either[String, Int] = Right(-2)
for {
  x <- errorOrInt if (x > 0)
} yield x + 1
// Either[String, Int] = Left("")

It just seems unlikely that this is actually what people would want. It seems like something like \/.ensure from scalaz should be used instead. I certainly can't speak for all users and what they want, but I guess it seems a bit error-prone to me.

@xuwei-k
Copy link
Contributor

xuwei-k commented Feb 8, 2015

similar discussion in scalaz https://groups.google.com/d/topic/scalaz/NCiRRxLjanM/discussion

@wedens
Copy link
Contributor Author

wedens commented Feb 8, 2015

also withFilter is required to deconstruct tuples in for comprehension

@non
Copy link
Contributor

non commented Feb 8, 2015

@xuwei-k MonadCombine is the analogue to scalaz.MonadPlus. MonadFilter is a bit weaker.

It's a great point -- we need laws for MonadFilter! I think most of the laws revolve around how its empty method behaves.

For example: empty[A].flatMap(f) = empty[A] for any f: A => M[A].

@mpilquist
Copy link
Member

In addition to the law @non listed, I think we also need: fa.flatMap { a => empty[B] } === empty[B] for any fa: F[A]

@non
Copy link
Contributor

non commented Feb 8, 2015

Right, and really my law should be generalized to empty[A].flatMap(f) = empty[B] for any f: A => M[B].

@aryairani
Copy link
Contributor

@wedens Definitely wish for withFilter if it will enable deconstructing into tuples instead of needing two lines for that.

@mpilquist
Copy link
Member

I don't think we need to solve this as part of this PR, but I think we should discuss whether we really want Either and Or to define a MonadFilter instance, or in the case of Or, a filter / withFilter. I share @ceedubs concern here. I don't think there's a relation between the Monoid[A].empty and MonadFilter[A Or ?].empty. Hence, I'd rather just define Monad for each of these types, and remove filter and withFilter.

@wedens
Copy link
Contributor Author

wedens commented Feb 10, 2015

Ok. I'll remove this controversial instances, so we can add them later when it'll became more clear.

implicit def eitherOrder[A: Order, B: Order]: Order[Either[A, B]] = new Order[Either[A, B]] {
def compare(x: Either[A, B], y: Either[A, B]): Int = x.fold(
a => y.fold(Order[A].compare(a, _), _ => -1),
b => y.fold(_ => 1, Order[B].compare(b, _))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry we haven't added it to the contributor guide, but can you please follow the convention agreed upon in #27 and use implicit A: Order[A], B: Order[B] and then use A.compare and B.compare instead of Order[A].compare and Order[B].compare?

@wedens
Copy link
Contributor Author

wedens commented Feb 10, 2015

@ceedubs done. should i squash commits?

def traverse[F[_]: Applicative, B, C](fa: Either[A, B])(f: B => F[C]): F[Either[A, C]] =
fa.fold(
a => Applicative[F].pure(Left(a)),
b => Applicative[F].map(f(b))(Right(_))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wedens ah sorry here's one more that I missed - the Applicative.apply calls.

@ceedubs
Copy link
Contributor

ceedubs commented Feb 10, 2015

@wedens nah it sounds like enough cats contributors dislike changing git history that it's preferred to not squash commits once a PR is open. We need to get this into the contributor guide.

Thanks for all of the work and the prompt turnarounds. Sorry that a bunch of discussions (MonadFilter, etc) got pork-barreled into this PR discussion.

@ceedubs
Copy link
Contributor

ceedubs commented Feb 10, 2015

Wow that was a fast turnaround!

👍 once Travis goes green.

@wedens
Copy link
Contributor Author

wedens commented Feb 10, 2015

@ceedubs yeah. It's definetly should be in contributor guide as it varies from project to project. Some prefer to squash commits, others prefer not to overwrite history.

@ceedubs
Copy link
Contributor

ceedubs commented Feb 10, 2015

@wedens yeah you are definitely right. I'll open a PR adding this to the contributor guide right away.

Edit: #170 submitted

@non
Copy link
Contributor

non commented Feb 10, 2015

👍

non added a commit that referenced this pull request Feb 10, 2015
instances for std Either. closes #117
@non non merged commit e27f316 into typelevel:master Feb 10, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants