Skip to content

Commit

Permalink
Implement local for Reader
Browse files Browse the repository at this point in the history
  • Loading branch information
jooohn committed Jun 27, 2019
1 parent 162ee0f commit 98358ac
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
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

0 comments on commit 98358ac

Please sign in to comment.