Navigation Menu

Skip to content

Commit

Permalink
Remove the Sync[Iterant] instance (#755)
Browse files Browse the repository at this point in the history
* Remove the Sync[Iterant] instance

* Remove junk and fix Scaladoc
  • Loading branch information
alexandru committed Oct 29, 2018
1 parent 4f1862c commit 42e8047
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 81 deletions.
75 changes: 8 additions & 67 deletions monix-tail/shared/src/main/scala/monix/tail/Iterant.scala
Expand Up @@ -21,7 +21,7 @@ import java.io.PrintStream

import cats.arrow.FunctionK
import cats.effect.{Async, Effect, Sync, _}
import cats.{Applicative, CoflatMap, Eq, Functor, Monoid, MonoidK, Order, Parallel}
import cats.{Applicative, CoflatMap, Defer, Eq, Functor, MonadError, Monoid, MonoidK, Order, Parallel, StackSafeMonad}

import scala.util.control.NonFatal
import monix.execution.internal.Platform.recommendedBatchSize
Expand Down Expand Up @@ -325,26 +325,6 @@ sealed abstract class Iterant[F[_], A] extends Product with Serializable {
final def bufferSliding(count: Int, skip: Int)(implicit F: Sync[F]): Iterant[F, Seq[A]] =
IterantBuffer.sliding(self, count, skip)

/** Implementation of `bracket` from `cats.effect.Bracket`.
*
* See [[https://typelevel.org/cats-effect/typeclasses/bracket.html documentation]].
*/
final def bracket[B](use: A => Iterant[F, B])(release: A => Iterant[F, Unit])
(implicit F: Sync[F]): Iterant[F, B] =
bracketCase(use)((a, _) => release(a))

/** Implementation of `bracketCase` from `cats.effect.Bracket`.
*
* See [[https://typelevel.org/cats-effect/typeclasses/bracket.html documentation]].
*/
final def bracketCase[B](use: A => Iterant[F, B])(release: (A, ExitCase[Throwable]) => Iterant[F, Unit])
(implicit F: Sync[F]): Iterant[F, B] = {

self.flatMap { a =>
use(a).guaranteeCase(release(a, _).completedL)
}
}

/** Builds a new iterant by applying a partial function to all
* elements of the source on which the function is defined.
*
Expand Down Expand Up @@ -2100,19 +2080,6 @@ object Iterant extends IterantInstances {
release)
}

/** DEPRECATED — please use [[Iterant.resource]].
*
* The `Iterant.bracket` operation was meant for streams, but
* this name in `Iterant` now refers to the semantics of the
* `cats.effect.Bracket` type class, implemented in
* [[Iterant!.bracket bracket]].
*/
@deprecated("Use Iterant.resource", since="3.0.0-RC2")
def bracket[F[_], A, B](acquire: F[A])
(use: A => Iterant[F, B], release: A => F[Unit])
(implicit F: Sync[F]): Iterant[F, B] =
resource(acquire)(release).flatMap(use)

/** Lifts a strict value into the stream context, returning a
* stream of one element.
*/
Expand Down Expand Up @@ -2662,39 +2629,25 @@ object Iterant extends IterantInstances {
}
}

private[tail] trait IterantInstances extends IterantInstances0 {
/** Provides the `cats.effect.Async` instance for [[Iterant]]. */
implicit def catsAsyncInstances[F[_]](implicit F: Async[F]): CatsAsyncInstances[F] =
new CatsAsyncInstances[F]()

/** Provides the `cats.effect.Async` instance for [[Iterant]]. */
class CatsAsyncInstances[F[_]](implicit F: Async[F])
extends CatsSyncInstances[F]
with Async[Iterant[F, ?]] {

override def async[A](k: (Either[Throwable, A] => Unit) => Unit): Iterant[F, A] =
Iterant.liftF(F.async(k))
override def asyncF[A](k: (Either[Throwable, A] => Unit) => Iterant[F, Unit]): Iterant[F, A] =
Iterant.liftF(F.asyncF(cb => k(cb).completedL))
override def never[A]: Iterant[F, A] =
Iterant.never
}
}

private[tail] trait IterantInstances0 {
private[tail] trait IterantInstances {
/** Provides the `cats.effect.Sync` instance for [[Iterant]]. */
implicit def catsSyncInstances[F[_]](implicit F: Sync[F]): CatsSyncInstances[F] =
new CatsSyncInstances[F]()

/** Provides the `cats.effect.Sync` instance for [[Iterant]]. */
class CatsSyncInstances[F[_]](implicit F: Sync[F])
extends Sync[Iterant[F, ?]]
extends StackSafeMonad[Iterant[F, ?]]
with MonadError[Iterant[F, ?], Throwable]
with Defer[Iterant[F, ?]]
with MonoidK[Iterant[F, ?]]
with CoflatMap[Iterant[F, ?]] {

override def pure[A](a: A): Iterant[F, A] =
Iterant.pure(a)

override def defer[A](fa: => Iterant[F, A]): Iterant[F, A] =
Iterant.suspend(fa)

override def map[A, B](fa: Iterant[F, A])(f: (A) => B): Iterant[F, B] =
fa.map(f)(F)

Expand Down Expand Up @@ -2739,17 +2692,5 @@ private[tail] trait IterantInstances0 {

override def recoverWith[A](fa: Iterant[F, A])(pf: PartialFunction[Throwable, Iterant[F, A]]): Iterant[F, A] =
fa.onErrorRecoverWith(pf)

override def suspend[A](thunk: => Iterant[F, A]): Iterant[F, A] =
Iterant.suspend(thunk)

override def delay[A](thunk: => A): Iterant[F, A] =
Iterant.eval(thunk)

override def bracket[A, B](acquire: Iterant[F, A])(use: A => Iterant[F, B])(release: A => Iterant[F, Unit]): Iterant[F, B] =
acquire.bracket(use)(release)

override def bracketCase[A, B](acquire: Iterant[F, A])(use: A => Iterant[F, B])(release: (A, ExitCase[Throwable]) => Iterant[F, Unit]): Iterant[F, B] =
acquire.bracketCase(use)(release)
}
}
Expand Up @@ -19,8 +19,7 @@ package monix.tail

import cats.Eq
import cats.data.EitherT
import cats.effect.laws.discipline.SyncTests
import cats.laws.discipline.{CoflatMapTests, MonadErrorTests, MonoidKTests, SemigroupalTests}
import cats.laws.discipline.{CoflatMapTests, DeferTests, MonadErrorTests, MonoidKTests, SemigroupalTests}
import monix.eval.Coeval

object TypeClassLawsForIterantCoevalSuite extends BaseLawsSuite {
Expand All @@ -34,9 +33,8 @@ object TypeClassLawsForIterantCoevalSuite extends BaseLawsSuite {
lazy val eqEitherT: Eq[EitherT[F, Throwable, Int]] =
implicitly[Eq[EitherT[F, Throwable, Int]]]

checkAllAsync("Sync[Iterant[Coeval]]") { implicit ec =>
implicit val eqE = eqEitherT
SyncTests[F].sync[Int, Int, Int]
checkAllAsync("Defer[Iterant[Coeval]]") { implicit ec =>
DeferTests[F].defer[Int]
}

checkAllAsync("MonadError[Iterant[Coeval]]") { implicit ec =>
Expand Down
Expand Up @@ -21,7 +21,7 @@ import cats.Eq
import cats.data.EitherT
import cats.effect.IO
import cats.effect.laws.discipline.AsyncTests
import cats.laws.discipline.{CoflatMapTests, MonadErrorTests, MonoidKTests, SemigroupalTests}
import cats.laws.discipline.{CoflatMapTests, DeferTests, MonadErrorTests, MonoidKTests, SemigroupalTests}
import monix.execution.schedulers.TestScheduler

object TypeClassLawsForIterantIOSuite extends BaseLawsSuite {
Expand All @@ -37,9 +37,8 @@ object TypeClassLawsForIterantIOSuite extends BaseLawsSuite {
lazy val eqEitherT: Eq[EitherT[F, Throwable, Int]] =
implicitly[Eq[EitherT[F, Throwable, Int]]]

checkAllAsync("Async[Iterant[IO]]", slowConfig) { _ =>
implicit val eqE = eqEitherT
AsyncTests[F].async[Int, Int, Int]
checkAllAsync("Defer[Iterant[IO]]") { implicit ec =>
DeferTests[F].defer[Int]
}

checkAllAsync("MonadError[Iterant[IO]]") { _ =>
Expand Down
Expand Up @@ -19,8 +19,7 @@ package monix.tail

import cats.Eq
import cats.data.EitherT
import cats.effect.laws.discipline.AsyncTests
import cats.laws.discipline.{CoflatMapTests, MonadErrorTests, MonoidKTests, SemigroupalTests}
import cats.laws.discipline.{CoflatMapTests, DeferTests, MonadErrorTests, MonoidKTests, SemigroupalTests}
import monix.eval.Task
import monix.execution.schedulers.TestScheduler

Expand All @@ -37,9 +36,8 @@ object TypeClassLawsForIterantTaskSuite extends BaseLawsSuite {
val eqEitherT: Eq[EitherT[F, Throwable, Int]] =
implicitly[Eq[EitherT[F, Throwable, Int]]]

checkAllAsync("Async[Iterant[Task]]", slowConfig) { _ =>
implicit val eqE = eqEitherT
AsyncTests[F].async[Int, Int, Int]
checkAllAsync("Defer[Iterant[Task]]", slowConfig) { _ =>
DeferTests[F].defer[Int]
}

checkAllAsync("MonadError[Iterant[Task]]") { _ =>
Expand Down

0 comments on commit 42e8047

Please sign in to comment.