Skip to content

Commit

Permalink
Add size to Foldable
Browse files Browse the repository at this point in the history
This addresses part of what is proposed in #1091.
  • Loading branch information
Andrea Fiore committed Jun 11, 2016
1 parent 5cf9da3 commit fe64eaf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats

import scala.collection.mutable
import cats.std.long._
import simulacrum.typeclass

/**
Expand Down Expand Up @@ -56,6 +57,8 @@ import simulacrum.typeclass
}
}

def size[A](fa: F[A]): Long = foldMap(fa)(_ => 1)

/**
* Fold implemented using the given Monoid[A] instance.
*/
Expand Down
6 changes: 6 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ abstract class FoldableCheck[F[_]: Foldable](name: String)(implicit ArbFInt: Arb

def iterator[T](fa: F[T]): Iterator[T]

test("size") {
forAll { (fa: F[Int]) =>
fa.size should === (iterator(fa).size.toLong)
}
}

test("summation") {
forAll { (fa: F[Int]) =>
val total = iterator(fa).sum
Expand Down

0 comments on commit fe64eaf

Please sign in to comment.