Encapsulate series of steps within a Monad
<p>In trying to understand Monads as "a structure that represents computations defined as sequences of steps" : <a href="https://en.wikipedia.org/wiki/Monad_(functional_programming)" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Monad_(functional_programming)</a></p>

<p>This example : </p>

<pre><code>def optionAdd(x:Option[Int], y:Option[Int]):Option[Int] = 
  for(xx &lt;- x; yy &lt;- y) yield xx+yy 
</code></pre>

<p>taken from <a href="https://stackoverflow.com/questions/15095588/scala-monad-full-worked-example">Scala Monad - Full worked example</a>. This monad concatenates two Strings in a predefined order.</p>

<p>I'm attempting to wrap these steps within a monad : </p>

<pre><code> 1. Get String to be converted to html
 2. Perform series of replace statements on the String to create html compliant String



  def getString = {
    "def test() \n { }"
  }                                               //&gt; getString: =&gt; String
  val stringOption = Option(getString);           //&gt; stringOption  : Option[String] = Some(def test() 
                                                  //|  { })
  def createHtmlMonad(x: Option[String]): Option[String] =
    Option(x.get.replace("\n", "&lt;br&gt;").replace("def", "&lt;div style=\"color:red\" &lt;/div&gt;"));
                                                  //&gt; createHtmlMonad: (x: Option[String])Option[String]
  val h = createHtmlMonad(stringOption);          //&gt; h  : Option[String] = Some(&lt;div style="color:red" &lt;/div&gt; test() &lt;br&gt; { })
</code></pre>

<p>The method createHtmlMonad is not a Monad ? Can this functionality be encapsulated within a Monad ?</p>

<p>I could just create a new method that encapsulates the series of steps but this is not a Monad : </p>

<pre><code>    def getString = {
    "def test() \n { }"
  }                                               //&gt; getString: =&gt; String

  def stepsToCreateHtml(stringOption: Option[String]) = {

    val replaceBr = Option(stringOption.get.replace("\n", "&lt;br&gt;"))
    val replaceStyle = Option(replaceBr.get.replace("def", "&lt;div style=\"color:red\" &lt;/div&gt;"))

    replaceStyle
  }                                               //&gt; stepsToCreateHtml: (stringOption: Option[String])Option[String]
  val stringOption = Option(getString);           //&gt; stringOption  : Option[String] = Some(def test() 
                                                  //|  { })

  val html = stepsToCreateHtml(stringOption);     //&gt; html  : Option[String] = Some(&lt;div style="color:red" &lt;/div&gt; test() &lt;br&gt; { })
                                                  //| 
</code></pre>

<p>Update : 
I think this helps to answer the question. Instead of creating a new monad I re-use the existing Option monad : </p>

<pre><code>object t extends App {

  println(parseService.getHtml("").flatMap(convertBr).flatMap(convertStyle))

  def convertBr = (str: String) =&gt; Option(str.replaceAll("\n", "&lt;br&gt;"))
  def convertStyle = (str: String) =&gt; Option(str.replaceAll("def", "&lt;div style= color : \"red\"&gt;&lt;/div&gt;"))

}

object parseService {
  def getHtml(str: String): Some[String] = {
    Some("test \n newline def")
  }
}
</code></pre>

<p>This is based on <a href="https://medium.com/@sinisalouc/demystifying-the-monad-in-scala-cc716bb6f534#.xprt8msoc" rel="nofollow noreferrer">https://medium.com/@sinisalouc/demystifying-the-monad-in-scala-cc716bb6f534#.xprt8msoc</a> . The monad itself is just a wrapper, the value is how it is composed with the functionality to be implemented, in this case I use the Option monad to compase appending of new Strings. I still do not fully understand monads but feel a little closer.</p>