Introduce The Inverse Type
Introduce The Inverse Type
The type Inverse[A] is a newtype which exists to invert the Order, PartialOrder, UpperBounded and LowerBounded instances for some type A.
It's basically just Id[A] with specialized instances for ordering.
Hypothetically, we could implement all of the instances defined for Id for Inverse, however before I do that (and before I write any more docs or any tests), I wanted to see what everyone thinks about this?
Prior art: https://hackage.haskell.org/package/base-4.6.0.1/docs/Data-Ord.html#Down
Thoughts?
scala> import cats.data._
import cats.data._
scala> import scala.collection.immutable.SortedSet
import scala.collection.immutable.SortedSet
scala> SortedSet(1, 2, 3)
val res0: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3)
scala> res0.map(Inverse.apply)
val res1: scala.collection.immutable.SortedSet[cats.data.Inverse[Int]] = TreeSet(3, 2, 1)
I've also considered defining it on object Order, similar to how Data.Ord scopes Down, but given the use of NewType having it in cats.data seemed better, but I'm open to other thoughts on where it should live (assuming people want it to live anywhere).
Currently we have a bunch of similar newtypes living in monoids. https://github.com/typelevel/monoids
:thinking: hmm...I don't think Inverse[A] in and of itself implies any Semigroup or Monoid, though I agree this does have a lot of similarity to things like Max and Min in structure. Do you think I should move this over there @armanbilge? It feels a little off to me since it isn't a monoid or semigroup directly, like the other types in there, but it also feels a little off in cats-core and it isn't a collection so it doesn't feel quite right in cats-collections either. :shrug:
cc @typelevel/monoids