-
Notifications
You must be signed in to change notification settings - Fork 24
Typeclass instances for Reader. #249
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
Open
dangerousben
wants to merge
1
commit into
typelevel:main
Choose a base branch
from
dangerousben:feature/reader
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
package io.catbird | ||
|
||
package object util extends FutureInstances with TryInstances with VarInstances with AsyncStreamInstances | ||
package object util | ||
extends AsyncStreamInstances | ||
with FutureInstances | ||
with ReaderInstances | ||
with TryInstances | ||
with VarInstances |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.catbird | ||
package util | ||
|
||
import cats.{ Alternative, Defer, Eq, Monad, Monoid, MonoidK, StackSafeMonad } | ||
import cats.instances.list._ | ||
import com.twitter.io.Reader | ||
import com.twitter.util.{ Await, Duration } | ||
import scala.collection.immutable.List | ||
|
||
/** Typeclass instances for [[com.twitter.io.Reader]] | ||
* | ||
* Note that while (to the best of my knowledge) these instances are lawful, Reader itself is inherently | ||
* unsafe: it is based on mutable state and also requires manual cleanup to ensure resource safety. To use | ||
* it in pure functional code, side-effecting operations should be wrapped in an effect type such as | ||
* [[Rerunnable]], and cleanup should be ensured using something like [[cats.effect.Resource]]. | ||
* | ||
* TODO: add facilities to make that easier to do. | ||
*/ | ||
trait ReaderInstances { | ||
implicit final val readerInstance: Alternative[Reader] with Defer[Reader] with Monad[Reader] = | ||
new Alternative[Reader] with ReaderDefer with ReaderMonad with ReaderMonoidK | ||
|
||
implicit final def readerMonoid[A](implicit A: Monoid[A]) = new ReaderMonoid[A] | ||
|
||
/** | ||
* Obtain a [[cats.Eq]] instance for [[com.twitter.io.Reader]]. | ||
* | ||
* These instances use [[com.twitter.util.Await]] so should be | ||
* [[https://finagle.github.io/blog/2016/09/01/block-party/ avoided in production code]]. Likely use cases | ||
* include tests, scrips, REPLs etc. | ||
*/ | ||
final def readerEq[A](atMost: Duration)(implicit A: Eq[A]): Eq[Reader[A]] = new Eq[Reader[A]] { | ||
final override def eqv(x: Reader[A], y: Reader[A]) = | ||
Await.result( | ||
Reader.readAllItems(x).joinWith(Reader.readAllItems(y))((xs, ys) => Eq[List[A]].eqv(xs.toList, ys.toList)), | ||
atMost | ||
) | ||
} | ||
} | ||
|
||
/** Monad instance for twitter-util's Reader streaming abstraction | ||
* | ||
* Also entrant for "most confusing class name of the year" | ||
*/ | ||
private[util] trait ReaderMonad extends StackSafeMonad[Reader] { | ||
final override def map[A, B](fa: Reader[A])(f: A => B): Reader[B] = fa.map(f) | ||
final override def pure[A](a: A): Reader[A] = Reader.value(a) | ||
final override def flatMap[A, B](fa: Reader[A])(f: A => Reader[B]): Reader[B] = fa.flatMap(f) | ||
final override def flatten[A](ffa: Reader[Reader[A]]): Reader[A] = ffa.flatten | ||
} | ||
|
||
private[util] trait ReaderDefer extends Defer[Reader] { | ||
/** Defer creation of a Reader | ||
* | ||
* There are a few ways to achieve this, such as using fromIterator on a lazy iterator, but this is the | ||
* simplest I've come up with. Might be worth benchmarking alternatives. | ||
*/ | ||
def defer[A](fa: => Reader[A]): Reader[A] = Reader.flatten(Reader.value(() => fa).map(_())) | ||
} | ||
|
||
private[util] trait ReaderMonoidK extends MonoidK[Reader] { | ||
final override def empty[A]: Reader[A] = Reader.empty | ||
final override def combineK[A](x: Reader[A], y: Reader[A]): Reader[A] = Reader.concat(List(x, y)) | ||
} | ||
|
||
private[util] final class ReaderMonoid[A](implicit A: Monoid[A]) extends Monoid[Reader[A]] { | ||
final override def empty = Reader.value(A.empty) | ||
final override def combine(xs: Reader[A], ys: Reader[A]) = | ||
for(x <- xs; y <- ys) yield A.combine(x, y) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.catbird | ||
package util | ||
|
||
import cats.Eq | ||
import cats.instances.boolean._ | ||
import cats.instances.int._ | ||
import cats.instances.tuple._ | ||
import cats.kernel.laws.discipline.MonoidTests | ||
import cats.laws.discipline.{ AlternativeTests, DeferTests, MonadTests } | ||
import com.twitter.io.Reader | ||
import com.twitter.conversions.DurationOps._ | ||
|
||
class ReaderSuite extends CatbirdSuite with ReaderInstances with ArbitraryInstances with EqInstances { | ||
implicit private def eqReader[A: Eq]: Eq[Reader[A]] = readerEq[A](1.second) | ||
|
||
checkAll("Reader[Int]", AlternativeTests[Reader].alternative[Int, Int, Int]) | ||
checkAll("Reader[Int]", DeferTests[Reader].defer[Int]) | ||
checkAll("Reader[Int]", MonadTests[Reader].monad[Int, Int, Int]) | ||
|
||
checkAll("Reader[Int]", MonoidTests[Reader[Int]].monoid) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to confirm, this and the two above are separate traits just for the sake of code organisation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mainly - I have a vague idea about trying to implement some of the cats-effect typeclasses for Reader (not actually sure if it's possible), and if so I'd want to be able to mix Defer and Monad in without MonoidK.