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

Add state method to MonadState #1651

Merged
merged 5 commits into from
May 10, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 6 additions & 0 deletions core/src/main/scala/cats/ApplicativeError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ trait ApplicativeError[F[_], E] extends Applicative[F] {
case Success(a) => pure(a)
case Failure(e) => raiseError(e)
}

/**
* Convert from scala.Either
*/
def fromEither[A](x: E Either A): F[A] =
x.fold(raiseError, pure)
}

object ApplicativeError {
Expand Down
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/MonadState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ package cats
* }}}
*/
trait MonadState[F[_], S] extends Monad[F] {
def state[A](f: S => (S, A)): F[A] =
flatMap(get)(s => f(s) match { case (s, a) => inspect(_ => a)})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this use set to set the S resulting from f(s)? As is this will just return the initial S, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peterneyens Right. Forgot to update the state ( Fixed this!


def get: F[S]

def set(s: S): F[Unit]
Expand Down