arrow-kt / arrow-core

Λrrow Core is part of Λrrow, a functional companion to Kotlin's Standard Library
http://arrow-kt.io
Other
81 stars 30 forks source link

Deprecate Order #326

Closed nomisRev closed 3 years ago

nomisRev commented 3 years ago

This PR deprecates Order since Kotlin, and Kotlin's Std doesn't take Order into account nor does Kotlin have the power to automatically derive or inject Order anywhere. This makes Order very cumbersome to work with, see Tuple10Order.kt

Another issue with Order is that non of the data types, or collections out there take Order into account. For example SortedMap doesn't work with Order, but still relies on the existing Comparable or Comparator type.

Sadly when implementing operator fun A.compareTo it still doesn't make the type A Comparable<A>. It's easy to derive Comparator in case all involved types are Comparable<A> for example, val x: Comparator<Either<Int, Stringn>> = Comparator { a, b -> a.compareTo(b) }.

Usage examples:

2.right() <= 3.right() // <=, and other comparing symbols enabled through `compareTo`

val x: List<Either<String, Int>> = listOf(3.right(), 1.right(), 2.right())
x.sortedWith(Either<String, Int>::compareTo) // Derive `Comparator` from `compareTo`

maxOf(2.right(), 3.right(), Either<String, Int>::compareTo)
minOf(2.right(), 3.right(), Either<String, Int>::compareTo)
sort(2.right(), 3.right(), 1,right(), -5.right(), comparator = Either<String, Int>::compareTo)
...

We could also expose Either.comparator from Either's companion but it's almost as easy as just using a lambda reference to derive it fromcompareTo since Comparator is a @FunctionalInterface interface.

nomisRev commented 3 years ago

Turned this back into a draft since I need to update the already done deprecations for Order.