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 Align for Stream #3144

Merged
merged 2 commits into from
Nov 13, 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
14 changes: 11 additions & 3 deletions core/src/main/scala-2.12/cats/instances/stream.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package cats
package instances

import cats.data.ZipStream
import cats.data.{Ior, ZipStream}
import cats.syntax.show._

import scala.annotation.tailrec

trait StreamInstances extends cats.kernel.instances.StreamInstances {

implicit val catsStdInstancesForStream
: Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] =
new Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] {
: Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] with Align[Stream] =
new Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] with Align[Stream] {

def empty[A]: Stream[A] = Stream.Empty

Expand Down Expand Up @@ -151,6 +151,14 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {

override def collectFirstSome[A, B](fa: Stream[A])(f: A => Option[B]): Option[B] =
fa.collectFirst(Function.unlift(f))

def functor: Functor[Stream] = this

def align[A, B](fa: Stream[A], fb: Stream[B]): Stream[Ior[A, B]] =
alignWith(fa, fb)(identity)

override def alignWith[A, B, C](fa: Stream[A], fb: Stream[B])(f: Ior[A, B] => C): Stream[C] =
Align.alignWithIterator[A, B, C](fa, fb)(f).toStream
}

implicit def catsStdShowForStream[A: Show]: Show[Stream[A]] =
Expand Down
18 changes: 2 additions & 16 deletions core/src/main/scala-2.13+/cats/instances/lazyList.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,22 +135,8 @@ trait LazyListInstances extends cats.kernel.instances.LazyListInstances {
def align[A, B](fa: LazyList[A], fb: LazyList[B]): LazyList[Ior[A, B]] =
alignWith(fa, fb)(identity)

override def alignWith[A, B, C](fa: LazyList[A], fb: LazyList[B])(f: Ior[A, B] => C): LazyList[C] = {

val alignIterator = new Iterator[C] {
val iterA = fa.iterator
val iterB = fb.iterator
def hasNext: Boolean = iterA.hasNext || iterB.hasNext
def next(): C =
f(
if (iterA.hasNext && iterB.hasNext) Ior.both(iterA.next(), iterB.next())
else if (iterA.hasNext) Ior.left(iterA.next())
else Ior.right(iterB.next())
)
}

LazyList.from(alignIterator)
}
override def alignWith[A, B, C](fa: LazyList[A], fb: LazyList[B])(f: Ior[A, B] => C): LazyList[C] =
LazyList.from(Align.alignWithIterator[A, B, C](fa, fb)(f))
}

implicit def catsStdShowForLazyList[A: Show]: Show[LazyList[A]] =
Expand Down
14 changes: 11 additions & 3 deletions core/src/main/scala-2.13+/cats/instances/stream.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cats
package instances

import cats.data.ZipStream
import cats.data.{Ior, ZipStream}
import cats.syntax.show._

import scala.annotation.tailrec
Expand All @@ -10,8 +10,8 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {

@deprecated("Use cats.instances.lazyList", "2.0.0-RC2")
implicit val catsStdInstancesForStream
: Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] =
new Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] {
: Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] with Align[Stream] =
new Traverse[Stream] with Alternative[Stream] with Monad[Stream] with CoflatMap[Stream] with Align[Stream] {

def empty[A]: Stream[A] = Stream.Empty

Expand Down Expand Up @@ -152,6 +152,14 @@ trait StreamInstances extends cats.kernel.instances.StreamInstances {

override def collectFirstSome[A, B](fa: Stream[A])(f: A => Option[B]): Option[B] =
fa.collectFirst(Function.unlift(f))

def functor: Functor[Stream] = this

def align[A, B](fa: Stream[A], fb: Stream[B]): Stream[Ior[A, B]] =
alignWith(fa, fb)(identity)

override def alignWith[A, B, C](fa: Stream[A], fb: Stream[B])(f: Ior[A, B] => C): Stream[C] =
Stream.from(Align.alignWithIterator[A, B, C](fa, fb)(f))
}

@deprecated("Use cats.instances.lazyList", "2.0.0-RC2")
Expand Down
13 changes: 13 additions & 0 deletions core/src/main/scala/cats/Align.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,17 @@ object Align {
def semigroup[F[_], A](implicit F: Align[F], A: Semigroup[A]): Semigroup[F[A]] = new Semigroup[F[A]] {
def combine(x: F[A], y: F[A]): F[A] = Align[F].alignCombine(x, y)
}

private[cats] def alignWithIterator[A, B, C](fa: Iterable[A], fb: Iterable[B])(f: Ior[A, B] => C): Iterator[C] =
new Iterator[C] {
private[this] val iterA = fa.iterator
private[this] val iterB = fb.iterator
def hasNext: Boolean = iterA.hasNext || iterB.hasNext
def next(): C =
f(
if (iterA.hasNext && iterB.hasNext) Ior.both(iterA.next(), iterB.next())
else if (iterA.hasNext) Ior.left(iterA.next())
else Ior.right(iterB.next())
)
}
}
4 changes: 4 additions & 0 deletions tests/src/test/scala/cats/tests/StreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cats
package tests

import cats.laws.discipline.{
AlignTests,
AlternativeTests,
CoflatMapTests,
CommutativeApplyTests,
Expand Down Expand Up @@ -34,6 +35,9 @@ class StreamSuite extends CatsSuite {
checkAll("Stream[Int]", TraverseFilterTests[Stream].traverseFilter[Int, Int, Int])
checkAll("TraverseFilter[Stream]", SerializableTests.serializable(TraverseFilter[Stream]))

checkAll("Stream[Int]", AlignTests[Stream].align[Int, Int, Int, Int])
checkAll("Align[Stream]", SerializableTests.serializable(Align[Stream]))

// Can't test applicative laws as they don't terminate
checkAll("ZipStream[Int]", CommutativeApplyTests[ZipStream].apply[Int, Int, Int])

Expand Down