<?xml version="1.0" encoding="UTF-8"?><rss 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" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abdul Fatah on Medium]]></title>
        <description><![CDATA[Stories by Abdul Fatah on Medium]]></description>
        <link>https://medium.com/@abdulfatah_?source=rss-6af9b5bd6f0a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gEAW8RJDV-Es3-c74CX5EA.jpeg</url>
            <title>Stories by Abdul Fatah on Medium</title>
            <link>https://medium.com/@abdulfatah_?source=rss-6af9b5bd6f0a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 18:31:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdulfatah_/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Functors and Applicatives]]></title>
            <link>https://medium.com/@abdulfatah_/functors-and-applicatives-6ea27bf58b51?source=rss-6af9b5bd6f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ea27bf58b51</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[category-theory]]></category>
            <dc:creator><![CDATA[Abdul Fatah]]></dc:creator>
            <pubDate>Tue, 27 Dec 2022 00:10:43 GMT</pubDate>
            <atom:updated>2022-12-27T00:18:52.707Z</atom:updated>
            <content:encoded><![CDATA[<p>In functional programming, Functors and Applicatives are often used to abstract away the specifics of a context, allowing you to focus on the functions and values themselves. This can make your code more reusable, easier to understand, and easier to test.</p><h3>Functor</h3><p>Functor is a concept that allows you to apply a function to a value within a specific context. The context is represented by a type constructor called F, which is like a container that can hold a single type of value. For example, a List is a Functor because it can hold a single type of value (such as a list of integers or a list of strings) and has the ability to apply a function to the values within the list.</p><p>Imagine that you have a list of user objects, each with a name and age field. You want to create a new list of strings that includes the name and age of each user, formatted as “name (age)”.</p><p>One way to do this would be to use the mapmethod of a List to apply a function to each element of the list. Here’s some sample code that demonstrates how this could work:</p><pre>case class User(name: String, age: Int)<br>val users: List[User] = List( User(&quot;Alice&quot;, 25), User(&quot;Bob&quot;, 30), User(&quot;Charlie&quot;, 35) )<br>def userToString(user: User): String = s&quot;${user.name} (${user.age})&quot;<br>val userStrings: List[String] = users.map(userToString)</pre><p>In this example, the userToString function takes a User object and returns a formatted string. The map method of the List applies this function to each element of the list, resulting in a new list of strings.</p><p>The map method of a Functor is used to apply a function to the values within the context. It takes a function (f) that goes from type A to type B, and a value of type F[A], and applies the function (f) to the value inside the context, resulting in a value of type F[B].</p><h3>Applicative</h3><p>Applicative is an abstraction that allows you to apply a function within a specific context to a value that is also within that context. It is a subclass of the Functor type class and adds two methods: “pure” and “ap”. These methods allow you to lift values and functions into the context and apply functions to values within the context.</p><ol><li>pure, which lifts a value into the context. For example, if the context is Option, pure will take a value and return Some(value).</li><li>ap, which applies a function inside the context to a value inside the context. For example, if the context is Option, ap will take a function f: A =&gt; B and an Option[A], and return an Option[B].</li></ol><p>Together, the “pure” and “ap” methods of an Applicative allow you to lift values and functions into a specific context and apply functions to values within that context. This can be useful when you want to perform operations on values within a context, such as a List or a Future, without having to worry about the specifics of the context itself.</p><p>It can be especially helpful when working with asynchronous or data-driven systems, where you may have multiple layers of context (such as Futures or Options) that need to be combined and manipulated using functions. By using an Applicative, you can abstract away the specifics of the context and focus on the functions and values themselves.</p><h3>Conclusion</h3><p>Functors and Applicatives are powerful concepts in functional programming that allow you to apply functions to values within a specific context. They can be used to perform operations on values within a container, such as a List or an Option, without having to worry about the specifics of the container itself. These concepts can be especially useful when working with asyncronous or data-driven systems.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ea27bf58b51" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monad Transformers]]></title>
            <link>https://medium.com/@abdulfatah_/monad-transformers-766ed19d9fcd?source=rss-6af9b5bd6f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/766ed19d9fcd</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[category-theory]]></category>
            <dc:creator><![CDATA[Abdul Fatah]]></dc:creator>
            <pubDate>Wed, 21 Dec 2022 19:39:11 GMT</pubDate>
            <atom:updated>2022-12-21T19:39:11.550Z</atom:updated>
            <content:encoded><![CDATA[<p>Monad transformers are a powerful concept in functional programming that allow you to compose monads in a way that allows you to perform complex operations in a straightforward manner.</p><p>At a high level, monads are abstractions that allow you to encapsulate computations that may have side effects, such as reading from a database or performing a network request. Monads provide a way to structure your code in a way that makes it easy to handle these side effects in a predictable manner.</p><p>Monad transformers are a special kind of monad that allow you to “lift” one monad into another. For example, you can use a StateT monad transformer to lift the State monad into the IO monad, allowing you to perform stateful computations that may have side effects.</p><p>Here’s a simple example of how you might use a monad transformer in a real-world scenario:</p><p>Imagine you have a web application that allows users to create and edit profiles. The application stores the profiles in a database, and you want to implement a feature that allows users to change their name.</p><p>To implement this feature, you might write a function that takes the user’s ID and the new name as input and updates the database with the new name. This function might look something like this:</p><pre>def updateName(id: Int, name: String): IO[Unit] = {<br>  // code to update the database goes here<br>}</pre><p>This function works fine on its own, but now suppose you want to add some additional functionality, such as logging every time a user’s name is updated. You could do this by modifying the updateName function to include the logging logic, but this might make the function harder to read and maintain.</p><p>Instead, you can use a monad transformer to add the logging functionality in a more modular way. Here’s how you might do this using the StateT monad transformer:</p><pre>import cats.data.StateT<br>import cats.effect.IO<br><br>type Log = List[String]<br><br>def updateName(id: Int, name: String): StateT[IO, Log, Unit] = {<br>  for {<br>    _ &lt;- StateT.modify[IO, Log](log =&gt; log :+ s&quot;Updating name for user $id to $name&quot;)<br>    _ &lt;- StateT.liftF(IO {<br>      // code to update the database goes here<br>    })<br>  } yield ()<br>}</pre><p>This code defines a function called updateName that takes an Int value id and a String value name as input and returns a StateT[IO, Log, Unit] value.</p><p>The StateT[IO, Log, Unit] type is a monad transformer that combines the StateT and IO monads, allowing you to perform stateful computations that may have side effects. The Log type is the type of the state, and the Unit type is the type of the result.</p><p>The body of the updateName function is a for comprehension that performs two operations:</p><ol><li>The first operation uses the StateT.modify method to modify the state (a Log value) by appending a new log message to the list.</li><li>The second operation uses the StateT.liftF method to lift the IO monad into the StateT monad. The IO computation passed to StateT.liftF represents the code to update the database. This computation is executed as part of the StateT computation, and any side effects it has will be handled by the IO monad.</li></ol><p>Finally, the for comprehension yields (), indicating that the updateName function has no result value.</p><p>Here’s an example of how you might use the updateName function in your web application:</p><pre>import cats.data.StateT<br>import cats.effect.IO<br><br>val program: StateT[IO, Log, Unit] = for {<br>  _ &lt;- updateName(1, &quot;Alice&quot;)<br>  _ &lt;- updateName(2, &quot;Bob&quot;)<br>} yield ()<br><br>val initialState = List.empty[String]<br>val result: IO[(Log, Unit)] = program.run(initialState)<br><br>result.unsafeRunSync()  // returns (List(&quot;Updating name for user 1 to Alice&quot;, &quot;Updating name for user 2 to Bob&quot;), ())</pre><p>In this example, the program value is a StateT[IO, Log, Unit] value that represents the sequence of name updates for users 1 and 2, along with the logging of the operations. The run method is used to execute the StateT computation, and the unsafeRunSync method is used to execute the IO computation and get the result.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=766ed19d9fcd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>