Creating a monad for repeated methods

I am trying to figure out which monads are in Scala.

Let's say, for example, I have methods in Java:

public void doSomeThing()
{
   a.call();
}

public void doOtherThing()
{
   a2.call();
}

public void doSomeOtherThing()
{
   a3.call();
}

      

All methods starting with do

, just call the method call with the name call

. If I convert this to a generic method in Java like:

public void doGeneric(SomeClass a)
{
   a.call();
}

      

and say it as a generic method, with a function call

, then doGeneric

is it a monad? Correct me if I am wrong.

Please note that I have provided examples in Java since I am just getting started with Scala.

+3


source to share


1 answer


The monad is associated with the concept of "container", for example Option

or List

.
Basically, the container that defines a method flatMap

is a monad:

trait Monad[A] { //let say a Monad container to be general. A well-known would be Option
  def flatMap[B](f: A => Monad[B]): Monad[B]
}

      



In your case, you are just calling a method on one object.
You are not calculating the value "container" from another container, so you don't have a monad.
Indeed, you are just using polymorphism.

+1


source







All Articles