Skip to content
This repository has been archived by the owner on Jun 9, 2020. It is now read-only.

Add cats.functor.Invariant instance for fs2.async.mutable.Queue #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 17 additions & 2 deletions src/main/scala/fs2/interop/cats/Instances.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package fs2.interop.cats

import fs2.util._
import fs2.async.immutable.Signal
import fs2.async.mutable.Queue
import _root_.cats.{ Eval, Functor => CatsFunctor, Monad => CatsMonad, MonadError }
import _root_.cats.arrow.NaturalTransformation
import _root_.cats.functor.Invariant

trait Instances extends Instances0 {
implicit def effectToMonadError[F[_]](implicit F: Effect[F]): MonadError[F, Throwable] = new MonadError[F, Throwable] {
Expand All @@ -17,6 +20,20 @@ trait Instances extends Instances0 {
implicit def uf1ToNatrualTransformation[F[_], G[_]](implicit uf1: UF1[F, G]): NaturalTransformation[F, G] = new NaturalTransformation[F, G] {
def apply[A](fa: F[A]) = uf1(fa)
}

implicit def catsInvariantQueue[F[_]](implicit F: Functor[F]): Invariant[Queue[F, ?]] =
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what are the implication of using Functor vs CatsFunctor here...

new Invariant[Queue[F, ?]] {
def imap[A, B](fa: Queue[F, A])(f: A => B)(g: B => A): Queue[F, B] =
new Queue[F, B] {
def available: Signal[F, Int] = fa.available
def dequeue1: F[B] = F.map(fa.dequeue1)(f)
def enqueue1(a: B): F[Unit] = fa.enqueue1(g(a))
def full: Signal[F, Boolean] = fa.full
def offer1(a: B): F[Boolean] = fa.offer1(g(a))
def size: Signal[F, Int] = fa.size
def upperBound: Option[Int] = fa.upperBound
}
}
}

private[cats] trait Instances0 extends Instances1 {
Expand All @@ -38,9 +55,7 @@ private[cats] trait Instances1 extends Instances2 {
}

private[cats] trait Instances2 {

implicit def functorToCats[F[_]](implicit F: Functor[F]): CatsFunctor[F] = new CatsFunctor[F] {
def map[A, B](fa: F[A])(f: A => B) = F.map(fa)(f)
}
}