How to define local method on top of a monad transformer stack in Scala cats

Labra

I have a monad transformer stack on top of a Kleisli defined as:

type Env = Map[String,Int]
type MyState = List[Int]
type S[A] = EitherT[StateT[WriterT[Kleisli[List,Env,?],String,?],MyState,?], String, A]

and I want to define a local method with the following signature:

def localE[A](f: Env => Env)(sa: S[A]): S[A] = ???

Is it possible?

I know that there is a local method in MonadReader with the signature:

def local[A](f: R => R)(fa: F[A]): F[A]

So the simplest solution would be to obtain the MonadReader implicit from S, however, I could not find how to do it.

A simple snippet of my code would be the following:

package examples
import cats._, data._
import cats.implicits._

object local {
 type Env = Map[String,Int]
 type MyState = List[Int]
 type S[A] = EitherT[StateT[WriterT[Kleisli[List,Env,?],String,?],MyState,?], String, A]

 // The following definition doesn't compile
 // implicit lazy val mr = MonadReader[S,Env]

 // Modify the environment
 def localE[A](f: Env => Env)(sa: S[A]): S[A] = ???
}
Labra

A possible solution is to unlift the monad stack manually. In this case, I defined the following two auxiliary functions:

  def localW[A](f: Env => Env)
               (c: WriterT[Kleisli[List,Env,?],String,A]): 
         WriterT[Kleisli[List,Env,?],String,A] = {
   type WriterTF[F[_],A] = WriterT[F, String, A]
   val r: WriterT[Kleisli[List,Env,?],String,(String,A)] = 
      c.run.local(f).liftT[WriterTF]
   r.mapBoth { case (_, (w, x)) => (w, x) }
 }

   def localS[A](f: Env => Env)
                (c: StateT[WriterT[Kleisli[List,Env,?],String,?],MyState,A]): 
        StateT[WriterT[Kleisli[List,Env,?],String,?],MyState,A] = {
  StateT(s => localW(f)(c.run(s)))
  }

And the local function can be defined as:

  def localE[A](f: Env => Env)(sa: S[A]): S[A] = {
     EitherT(localS(f)(sa.value))
  }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to kickstart monad transformer stack from main?

From Dev

How to implement the `List` monad transformer in Scala?

From Dev

How to implement the `List` monad transformer in Scala?

From Dev

Monad Transformer stacks in Scala

From Dev

How do I add MonadLogger to my Free monad transformer stack?

From Dev

Unwrapping the STT monad in a transformer stack?

From Java

How is Kleisli a monad Transformer?

From Java

How to reason about stack safety in Scala Cats / fs2?

From Dev

Adding a monad transformer to the Yesod Handler stack

From Dev

Converting a nested type into a monad transformer stack

From Dev

Lift to fix the *inside* of a monad transformer stack

From Dev

Lift to fix the *inside* of a monad transformer stack

From Java

Scala, cats - how to create tagless-final implementation with IO (or other monad) and Either?

From Dev

How do I write a function which behaves differently depending on which monad is at the base of the transformer stack

From Dev

"No instance for MonadRandom" when using weightedSample in a monad transformer stack

From Dev

"No instance for MonadRandom" when using weightedSample in a monad transformer stack

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

How to refactor program that uses state monad transformer?

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

How to define an implicit converter to a Monad?

From Dev

How to use persistent in a monad stack?

From Dev

Monad transformer in Scala for comprehension to handle Option and collect error messages

From Dev

Monad transformer in Scala for comprehension to handle Option and collect error messages

From Dev

.NET CLI: How is a local variable popped off the stack if not on top?

From Dev

Cats Monad Transformers

From Dev

Accessing an instance method embedded in the monad stack

From Dev

Reader monad in Scala: return, local, and sequence

From Dev

How to implement Applicative for State[S, A] in scala / cats

From Dev

How to use class type passed as parameter to method to define local variables

Related Related

  1. 1

    How to kickstart monad transformer stack from main?

  2. 2

    How to implement the `List` monad transformer in Scala?

  3. 3

    How to implement the `List` monad transformer in Scala?

  4. 4

    Monad Transformer stacks in Scala

  5. 5

    How do I add MonadLogger to my Free monad transformer stack?

  6. 6

    Unwrapping the STT monad in a transformer stack?

  7. 7

    How is Kleisli a monad Transformer?

  8. 8

    How to reason about stack safety in Scala Cats / fs2?

  9. 9

    Adding a monad transformer to the Yesod Handler stack

  10. 10

    Converting a nested type into a monad transformer stack

  11. 11

    Lift to fix the *inside* of a monad transformer stack

  12. 12

    Lift to fix the *inside* of a monad transformer stack

  13. 13

    Scala, cats - how to create tagless-final implementation with IO (or other monad) and Either?

  14. 14

    How do I write a function which behaves differently depending on which monad is at the base of the transformer stack

  15. 15

    "No instance for MonadRandom" when using weightedSample in a monad transformer stack

  16. 16

    "No instance for MonadRandom" when using weightedSample in a monad transformer stack

  17. 17

    How to get ReaderT to work with another monad transformer?

  18. 18

    How to refactor program that uses state monad transformer?

  19. 19

    How to get ReaderT to work with another monad transformer?

  20. 20

    How to define an implicit converter to a Monad?

  21. 21

    How to use persistent in a monad stack?

  22. 22

    Monad transformer in Scala for comprehension to handle Option and collect error messages

  23. 23

    Monad transformer in Scala for comprehension to handle Option and collect error messages

  24. 24

    .NET CLI: How is a local variable popped off the stack if not on top?

  25. 25

    Cats Monad Transformers

  26. 26

    Accessing an instance method embedded in the monad stack

  27. 27

    Reader monad in Scala: return, local, and sequence

  28. 28

    How to implement Applicative for State[S, A] in scala / cats

  29. 29

    How to use class type passed as parameter to method to define local variables

HotTag

Archive