|
| 1 | +package io.catbird |
| 2 | +package util |
| 3 | + |
| 4 | +import cats.{ Alternative, Defer, Eq, Monad, Monoid, MonoidK, StackSafeMonad } |
| 5 | +import cats.instances.list._ |
| 6 | +import com.twitter.io.Reader |
| 7 | +import com.twitter.util.{ Await, Duration } |
| 8 | +import scala.collection.immutable.List |
| 9 | + |
| 10 | +/** Typeclass instances for [[com.twitter.io.Reader]] |
| 11 | + * |
| 12 | + * Note that while (to the best of my knowledge) these instances are lawful, Reader itself is inherently |
| 13 | + * unsafe: it is based on mutable state and also requires manual cleanup to ensure resource safety. To use |
| 14 | + * it in pure functional code, side-effecting operations should be wrapped in an effect type such as |
| 15 | + * [[Rerunnable]], and cleanup should be ensured using something like [[cats.effect.Resource]]. |
| 16 | + * |
| 17 | + * TODO: add facilities to make that easier to do. |
| 18 | + */ |
| 19 | +trait ReaderInstances { |
| 20 | + implicit final val readerInstance: Alternative[Reader] with Defer[Reader] with Monad[Reader] = |
| 21 | + new Alternative[Reader] with ReaderDefer with ReaderMonad with ReaderMonoidK |
| 22 | + |
| 23 | + implicit final def readerMonoid[A](implicit A: Monoid[A]) = new ReaderMonoid[A] |
| 24 | + |
| 25 | + /** |
| 26 | + * Obtain a [[cats.Eq]] instance for [[com.twitter.io.Reader]]. |
| 27 | + * |
| 28 | + * These instances use [[com.twitter.util.Await]] so should be |
| 29 | + * [[https://finagle.github.io/blog/2016/09/01/block-party/ avoided in production code]]. Likely use cases |
| 30 | + * include tests, scrips, REPLs etc. |
| 31 | + */ |
| 32 | + final def readerEq[A](atMost: Duration)(implicit A: Eq[A]): Eq[Reader[A]] = new Eq[Reader[A]] { |
| 33 | + final override def eqv(x: Reader[A], y: Reader[A]) = |
| 34 | + Await.result( |
| 35 | + Reader.readAllItems(x).joinWith(Reader.readAllItems(y))((xs, ys) => Eq[List[A]].eqv(xs.toList, ys.toList)), |
| 36 | + atMost |
| 37 | + ) |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/** Monad instance for twitter-util's Reader streaming abstraction |
| 42 | + * |
| 43 | + * Also entrant for "most confusing class name of the year" |
| 44 | + */ |
| 45 | +private[util] trait ReaderMonad extends StackSafeMonad[Reader] { |
| 46 | + final override def map[A, B](fa: Reader[A])(f: A => B): Reader[B] = fa.map(f) |
| 47 | + final override def pure[A](a: A): Reader[A] = Reader.value(a) |
| 48 | + final override def flatMap[A, B](fa: Reader[A])(f: A => Reader[B]): Reader[B] = fa.flatMap(f) |
| 49 | + final override def flatten[A](ffa: Reader[Reader[A]]): Reader[A] = ffa.flatten |
| 50 | +} |
| 51 | + |
| 52 | +private[util] trait ReaderDefer extends Defer[Reader] { |
| 53 | + /** Defer creation of a Reader |
| 54 | + * |
| 55 | + * There are a few ways to achieve this, such as using fromIterator on a lazy iterator, but this is the |
| 56 | + * simplest I've come up with. Might be worth benchmarking alternatives. |
| 57 | + */ |
| 58 | + def defer[A](fa: => Reader[A]): Reader[A] = Reader.flatten(Reader.value(() => fa).map(_())) |
| 59 | +} |
| 60 | + |
| 61 | +private[util] trait ReaderMonoidK extends MonoidK[Reader] { |
| 62 | + final override def empty[A]: Reader[A] = Reader.empty |
| 63 | + final override def combineK[A](x: Reader[A], y: Reader[A]): Reader[A] = Reader.concat(List(x, y)) |
| 64 | +} |
| 65 | + |
| 66 | +private[util] final class ReaderMonoid[A](implicit A: Monoid[A]) extends Monoid[Reader[A]] { |
| 67 | + final override def empty = Reader.value(A.empty) |
| 68 | + final override def combine(xs: Reader[A], ys: Reader[A]) = |
| 69 | + for(x <- xs; y <- ys) yield A.combine(x, y) |
| 70 | +} |
0 commit comments