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

Implement local for Reader #2917

Merged
merged 1 commit into from
Jun 27, 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
2 changes: 2 additions & 0 deletions core/src/main/scala/cats/data/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ package object data {

object Reader {
def apply[A, B](f: A => B): Reader[A, B] = ReaderT[Id, A, B](f)

def local[A, R](f: R => R)(fa: Reader[R, A]): Reader[R, A] = Kleisli.local(f)(fa)
}

type Writer[L, V] = WriterT[Id, L, V]
Expand Down
14 changes: 14 additions & 0 deletions tests/src/test/scala/cats/tests/KleisliSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ class KleisliSuite extends CatsSuite {
kconfig1.run(config) should ===(kconfig2.run(config))
}

test("local for Reader") {
val rint1 = Reader { (x: Int) =>
x.toDouble
}
val rint1local = Reader.local((i: Int) => i * 2)(rint1)
val rint2 = Reader { (i: Int) =>
(i * 2).toDouble
}

val config = 10
rint1local.run(config) should ===(rint2.run(config))

}

test("flatMap is stack safe on repeated left binds when F is") {
val unit = Kleisli.pure[Eval, Unit, Unit](())
val count = if (Platform.isJvm) 10000 else 100
Expand Down