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

Add minimumBy/maximumBy/Option to Foldable #3084

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,34 @@ import Foldable.sentinel
def maximumOption[A](fa: F[A])(implicit A: Order[A]): Option[A] =
reduceLeftOption(fa)(A.max)

/**
* Find the minimum `A` item in this structure according to an `Order.by(f)`.
*
* @return `None` if the structure is empty, otherwise the minimum element
* wrapped in a `Some`.
*
* @see [[Reducible#minimum]] for a version that doesn't need to return an
* `Option` for structures that are guaranteed to be non-empty.
*
* @see [[maximumOptionBy]] for maximum instead of minimum.
*/
def minimumOptionBy[A, B: Order](fa: F[A])(f: A => B)(implicit F: Foldable[F]): Option[A] =
F.minimumOption(fa)(Order.by(f))

/**
* Find the maximum `A` item in this structure according to an `Order.by(f)`.
*
* @return `None` if the structure is empty, otherwise the maximum element
* wrapped in a `Some`.
*
* @see [[Reducible#maximum]] for a version that doesn't need to return an
* `Option` for structures that are guaranteed to be non-empty.
*
* @see [[minimumOptionBy]] for minimum instead of maximum.
*/
def maximumOptionBy[A, B: Order](fa: F[A])(f: A => B)(implicit F: Foldable[F]): Option[A] =
F.maximumOption(fa)(Order.by(f))

/**
* Get the element at the index of the `Foldable`.
*/
Expand Down
16 changes: 16 additions & 0 deletions core/src/main/scala/cats/Reducible.scala
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ import simulacrum.typeclass
def maximum[A](fa: F[A])(implicit A: Order[A]): A =
reduceLeft(fa)(A.max)

/**
* Find the minimum `A` item in this structure according to an `Order.by(f)`.
*
* @see [[maximumBy]] for maximum instead of minimum.
*/
def minimumBy[A, B: Order](fa: F[A])(f: A => B)(implicit F: Reducible[F]): A =
F.minimum(fa)(Order.by(f))

/**
* Find the maximum `A` item in this structure according to an `Order.by(f)`.
*
* @see [[minimumBy]] for minimum instead of maximum.
*/
def maximumBy[A, B: Order](fa: F[A])(f: A => B)(implicit F: Reducible[F]): A =
F.maximum(fa)(Order.by(f))

/**
* Intercalate/insert an element between the existing elements while reducing.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,20 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
}
}

test(s"Foldable[$name].maximumBy/minimumBy") {
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry I should've mentioned this in the earlier comment. Can we make these laws like the other _Ref laws in Foldable Laws and Reducible laws?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmm I'm not sure what you mean 🤔 couldn't find _Ref anywhere in the file.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah ok I found them. Ok I will add some laws. Should I keep the tests in the suite?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think having them in law is enough. sorry I should've been clear about where the law code is.

Copy link
Member Author

@joroKr21 joroKr21 Sep 29, 2019

Choose a reason for hiding this comment

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

Hmm, if I want to add tests for minimum/maximum to FoldableTests that would mean adding an Order constraint on B in def foldable[A: Arbitrary, B: Arbitrary], which is not binary compatible.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, right. Then we can't add to laws. We can only use the current tests

forAll { (fa: F[Int], f: Int => Int) =>
val maxOpt = fa.maximumOptionBy(f).map(f)
val minOpt = fa.minimumOptionBy(f).map(f)
val nelOpt = fa.toList.toNel
maxOpt should ===(nelOpt.map(_.maximumBy(f)).map(f))
maxOpt should ===(nelOpt.map(_.toList.maxBy(f)).map(f))
minOpt should ===(nelOpt.map(_.minimumBy(f)).map(f))
minOpt should ===(nelOpt.map(_.toList.minBy(f)).map(f))
maxOpt.forall(i => fa.forall(f(_) <= i)) should ===(true)
minOpt.forall(i => fa.forall(f(_) >= i)) should ===(true)
}
}

test(s"Foldable[$name].reduceLeftOption/reduceRightOption") {
forAll { (fa: F[Int]) =>
val list = fa.toList
Expand Down