Skip to content

Commit

Permalink
Merge pull request #533 from ceedubs/foldable-toStreaming
Browse files Browse the repository at this point in the history
Add Foldable.toStreaming
  • Loading branch information
adelbertc committed Sep 15, 2015
2 parents 14b5772 + 5cdeabb commit e792851
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/Foldable.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package cats

import cats.data.Streaming

import scala.collection.mutable
import simulacrum.typeclass

Expand Down Expand Up @@ -206,6 +208,11 @@ import simulacrum.typeclass
val F = self
val G = ev
}

def toStreaming[A](fa: F[A]): Streaming[A] =
foldRight(fa, Now(Streaming.empty[A])){ (a, ls) =>
Now(Streaming.cons(a, ls))
}.value
}

/**
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/data/Streaming.scala
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,9 @@ object Streaming extends StreamingInstances {
loop(Empty(), as.reverse)
}

def fromFoldable[F[_], A](fa: F[A])(implicit F: Foldable[F]): Streaming[A] =
F.toStreaming(fa)

/**
* Create a stream from an iterable.
*
Expand Down Expand Up @@ -919,6 +922,9 @@ trait StreamingInstances extends StreamingInstances1 {

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

override def toStreaming[A](fa: Streaming[A]): Streaming[A] =
fa
}

implicit def streamOrder[A: Order]: Order[Streaming[A]] =
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/scala/cats/std/list.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package std
import algebra.Eq
import algebra.std.{ListMonoid, ListOrder}

import cats.data.Streaming
import cats.syntax.order._

import scala.annotation.tailrec
Expand Down Expand Up @@ -62,6 +63,9 @@ trait ListInstances extends ListInstances1 {
fa.forall(p)

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

override def toStreaming[A](fa: List[A]): Streaming[A] =
Streaming.fromList(fa)
}

implicit def listAlgebra[A]: Monoid[List[A]] = new ListMonoid[A]
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/std/vector.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cats
package std

import cats.data.Streaming

trait VectorInstances {
implicit val vectorInstance: Traverse[Vector] with MonadCombine[Vector] =
new Traverse[Vector] with MonadCombine[Vector] {
Expand Down Expand Up @@ -36,6 +38,9 @@ trait VectorInstances {
fa.exists(p)

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

override def toStreaming[A](fa: Vector[A]): Streaming[A] =
Streaming.fromVector(fa)
}

// TODO: eventually use algebra's instances (which will deal with
Expand Down
4 changes: 4 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class FoldableCheck[F[_]: ArbitraryK: Foldable](name: String) extends C
test("toList/isEmpty/nonEmpty") {
forAll { (fa: F[Int]) =>
fa.toList shouldBe iterator(fa).toList
fa.toStreaming.toList shouldBe iterator(fa).toList
fa.isEmpty shouldBe iterator(fa).isEmpty
fa.nonEmpty shouldBe iterator(fa).nonEmpty
}
Expand Down Expand Up @@ -94,5 +95,8 @@ class FoldableTestsAdditional extends CatsSuite {
if (n == 2) Now(true) else lb
}
assert(result.value)

// toStreaming should be lazy
assert(dangerous.toStreaming.take(3).toList == List(0, 1, 2))
}
}

0 comments on commit e792851

Please sign in to comment.