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

Optimize Iterable instances implementation. #3174

Merged
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
20 changes: 20 additions & 0 deletions alleycats-core/src/main/scala/alleycats/std/iterable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,25 @@ trait IterableInstances {

override def foldMap[A, B](fa: Iterable[A])(f: A => B)(implicit B: Monoid[B]): B =
B.combineAll(fa.iterator.map(f))

override def reduceLeftOption[A](fa: Iterable[A])(f: (A, A) => A): Option[A] = fa.reduceLeftOption(f)

override def collectFirst[A, B](fa: Iterable[A])(pf: PartialFunction[A, B]): Option[B] = fa.collectFirst(pf)

override def fold[A](fa: Iterable[A])(implicit A: Monoid[A]): A = fa.fold(A.empty)(A.combine)

override def find[A](fa: Iterable[A])(f: A => Boolean): Option[A] = fa.find(f)

override def toIterable[A](fa: Iterable[A]): Iterable[A] = fa

override def exists[A](fa: Iterable[A])(p: A => Boolean): Boolean = fa.exists(p)

override def forall[A](fa: Iterable[A])(p: A => Boolean): Boolean = fa.forall(p)

override def toList[A](fa: Iterable[A]): List[A] = fa.toList

override def isEmpty[A](fa: Iterable[A]): Boolean = fa.isEmpty

override def nonEmpty[A](fa: Iterable[A]): Boolean = fa.nonEmpty
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import cats._
import cats.instances.AllInstances
import cats.syntax.{AllSyntax, EqOps}
import cats.tests.StrictCatsEquality
import org.scalatest.Matchers
import org.scalatest.funsuite.AnyFunSuiteLike
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
import org.typelevel.discipline.scalatest.Discipline
import org.scalacheck.{Arbitrary, Gen}
import org.scalacheck.Arbitrary.arbitrary
import org.scalatest.matchers.should.Matchers

import scala.util.{Failure, Success, Try}

Expand All @@ -36,6 +36,8 @@ trait AlleycatsSuite
// disable Eq syntax (by making `catsSyntaxEq` not implicit), since it collides
// with scalactic's equality
override def catsSyntaxEq[A: Eq](a: A): EqOps[A] = new EqOps[A](a)

implicit def EqIterable[A: Eq]: Eq[Iterable[A]] = Eq.by(_.toList)
}

sealed trait TestInstances {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests

import cats.{Eval, Foldable}
import alleycats.std.all._
import cats.laws.discipline.FoldableTests

class IterableTests extends AlleycatsSuite {

Expand All @@ -22,4 +23,5 @@ class IterableTests extends AlleycatsSuite {
.value shouldEqual (Eval.now("OK").value)
}

checkAll("Foldable[Iterable]", FoldableTests[Iterable].foldable[Int, Int])
}