Skip to content

Commit

Permalink
Optimize iterable instances implementation. (#3174)
Browse files Browse the repository at this point in the history
  • Loading branch information
takayahilton authored and kailuowang committed Dec 10, 2019
1 parent 2d7298c commit f651c2b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
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])
}

0 comments on commit f651c2b

Please sign in to comment.