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

ziggystar

I have a monad that is very similar to a collection monad. I'm currently trying to implement a monad transformer for it, but I'm failing.

I've looked at the ListT implementation in Scalaz 6 and 7, but I cannot understand how it works. It uses some additional type Step, whose purpose is unclear to me.

So can someone please explain to me how to implement a list monad transformer, either by explaining the Scalaz approach or using a different implementation?

drexin

I am not quite sure, what the Step means in scalaz, but implementing a ListT is pretty straight forward. Depending on how many operations you want to put on it, it can be a little work, but the basic monad operations can be implemented as follows.

First we need typeclasses for monad and functor (we could also add applicative, but that is not necessary for this example):

trait Functor[F[_]] {
  def map[A,B](fa: F[A])(f: A => B): F[B]
}

trait Monad[F[_]] extends Functor[F] {
  def flatMap[A,B](fa: F[A])(f: A => F[B]): F[B]
  def pure[A](x: A): F[A]
}

object Monad {

  implicit object ListMonad extends Monad[List] {
    def map[A,B](fa: List[A])(f: A => B) = fa map f
    def flatMap[A,B](fa: List[A])(f: A => List[B]) = fa flatMap f
    def pure[A](x: A) = x :: Nil
  }

  implicit object OptionMonad extends Monad[Option] {
    def map[A,B](fa: Option[A])(f: A => B) = fa map f
    def flatMap[A,B](fa: Option[A])(f: A => Option[B]) = fa flatMap f
    def pure[A](x: A) = Some(x)
  }

  def apply[F[_] : Monad]: Monad[F] = implicitly[Monad[F]]

}

Once we have those, we can create the transformer, which basically just wraps the F[List[A]] and forwards the call to its map and flatMap function to the list by calling map on the containing functor and then calling map or flatMap resp. on the contained List/s.

final case class ListT[F[_] : Monad, A](fa: F[List[A]]) {
  def map[B](f: A => B) = ListT(Monad[F].map(fa)(_ map f))

  def flatMap[B](f: A => ListT[F, B]) = ListT(Monad[F].flatMap(fa) { _ match {
    case Nil => Monad[F].pure(List[B]())
    case list => list.map(f).reduce(_ ++ _).run
  }})

  def ++(that: ListT[F,A]) = ListT(Monad[F].flatMap(fa) { list1 =>
    Monad[F].map(that.run)(list1 ++ _)
  })

  def run = fa
}

Once we are done with modifying, we can get the resulting object by calling the run method on the ListT object. If you want, you can also add other list specific operations like in scalaz. This should be pretty straight forward. For example a :: could look as follows:

def ::(x: A) = ListT(Monad[F].map(fa)(x :: _))

Usage:

scala> ListT(Option(List(1,2,3)))
res6: ListT[Option,Int] = ListT(Some(List(1, 2, 3)))

scala> res6.map(_+45)
res7: ListT[Option,Int] = ListT(Some(List(46, 47, 48)))

scala> 13 :: res7
res8: ListT[Option,Int] = ListT(Some(List(13, 46, 47, 48)))

scala> res8.run
res10: Option[List[Int]] = Some(List(13, 46, 47, 48))

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 implement the `List` monad transformer in Scala?

From Dev

Monad Transformer stacks in Scala

From Dev

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

From Java

How is Kleisli a monad Transformer?

From Dev

How to implement a short-circuit with IO monad in Scala

From Dev

Use List as monad in Scala

From Dev

Use list monad inside monad transformer type classes?

From Dev

Scala: How do I compose a list of operations into a free monad?

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 kickstart monad transformer stack from main?

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

How is List a monad?

From Dev

How to use a Monad Transformer when Disjunction is the outermost container?

From Dev

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

From Java

Is there a valid array monad transformer?

From Dev

Stacking ResourceT monad transformer

From Dev

WriterT monad transformer

From Dev

How to implement a reader monad to access a database

From Dev

Subclass list in scala to implement graph

From Dev

How to implement "unescape" in Scala?

From Dev

Unwrapping the STT monad in a transformer stack?

From Dev

Monad Transformer stacks with MaybeT and RandT

From Dev

Working with monad transformer RWST in Haskell

From Dev

Deriving a base monad using a monad transformer and the identity monad

From Dev

How to build a Haskell list inside a monad lazily?

From Dev

How to combine List and State Monad in Haskell

Related Related

  1. 1

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

  2. 2

    Monad Transformer stacks in Scala

  3. 3

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

  4. 4

    How is Kleisli a monad Transformer?

  5. 5

    How to implement a short-circuit with IO monad in Scala

  6. 6

    Use List as monad in Scala

  7. 7

    Use list monad inside monad transformer type classes?

  8. 8

    Scala: How do I compose a list of operations into a free monad?

  9. 9

    How to get ReaderT to work with another monad transformer?

  10. 10

    How to refactor program that uses state monad transformer?

  11. 11

    How to get ReaderT to work with another monad transformer?

  12. 12

    How to kickstart monad transformer stack from main?

  13. 13

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

  14. 14

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

  15. 15

    How is List a monad?

  16. 16

    How to use a Monad Transformer when Disjunction is the outermost container?

  17. 17

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

  18. 18

    Is there a valid array monad transformer?

  19. 19

    Stacking ResourceT monad transformer

  20. 20

    WriterT monad transformer

  21. 21

    How to implement a reader monad to access a database

  22. 22

    Subclass list in scala to implement graph

  23. 23

    How to implement "unescape" in Scala?

  24. 24

    Unwrapping the STT monad in a transformer stack?

  25. 25

    Monad Transformer stacks with MaybeT and RandT

  26. 26

    Working with monad transformer RWST in Haskell

  27. 27

    Deriving a base monad using a monad transformer and the identity monad

  28. 28

    How to build a Haskell list inside a monad lazily?

  29. 29

    How to combine List and State Monad in Haskell

HotTag

Archive