Comments (8)
Seems like a good PR.
I don't know all the motivations for Order but I think it was that Ordering I believe was contravariant and scala had (or maybe has?) some issues with very surprising implicit resolution on contravariant type classes.
from cats.
I think it would be a great addition indeed.
However, I wonder why orElse
and orElseBy
were added to Ordering
, but not PartialOrdering
.
And whether we'd like to address it in PartialOrder
instead.
from cats.
I wonder why orElse and orElseBy were added to Ordering, but not PartialOrdering
What would be the semantics of orElse
etc when two elements are not comparable on the primary PartialOrder? ie tryCompare
returns None
. presumably theres no delegation for consistency. The name orElse
could lead people to think it tries another PO if the first is incomparable.
That semantic does seem workable . Perhaps the slight ambiguity, or simply that PartialOrdering is less commonly used, is the reason.
from cats.
That's right. So I think that in theory, PartialOrder
could offer two different orElse
like combinators, e.g. orElseOnEqual
and orElseOnUnrelated
. Then in Order
the former one should be simply inherited without changes, while the latter could short-circuit in order to simply return a result of "this" Order
instance.
Not sure if all that makes sense though, wdyt?
from cats.
A precedent for Partial order orElse
combinator
That's right. So I think that in theory, PartialOrder could offer two different orElse like combinators, e.g. orElseOnEqual and orElseOnUnrelated
I'm reluctant to implement a orElseOnUnrelated
combinator without a clear use case.
Then in Order the former one should be simply inherited without changes
In spirit, yes. But partial orders return a comparison wrapped in an Option, while total orders omit the Option.
Interestingly, ZIO nicely avoids an Option by using a 4-branch sealed trait (LT, Eq, GT, Incomparable), which nests a 3-branch trait used for total orders 🆒
from cats.
In spirit, yes. But partial orders return a comparison wrapped in an Option, while total orders omit the Option.
Interestingly, ZIO nicely avoids an Option by using a 4-branch sealed trait (LT, Eq, GT, Incomparable), which nests a 3-branch trait used for total orders 🆒
Actually, in Cats the tryCompare
method (which returns Option
) is complementary. The primary one is partialCompare
:
cats/kernel/src/main/scala/cats/kernel/PartialOrder.scala
Lines 59 to 80 in dd89133
So it still should be quite efficient.
from cats.
I'm reluctant to implement a orElseOnUnrelated combinator without a clear use case.
Agree. I'm just trying to poke around different possibilities and outcomes. For example, if a method can be added to a base trait, it is usually better to add it there from the beginning even if it is not needed right away. Because if we decided to add it to the base class later, then it would be more difficult to overcome inevitable binary compatibility issues.
But when it comes to orElseOnUnrelated
, it does not seem to be the case anyway – looks like it can be added later if necessary without any consequences (unless I'm missing something).
from cats.
What would be the semantics of
orElse
etc when two elements are not comparable on the primary PartialOrder? ietryCompare
returnsNone
. presumably theres no delegation for consistency. The nameorElse
could lead people to think it tries another PO if the first is incomparable.
A possible fix for this provides a nice parallel with the existing method: name it whenEqual
and expose it on the instance rather than the companion object.
This seems to imply fallbacks in any ambiguous case (equal or incomparable):
PartialOrdering
.by[Single, VPNTier](_.tier)
.orElseBy(it => (it.lang1, it.lang2))
.orElseBy(_.num)
.orElseBy(_.postfix)
This doesn't have that connotation (at least to my reading):
PartialOrdering
.by[Single, VPNTier](_.tier)
.whenEqual(it => (it.lang1, it.lang2))
.whenEqual(_.num)
.whenEqual(_.postfix)
from cats.
Related Issues (20)
- Inconsistent behaviour when using Eval as Applicative
- `Tuple1SemigroupalOps` methods have different names from other `TupleNSemigroupalOps` classes HOT 3
- Instances for Currency HOT 7
- EitherT[Option, ?, ?]] can't be used as a bifunctor HOT 5
- trait EuclidianRing should not be a Ring HOT 4
- OutOfMemoryError when IO.uncancelable is used in recursive function HOT 1
- ambiguous implicit resolution of `Show.ContravariantShow[immutable.SortedMap[K, V]]` HOT 1
- Should Monad extend Defer? HOT 11
- Support Scala Native 0.5 HOT 3
- Adding an alternative version of the method whenA with a different signature
- `Eval` thread safety HOT 4
- Optimize `distinct`/`distinctBy` implementations for non-empty collections
- [PROPOSAL] Aliases for methods `traverse_` and `sequence_` HOT 7
- Any reason `intercalate` isn't exposed directly on the `Semigroup` companion object? HOT 3
- (very breaking) change in Future instances? HOT 5
- Reveal internal but public identifiers and make them `private[cats]`
- Loss of type safety in scala 3 under certain conditions
- Make Later covariant in its type parameter
- Ease awkward constructions when doing something before raising HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cats.