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

Adding reader method to MonadReader #1391

Merged
merged 1 commit into from
Sep 22, 2016
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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/MonadReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ trait MonadReader[F[_], R] extends Monad[F] {

/** Modify the environment */
def local[A](f: R => R)(fa: F[A]): F[A]

/** Retrieves a function of the environment */
def reader[A](f: R => A): F[A] = map(ask)(f)
}

object MonadReader {
Expand Down
3 changes: 3 additions & 0 deletions laws/src/main/scala/cats/laws/MonadReaderLaws.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ trait MonadReaderLaws[F[_], R] extends MonadLaws[F] {

def monadReaderLocalFlatMap[A, B](fra: F[A], f: A => F[B], g: R => R): IsEq[F[B]] =
F.local(g)(F.flatMap(fra)(f)) <-> F.flatMap(F.local(g)(fra))(a => F.local(g)(f(a)))

def monadReaderReaderAsk[A](f: R => A): IsEq[F[A]] =
F.reader(f) <-> F.map(F.ask)(f)
}

object MonadReaderLaws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ trait MonadReaderTests[F[_], R] extends MonadTests[F] {
"monadReader ask idempotent" -> laws.monadReaderAskIdempotent,
"monadReader local ask" -> forAll(laws.monadReaderLocalAsk _),
"monadReader local pure" -> forAll(laws.monadReaderLocalPure[A] _),
"monadReader local flatMap" -> forAll(laws.monadReaderLocalFlatMap[A, B] _)
"monadReader local flatMap" -> forAll(laws.monadReaderLocalFlatMap[A, B] _),
"monadReader reader ask" -> forAll(laws.monadReaderReaderAsk[A] _)
)
}
}
Expand Down