<?xml version="1.0" encoding="UTF-8"?>
<rss version='2.0' xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Blog of Alexander Semenov</title>
    <description>Who AM I?</description>
    <link>http://blog.alexander-semenov.com/feed</link>
    <atom:link href="http://blog.alexander-semenov.com/feed" rel="self" type="application/rss+xml"/>
    <category domain="blog.alexander-semenov.com">Content Management/Blog</category>
    <language>en-us</language>
      <pubDate>Sat, 22 Apr 2017 01:39:42 -1100</pubDate>
    <managingEditor>tvaroh@icloud.com (Blog of Alexander Semenov)</managingEditor>
      <item>
        <guid>http://blog.alexander-semenov.com/monadic-abstraction#31438</guid>
          <pubDate>Sat, 22 Apr 2017 01:39:42 -1100</pubDate>
        <link>http://blog.alexander-semenov.com/monadic-abstraction</link>
        <title>Structuring real-world code using monadic abstractions</title>
        <description></description>
        <content:encoded><![CDATA[<p>In this article I present a simple relatively boilerplate-free approach to structuring Scala code using <em>monadic abstractions</em>. I use <em>cats</em> but it can be done using any other library, like <em>Scalaz</em>.</p>

<h1 id="introduction">Introduction</h1>

<p>Presented approach is nothing new but just well-known patterns used in a cohesive way. In a sense it tries to compete with <em>free monads</em> or more modern <em>Eff monad</em> without going crazy too much. Those techniques are much more complicated and consequently more powerful, but they require a lot of boilerplate (even when using some libraries born to reduce it, like <em>Freek</em> or <em>Liberator</em> for free monads) and refactoring.</p>

<p>On the other hand, <em>monadic abstractions</em> approach in a sense is a less radical step forward to pure functional programming. It&#39;s much simpler and can be more easily applied to existing code that uses <code>scala.concurrent.Future</code>s directly, without re-writing it from scratch.</p>

<h1 id="step-by-step-guide-by-example">Step by step guide by example</h1>

<p>So, let&#39;s pretend we&#39;re writing a microservice that uses Scala futures for asynchronicity, <em>Slick 3</em> for relational database access, and something like <code>akka-http</code> to make REST calls to other microservices. First thing we do is...</p>

<h2 id="abstracting-interfaces">Abstracting interfaces</h2>

<p>Let&#39;s say we have a service layer and a repository layer. The first trick is to abstract away from concrete &quot;monad&quot; used as a methods result types wrapper, e.g. <code>Future</code> or <code>DBIO</code>, in interfaces:</p>

<p><code data-gist-id='57c54d734aed4f719c19109a7afddc93'> </code></p>

<p>Currently dominant style is to use <code>scala.concurrent.Future</code> as <code>M</code> in service trait and <code>scala.concurrent.Future</code> or <code>DBIO</code> as <code>M</code> in repository traits. There are two downsides with such approach:</p>

<ul>
<li>using futures in repositories would force to evaluate your <code>DBIO</code> actions too early (before leaving repository layer) effectively disallowing a transaction to span across multiple repository calls;</li>
<li>using <code>DBIO</code> as <code>M</code> in repository interfaces would leak repository layer implementation details to upper layers, adding `Slick* stuff to services classpath.</li>
</ul>

<p>So, by abstracting it out, we want to be able to run cross-repository calls transactions and fix the implementation details leak. And we don&#39;t really want to have <em>Slick</em> classes in service layer auto-complete!</p>

<p>We use higher order type parameter <code>M</code>, saying that we return a &quot;container&quot; of something, but we&#39;re not explicit about its type. It can be Scala <code>Future</code>, <em>Monix</em> <code>Task</code>, Slick&#39;s <code>DBIO</code>, cats&#39; <code>Eval</code>, or even <code>Function0</code>!</p>

<h2 id="implementing-repositories">Implementing repositories</h2>

<p>Next we implement repositories by using <code>DBIO</code> to substitute abstract <code>M</code>s:</p>

<p><code data-gist-id='eca5d80b48d2340c33adec688fe189ab'> </code></p>

<p>Implementation details aren&#39;t relevant here and therefore omitted for brevity. More interesting is how we implement the service.</p>

<h2 id="implementing-services">Implementing services</h2>

<p>Notice that service implementation should not depend on repository implementation but only on interfaces. We can say that each layer (e.g. repository, service, etc) has at least two parts, and consequently build artifacts: the <code>api</code> part and one or more <code>impl</code> parts. So each layer above only depends on <code>api</code> artifact of a layer below. Explicitly:</p>

<ul>
<li>service <code>api</code> doesn&#39;t depend on repository artifacts, cause it would make them available to layers above - something we what to disallow;</li>
<li>service <code>impl</code> depends on repository <code>api</code> (not <code>impl</code>!) so that repository implementation could be swapped without changing service code (ideally);</li>
<li>there is an <em>app</em> layer on the very top, where all the wiring takes place, it depends on both service and repository layers <code>impl</code> artifacts (and <code>api</code>s transitively) and provides a concrete repository layer implementation to the service layer.</li>
</ul>

<p>But how the service code looks then? Here comes the most interesting part:</p>

<p><code data-gist-id='5816e2c8f7e9e2c83d879ef967dc52d7'> </code></p>

<p>Let&#39;s iterate it one thing at a time.</p>

<h3 id="still-generic-m-_">Still generic <code>M[_]</code></h3>

<p>Notice that service code is still generic in the sense that it&#39;s detached from a concrete monad implementation, we&#39;re still using <code>M[_]</code>. And it&#39;s a good sign!</p>

<h3 id="dbeffect-type-parameter"><code>DbEffect</code> type parameter</h3>

<p>Additionally, our service implementation declares a type parameter to abstract over a concrete &quot;monad&quot; (or &quot;effect&quot;, or &quot;container&quot;) of the repository layer. We could call it just <code>D</code> but I like the <code>DbEffect</code> name as more self-explanatory here. We also parameterize our repositories dependencies with <code>DbEffect</code> effectively saying that we depend on repositories that produce values within a generic container called <code>DbEffect</code> - and that&#39;s all we know about them so far, but... stay tuned!</p>

<p>And yes - we&#39;re using constructor parameters for dependency injection.</p>

<h3 id="evaldb-argument"><code>evalDb</code> argument</h3>

<p>We&#39;ve also got a fancy looking <code>evalDb: DbEffect ~&gt; M</code> constructor parameter. It can be written more explicitly like this:</p>

<p><code data-gist-id='cd21c151be524a1e656155ead63d8b40'> </code></p>

<p>In cats&#39; code it&#39;s just a type alias for <code>FunctionK</code> trait:</p>

<p><code data-gist-id='d9af4e5b4896b99c128716f6f9dc09dc'> </code></p>

<p>This thing is called a <em>natural transformation</em> and all it can do is to convert a value of one generic type <code>F[_]</code> to another generic type <code>G[_]</code>. And that&#39;s exactly what we need to &quot;run&quot; our underlying <code>DbEffect</code>s: whatever monad service code uses for its results, we want to transform repository effects into it. For <code>DBIO</code> actions, when we pass <code>evalDb</code> implementation to our service in the <em>app</em> layer, we are going to execute them transactionally and get back futures (if we decide to use them as service&#39;s <code>M</code> implementation).</p>

<p>There is one aspect of <code>DbEffect</code> and <code>M</code> that gives compile-time guarantee of not having long-running transactions (what is probably a good idea). If you decide to do something like making REST call when transaction is running, you&#39;ll get a result of type <code>DbEffect[M[DbEffect[T]]]</code>. And you won&#39;t be able to escape out of it. Strictly speaking, there is a typeclass called <code>Traverse</code> that brings the ability to &quot;traverse&quot; one context with another, i.e. go from <code>F[G[T]]</code> to <code>G[F[T]]</code>, and since we can&#39;t have it for <code>DbEffect</code> and <code>M</code> (at least when <code>DbEffect</code> is <code>DBIO</code> and <code>M</code> is some asynchronous boundary like <code>Future</code>), - we cannot traverse it!</p>

<p>Now to the...</p>

<h3 id="implicit-parameters">Implicit parameters</h3>

<p>To recap, we require the following implicits when creating the service:</p>

<p><code data-gist-id='7a74622dd69eeccad8fa8d17cd68b2ed'> </code></p>

<p>In case you know what is a <code>Monad</code> (e.g. from my previous article) but never saw the <code>MonadError</code>, you can think about it like a <code>Monad</code> with built-in error handling. You can use standard Scala <code>Future</code> methods intuition, like <code>Future.failed</code>, <code>Future.recover</code>, or <code>Future.recoverWith</code>. They allow to create a failed value and to recover from a failure. Same for the <code>MonadError</code> typeclass but in much more abstract purely functional setting.</p>

<p>Why we require <code>MonadError</code> for <code>M</code> but <code>Monad</code> for <code>DbEffect</code>? It&#39;s just a rule of least power: I&#39;m not going to use error handling for <code>DbEffect</code> values in the method implementation so I just stick with the <code>Monad</code> instance for <code>DbEffect</code> but it could definitely be <code>MonadError[DbEffect, Throwable]</code> as well - no trickery here!</p>

<p>So, by having this implicit parameters we declare that:</p>

<ul>
<li><code>M</code> is a monad (with error handling) and consequently we can <code>map</code>/<code>flatMap</code>/<code>recover</code> values of type <code>M</code> - same way as one would use <code>Future</code>s in <em>for</em> comprehension;</li>
<li><code>DbEffect</code> is a monad, so we can chain <code>DbEffect</code> values as well, before transforming them to <code>M</code> using <code>evalDb</code> natural transformation (you can think about it like about transaction).</li>
</ul>

<h2 id="wiring-it-all-together">Wiring it all together</h2>

<p>Before actually implementing our service method, let&#39;s see how the components can be wired in the top <em>app</em> level. Here are some common Slick helpers to be used:</p>

<p><code data-gist-id='f2bf8fc1b196e402ed2b7cb9e162eb7d'> </code></p>

<p>I won&#39;t go into much detail here, this is something similar to what many teams come up with when using Slick. The most important thing is to be able to define <code>~&gt;</code> instance that can evaluate <code>DBIO</code> values. Notice that we&#39;re using same &quot;abstracting interfaces&quot; approach for shared common code as for the components above.</p>

<p>Now, wiring can look similar to this:</p>

<p><code data-gist-id='e1ee26fc5a68118ad8907331052e3da2'> </code></p>

<p>For simplicity I&#39;m not going to define web layer, but it might use <code>akka-http</code> and require services dependencies as <code>securityService: SecurityService[Future]</code>-like constructor parameters (or even <code>securityService: SecurityService[M]</code> if you decide to generify your controllers/resources/whatever-you-call-it as well, though there&#39;s less benefit from doing that in comparison to service code).</p>

<h3 id="but-what-about-monad-instances">But what about monad instances?</h3>

<p>An astute reader might notice that something is missing when creating the <code>SecurityServiceGenericImpl</code> above - namely the implicit <code>Monad</code> and <code>MonadError</code> instances:</p>

<ul>
<li><code>MonadError[Future]</code>;</li>
<li><code>Monad[DBIO]</code>.</li>
</ul>

<p>The first is defined in <em>cats</em> and can be put in scope by adding <code>import cats.implicits._</code> import.</p>

<p>As for the second one, there is a project on Github called <em>slick-cats</em> that defines <code>MonadError</code> and some other instances for <code>DBIO</code>. They can be brought in scope by introducing a dependency on <code>slick-cats</code> and adding <code>import com.rms.miu.slickcats.DBIOInstances._</code> import statement.</p>

<p>Finally we&#39;re ready to implement our example service method.</p>

<h2 id="service-method-implementation">Service method implementation</h2>

<p>Method logic is following:</p>

<ol>
<li>get user&#39;s password last changed date;</li>
<li>independently, get user&#39;s password validity period setting;</li>
<li>if password is expired, call some other microservice to send a notification email to the user;</li>
<li>if an error occurs during the notification sending, recover error to a specific <code>SendNotificationResult</code> ADT value.</li>
</ol>

<p>Even in this simplified example we have multiple things to consider, namely:</p>

<ul>
<li>we want to make first two calls independently on each other, i.e. no <code>flatMap</code>ing;</li>
<li>execute both calls in a single transaction (it might be more important in more real-world scenario than here);</li>
<li>return a <code>ErrorWhileSending</code> ADT value if an error occurs during notification sending (error handling);</li>
<li>though we imply that repository calls cannot fail in our example, we might want to provide <code>MonadError</code> instance for <code>DbEffect</code> and handle DB errors gracefully (not covered here).</li>
</ul>

<p>Here is the implementation split to several methods:</p>

<p><code data-gist-id='4ae13447951e7da3160af26da1d97d57'> </code></p>

<p>Steps 1 and 2 are implemented in <code>isPasswordExpired</code> method. Notice that it doesn&#39;t leave <code>DbEffect</code> monad yet, effectively causing both repository calls to execute in a single transaction, when run later by <code>evalDb</code>. This method uses applicative builder syntax to leverage the fact that <code>Monad</code> is also an <code>Applicative</code> functor. In a nutshell, <code>Applicative</code> is a less powerful abstraction that allows to, in this case, apply a function <code>(A, B) =&gt; C</code> to <code>F[A]</code> and <code>F[B]</code> to get <code>F[C]</code>. First, we use the fact that <code>DbEffect</code> is an <code>Applicative</code>, then the fact that <code>Option</code> is also an <code>Applicative</code> (typeclass instance for <code>Applicative[Option]</code> provided by cats).</p>

<p>The main difference between <code>Monad</code> and <code>Applicative</code> is that the former allows for computations chaining making one computation to depend on another, while the latter doesn&#39;t allow chaining and considers computations simultaneously, even with ability to run them in parallel when looking at how <code>Applicative[Future]</code> instance works. See cats documentation for more information.</p>

<p>In <code>sendNotificationIfPasswordExpired</code> method we <code>evalDb</code> the <code>isPasswordExpired</code> method result and, since it has <code>DbEffect[Option[Boolean]]</code> type, we can apply <code>OptionT</code> monad transformer to it. Monad transformers (hence the <code>T</code> in <code>OptionT</code>) allow to stack one monad on top of another and work with them without nesting. Again, google for <em>cats monads transformers</em> if needed. In <code>semiflatMap</code> method lambda we&#39;re already in <code>M</code> &quot;domain&quot; and we call <code>sendExpiredPasswordEmail</code> method, mapping it to <code>NotificationSent</code> ADT on success and recovering to <code>ErrorWhileSending</code> ADT on an error. In the end of the method, to get out of <code>OptionT</code> transformer, we call <code>getOrElse</code> method and return <code>UserNotFound</code> ADT if we were unable to get user data out of our repositories (any of two calls).</p>

<p><code>sendExpiredPasswordEmail</code> method implementation is supposed to make email sender microservice call, here we pretend it does it successfully and use <code>pure</code> monadic operation to wrap <code>Unit</code> value into monadic context.</p>

<h1 id="observations">Observations</h1>

<p>As you can see, we are very abstract about what monads we&#39;re using in the service implementation. This allows to decouple application layers and change our monads without touching the service code. If one day you decide to go for Monix&#39; <code>Task</code> instead of Scala <code>Future</code>, you won&#39;t need to change the service implementation at all. Just provide the required <code>Monad</code>/<code>MonadError</code> instances (and yes - Monix does have them!) and all&#39; keep working.</p>

<h2 id="applications-to-unit-testing">Applications to unit testing</h2>

<p>Last thing to touch upon is how this approach affects unit testing. Instead of dealing with asynchronous results in unit tests (even though <em>ScalaTest</em> has a decent support for them), you can instantiate the service by substituting <code>cats.Eval</code> as your <code>M</code> and <code>DbEffect</code> monads.</p>

<p><em>Notice that cats have <code>MonadError</code> instance for <code>Eval</code> in the master branch at the moment of writing, but it&#39;s not released yet.</em></p>

<p>Using <em>ScalaTest</em> and <em>Mockito</em>, you can do something like this:</p>

<p><code data-gist-id='1d854512ab041981383818e93e69a8cc'> </code></p>

<p>I use <code>FunctionK.id[Eval]</code> that&#39;s just a &quot;higher order identity&quot;(i.e. like Scala <code>identity</code> but applied to <code>M[_]</code>) to &quot;evaluate&quot; <code>DbEffect</code> values. So, same <code>Eval</code> monad for both service and repository layers in tests.</p>

<p>It&#39;s worth saying that this test reveals another problem with our implementation: invoking side-effective methods like <code>LocalDateTime.now()</code> in the service code. Instead, some date/time provider component should be passed to the service constructor to make the code more purely functional and consequently testable.</p>

<h2 id="wrapping-blocking-apis">Wrapping blocking APIs</h2>

<p>To wrap blocking APIs - those that return immediate values and do IO during their computation - one can define a transformation from <code>Eval</code> to whatever monad you use, e.g. <code>Future</code>:</p>

<p><code data-gist-id='599cce19862575298c40302366abf905'> </code></p>

<p>And then use it to transform blocking calls into <code>M</code>s:</p>

<p><code data-gist-id='da6e6413916058f97f1b57767c69ae53'> </code></p>

<h1 id="conclusion">Conclusion</h1>

<p>I hope you came away with something applicable to the real-world from the presented <em>monadic</em> approach to <em>abstract</em> and <em>structure</em> (are these two synonyms in a sense?) the code. You can imagine having more &quot;effects&quot; like <code>DbEffect</code> in your code and that&#39;s completely fine if you have required natural transformations machinery for them in place.</p>

<p>The thing I like the most about this approach is that it is <em>simple</em>. If you looked at free monads or Eff, you know what I mean. It doesn&#39;t force you to rewrite your code from scratch and can be adapted iteratively: first you apply it to the commons code, like <code>DatabaseSupport</code> in the above examples, then to app&#39;s bottom layers, then move higher to services, and so on.</p>

<p>Next logical step to investigate is how to abstract streaming - very important technique nowadays. It would be cumbersome to have <em>akka-http</em>, or <em>Monix</em>, or <em>Reactive Streams</em> classes along with <code>M</code> and <code>DbEffect</code>. But that&#39;s the topic of another article.</p>
]]></content:encoded>
      </item>
      <item>
        <guid>http://blog.alexander-semenov.com/monads#18751</guid>
          <pubDate>Wed, 14 Oct 2015 10:08:00 -1100</pubDate>
        <link>http://blog.alexander-semenov.com/monads</link>
        <title>Monads</title>
        <description>...from programmer&#39;s perspective</description>
        <content:encoded><![CDATA[<h1 id="what-is-a-monad">What is a monad</h1>

<p><em>Monad</em> is a trait with the following operations:</p>

<p><code data-gist-id='f0b602f273de7a0a642d8481e942e90d'> </code></p>

<p>Having such trait implementation for a type <code>M</code> makes it a monad <em>instance</em>.</p>

<p>Sometimes, <code>pure</code> is called <code>return</code> or <code>point</code>, whereas <code>bind</code> might instead be named <code>flatMap</code> (preferred in Scala) or <code>&gt;&gt;=</code> (Haskell&#39;s way). The method names are not so important, what&#39;s important is their signatures and what they do.</p>

<p><code>pure</code> and <code>bind</code> implementations must obey some easily google-able mathematical laws omitted here to make things less boring. </p>

<h2 id="m-type-parameter"><code>M</code> type parameter</h2>

<p>Type parameter of higher kind <code>M</code> requires a monad instance to be parameterized by a type with a single type parameter, e.g. <code>List</code>, <code>Future</code>, <code>Option</code>, <code>({ type λ[A] = Either[Throwable, A] })#λ</code>, or any type similar to:</p>

<p><code data-gist-id='13b335fb3fe66a90538670145bcdee5f'> </code></p>

<h2 id="pure-operation"><code>pure</code> operation</h2>

<p>In a nutshell, <code>pure</code> gives a way to wrap a value of type <code>A</code> into type <code>M</code> (sometimes called <em>monadic context</em>). E.g.: <code>List(a)</code>, <code>Future { a }</code>, <code>Some(a)</code>, <code>Right[Throwable, A](a)</code>. Put clearly: <code>pure</code> is <code>A =&gt; M[A]</code>, and that&#39;s it.</p>

<h2 id="bind-operation"><code>bind</code> operation</h2>

<p>The essence of this operation is to allow <em>flattening</em> of <code>M[M[A]]</code> to <code>M[A]</code>. This might be not clear immediately, given the above <code>Monad</code> trait definition, but that&#39;s easy to understand: having a value <code>ma: M[A]</code> and a function <code>f: A =&gt; M[B]</code>, to apply this function to <code>ma</code>, one needs a way to apply <code>f</code> to an <code>a: A</code> that&#39;s inside the context M, and the result will be of type <code>M[M[B]]</code>. So, <code>bind</code> allows to apply a function to monadic values and to flatten nested monadic contexts, while <code>pure</code> - to add new ones. For example, <code>List(a).flatMap(a =&gt; List(a))</code>, <code>Future { a } flatMap { a =&gt; Future { a } }</code>, <code>Some(a).flatMap(a =&gt; Some(a))</code>. These are artificial examples, but it&#39;s easy to imagine some nested conditional logic in the supplied <code>f</code> function.</p>

<h2 id="different-perspective-on-monad-trait">Different perspective on <code>Monad</code> trait</h2>

<p>It turns out, <code>Monad</code> trait might be formulated in a different way that is closer to the flattening intuition above:</p>

<p><code data-gist-id='32f4328730cc2732809b5edb848f8e8e'> </code></p>

<p>Here, <code>join</code> (or <code>flatten</code>) does exactly that. Also, we&#39;ve got a <code>map</code> operation which is needed to fill the &#39;gap&#39; from loosing the full power of <code>bind</code>. Those two definitions are equivalent: <code>bind</code> can be implemented in terms of <code>join</code> and <code>map</code>:</p>

<p><code data-gist-id='ff7228fc562bfb0580b4cbeeb4332710'> </code></p>

<p>or <code>map</code> and <code>join</code> in terms of <code>bind</code> and <code>pure</code>:</p>

<p><code data-gist-id='d0aee385db7228c7c4955ed42cf8adbc'> </code></p>

<h2 id="different-perspective-on-map-and-bind">Different perspective on <code>map</code> and <code>bind</code></h2>

<p>There is also an alternative view on <code>map</code> and <code>bind</code> methods signatures. Let&#39;s call &#39;em <code>lift</code> and <code>liftM</code> accordingly:</p>

<p><code data-gist-id='d23ef35af52a3cfac56a485953d4805f'> </code></p>

<p>Notice how similar they are. Both take a function, either ordinary <code>A =&gt; B</code> in case of <code>lift</code> or a monadic one <code>A =&gt; M[B]</code> (sometimes called <em>Kleisli arrow</em>) in case of <code>liftM</code>, and <em>lift</em> it to a function that works on monadic values. And these definitions are easily expressible in terms of original <code>map</code>/<code>bind</code>:</p>

<p><code data-gist-id='92b519e2397cec87f020a59d184bddf6'> </code></p>

<p>So, from this perspective, a monad can also be defined using <code>pure</code> and <code>liftM</code> or <code>pure</code>, <code>join</code>, and <code>lift</code>.</p>

<h2 id="the-essence-of-monads">The essence of monads</h2>

<p>From the dry definition above it might be not clear how monads can be useful, why they exist in the first place. So, to put it simply, monads give a way to chain computations (of type <code>A =&gt; M[B]</code>) while abstracting some details of how to exactly do that (extract values out of monadic context and flatten nested contexts) into the monad itself. For example, for <code>Lists</code>s it performs flattening, for <code>Option</code>s it stops on first <code>None</code>, for <code>Future</code>s - allows to make another asynchronous computation from within asynchronous computation, for some more esoteric <em>state</em> monad - to emulate mutable state in pure functional setting. This is what actually happens in Scala when using <em>for comprehension</em> syntax (though it doesn&#39;t force to implement any kind of trait, like the <code>Monad</code> above: just follow the method signatures, and all&#39; be fine):</p>

<p><code data-gist-id='f4ae9e58f4fdd7fb7f8ba8c066e8e612'> </code></p>

<p>And it&#39;s just a syntax sugar for:</p>

<p><code data-gist-id='b1d3d9a18500551a263a507259fc928e'> </code></p>

<p>Unlike the <code>Monad</code> trait definition above, monadic operations here are written in object-oriented style, but that doesn&#39;t change their meaning.</p>

<h2 id="some-monad-instances">Some monad instances</h2>

<p>A monad&#39;s essence is in how chaining (<code>bind</code> operation) is implemented. So, here some monads instances to make it all clearer:</p>

<p><code data-gist-id='bde280caf0354208b38ff7beb7a0be9f'> </code></p>

<p>Notice how partial type application is used for <code>Either</code> which has two type parameters while monad requires just one. In general, for types having more than one type parameter, one needs to <em>fix</em> all the parameters but one to make an, in fact, a new type that could potentially be a monad instance.</p>

<h1 id="conclusion">Conclusion</h1>

<p>Don&#39;t expect to understand the nature of monads just in one seat if you are new to them: you definitely need to play with various monads, see how they behave, implement some of them on your own. But the underlying idea is simple and is just a common programming practice: separate concerns and, as a consequence, prevent code duplication, by moving common stuff into a single easily maintainable and comprehensible place.</p>
]]></content:encoded>
      </item>
  </channel>
</rss>