<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: BlackOpsREPL</title>
    <description>The latest articles on DEV Community by BlackOpsREPL (@blackopsrepl).</description>
    <link>https://dev.to/blackopsrepl</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2638639%2F8e61e3ec-a529-4454-b9eb-126dd4919c22.png</url>
      <title>DEV Community: BlackOpsREPL</title>
      <link>https://dev.to/blackopsrepl</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/blackopsrepl"/>
    <language>en</language>
    <item>
      <title>A monad is a monoid in the category of endofunctors...</title>
      <dc:creator>BlackOpsREPL</dc:creator>
      <pubDate>Thu, 02 Jan 2025 13:04:08 +0000</pubDate>
      <link>https://dev.to/blackopsrepl/a-monad-is-a-monoid-in-the-category-of-endofunctors-47oi</link>
      <guid>https://dev.to/blackopsrepl/a-monad-is-a-monoid-in-the-category-of-endofunctors-47oi</guid>
      <description>&lt;p&gt;What is a Monad?&lt;/p&gt;

&lt;p&gt;A monad is a &lt;strong&gt;monoid&lt;/strong&gt; in the category of &lt;strong&gt;endofunctors&lt;/strong&gt;... 😎&lt;/p&gt;

&lt;p&gt;...let's try again:&lt;/p&gt;

&lt;p&gt;A monad is an abstraction used in functional programming to handle computations in a consistent way. It is a design pattern that allows the chaining of operations while encapsulating side effects, such as state changes, I/O, or errors, in a controlled manner.&lt;/p&gt;

&lt;p&gt;Monads can be thought of as a way to wrap values and apply functions to them, ensuring that computations are carried out in a predictable and controlled environment.&lt;/p&gt;

&lt;p&gt;My personal understanding is: a monad is a data structure with&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a single, immutable value (or function)&lt;/li&gt;
&lt;li&gt;a map/bind function to map a function to that value&lt;/li&gt;
&lt;li&gt;a return/flatten function that returns the new state into a new monad each time&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This enables chaining and composition of operations in a way that preserves immutability and functional purity—a "reverse" approach to a class, where state cannot be modified in-place, but transformations are applied functionally to produce new instances.&lt;/p&gt;

&lt;p&gt;This "reverse symmetry" highlights the contrast:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Classes modify their internal state, maintaining mutability and direct interaction through internal methods. They most of the time interact with a global state&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Monads reach similar outcomes to a class, but by wrapping values and mapping external immutable transformations, they have no side-effects: there is no global state&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Class:
    A Box class holds a value, and you can modify the value by calling methods like setValue(newValue), which mutates the internal state of the Box.&lt;/li&gt;
&lt;li&gt;Monad:
    A BoxMonad would hold a value in an immutable way. To transform the value, you would use a function like map, which applies an external function to the value and returns a new BoxMonad, with the new value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example project in Python:&lt;br&gt;
&lt;a href="https://github.com/blackopsrepl/py-monad-task-manager" rel="noopener noreferrer"&gt;https://github.com/blackopsrepl/py-monad-task-manager&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this application, we use the State Monad to manage the state of the task list. The State Monad provides a way to model the task management process as a series of transformations on the application's state, while keeping the state itself immutable.&lt;/p&gt;

&lt;p&gt;Instead of directly modifying the state of the task list, we use the return_ function to wrap the initial state into a monadic container, and the bind function to apply transformations to the state in a predictable way. Each time an operation is performed (such as adding, editing, or deleting tasks), it returns a new instance of the state, ensuring immutability and composability.&lt;/p&gt;

&lt;p&gt;Key functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;return_: Wraps a value into a monadic container, making it part of the monadic computation chain.&lt;/li&gt;
&lt;li&gt;bind: Applies a function to the value inside the monad, returning a new monadic container with the transformed value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;By using the State Monad, we can handle task management operations in a way that ensures the state is updated immutably and computations are composed without side effects or unwanted interactions between operations.&lt;/p&gt;

</description>
      <category>python</category>
      <category>functional</category>
      <category>monad</category>
    </item>
  </channel>
</rss>
