<?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 Francisco Perrotta on Medium]]></title>
        <description><![CDATA[Stories by Francisco Perrotta on Medium]]></description>
        <link>https://medium.com/@PerrottaFrancisco?source=rss-5b40647aa7a0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CPzPdbmXDCTynEyfp1enAA.jpeg</url>
            <title>Stories by Francisco Perrotta on Medium</title>
            <link>https://medium.com/@PerrottaFrancisco?source=rss-5b40647aa7a0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:37:23 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@PerrottaFrancisco/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[Learning Cats Effects — Tagless final pattern ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-tagless-final-pattern-b0b0c9fbf274?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/b0b0c9fbf274</guid>
            <category><![CDATA[cats-effect]]></category>
            <category><![CDATA[final-tagless]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Sat, 01 Apr 2023 14:07:18 GMT</pubDate>
            <atom:updated>2023-04-01T14:07:18.915Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning Cats Effects — Tagless final pattern 😼</h3><p>In this ninth and last post about cats effects we will talk about a pattern that I love which cats effects allow to develop and think.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library ⚠️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/0*I7cg6TsynXuNAJbx.jpg" /></figure><h3>Introduction 👩🏻‍💻</h3><p>➝ <strong>Tagless Final? What are you talking about? 🤨</strong></p><p>The Tagless Final pattern is a technique used in functional programming languages, such as Haskell and Scala, to achieve polymorphism without using typeclasses. It involves representing the domain-specific language (DSL) as a set of composable functions, rather than relying on concrete data types.</p><p>➝ <strong>Simpler please </strong>😒</p><p>The Tagless Final pattern separates the definition of the DSL from its interpretation, making it easier to build and maintain complex systems.</p><p><em>Consists of two main components:</em></p><p>1- <strong>The algebraic data type (ADT)</strong>, which defines the operations of the DSL.</p><p>The ADT is defined as a set of functions that represent the operations of the DSL. Each function takes a polymorphic type parameter, which allows the function to operate on any type that satisfies the required constraints.</p><p>2- <strong>The Interpreter</strong>, which provides a concrete implementation of the DSL.</p><p>The interpreter is responsible for providing a concrete implementation of the DSL by providing a set of functions that interpret the ADT. The interpreter can be implemented in a variety of ways, such as using a monad, a free monad, or an extensible effects system.</p><p><strong>➝ What is the advantage of this pattern? </strong>😮</p><p>The key advantage of the Tagless Final pattern is that it separates the definition of the DSL from its interpretation, allowing for greater flexibility and composability.</p><p><strong>➝ How is this pattern related to cats? </strong>🤔</p><p>Cats Effect is a library in Scala that provides a set of abstractions for writing asynchronous, non-blocking code. It is built on top of the Tagless Final pattern, which provides a powerful and flexible way to define and interpret effectful computations.</p><p>In Cats Effect, the Tagless Final pattern is used to represent effectful computations as a set of composable functions, rather than using concrete data types.</p><h3>Why do we need it? 🙄</h3><p>As experienced functional developers, we know that learning the basic notions of functional programmings — such as referential transparency, lambda calculus, functors, and monads — is only the first step.</p><p>The first time we need to approach a non-trivial program, <strong>we must understand how to manage constraints among modules to avoid a functional </strong><a href="http://www.laputan.org/mud/"><strong>big ball of mud</strong></a>.</p><p>For example, let’s build a program that deals with shopping carts on an e-commerce site. First thing first, we need a representation of our domain model:</p><blockquote>case class <strong>Product</strong>(id: <strong>String</strong>, description: <strong>String</strong>)</blockquote><blockquote>case class <strong>ShoppingCart</strong>(id: <strong>String</strong>, products: <strong>List</strong>[<strong>Product</strong>])</blockquote><p>However, it’s not enough. We need some functions to deal with the domain model:</p><blockquote>def <strong>create</strong>(id: String): Unit</blockquote><blockquote>def <strong>find</strong>(id: String): Option[ShoppingCart]</blockquote><blockquote>def <strong>add</strong>(sc: ShoppingCart, product: Product): ShoppingCart</blockquote><p>The first problem with these functions is that they perform some side effects for sure. The <em>create </em>function will probably persist information on a database, as will the <em>add </em>function. The <em>find</em> function will eventually retrieve a shopping cart from a database, and this operation can eventually fail.</p><p>Fortunately, many Scala libraries (such as Cats Effects, Monix, or even ZIO) allow developers <strong>to encapsulate the operation description that produces side effects inside some declarative contexts. Such contexts are called effects.</strong> We can use the type of a function to describe the value it will produce and the side effects through the effect pattern. Hence, the description of the side effect is separated from its execution.</p><p>For example, we can use the generic <em>IO[T]</em> effect from Cats. Hence, our function becomes:</p><blockquote>def <strong>create</strong>(id: <strong>String</strong>): <strong>IO</strong>[<strong>Unit</strong>]</blockquote><blockquote>def <strong>find</strong>(id: <strong>String</strong>): <strong>IO</strong>[<strong>Option</strong>[<strong>ShoppingCart</strong>]]</blockquote><blockquote>def <strong>add</strong>(sc: <strong>ShoppingCart</strong>, product: <strong>Product</strong>): <strong>IO</strong>[<strong>ShoppingCart</strong>]</blockquote><p><strong>The best property about using an effect library is to continue to reason about our programs as pure functions. </strong>However, we now have to deal with the effect during the testing phase, which isn’t always obvious.</p><p>The second problem with our functions on shopping carts is that we don’t know where to put the code that implements them. Should we use a single concrete <em>class</em>? Or should we use an abstract <em>trait</em> and separate implementations?</p><p><strong><em>In Scala, many patterns have been applied through the years to answer the above-mentioned problems. The Free pattern is one of these. However, many developers have been choosing another pattern in the last few years: The Tagless Final pattern.</em></strong></p><blockquote><strong>the pattern’s main aim is to use interfaces as much as possible. In the pattern jargon, we call such interfaces algebras.</strong></blockquote><p><em>Algebras represent the DSL specific to the domain that we want to model. They represent both syntax and semantics of the DSL through the types and the signatures of their functions.</em></p><h3>Tell me the truth doctor! 😠</h3><h4>Advantages: 😇</h4><ol><li>Flexibility: The Tagless Final pattern allows for greater flexibility in implementing program effects, making it useful for programs that require a high degree of modularity and code reuse.</li><li>Testability: Because Tagless Final uses typeclasses and interfaces to define effects rather than concrete implementations, it is easy to write tests for programs that use this pattern, making automated testing easier and reducing the possibility of runtime errors.</li><li>Composability: The Tagless Final pattern makes it easy to compose different effects in a single program and modify their behavior at runtime, making programs more scalable and easier to maintain.</li><li>Type safety: The use of typeclasses in the Tagless Final pattern ensures that effects are applied correctly at compile-time, reducing the possibility of runtime errors.</li></ol><h4>Disadvantages: 😈</h4><ol><li>Learning curve: The Tagless Final pattern can have a steep learning curve, especially if you are not familiar with typeclasses and related concepts. It requires a solid understanding of the fundamentals of functional programming in Scala.</li><li>Increased complexity: In some cases, adopting the Tagless Final pattern can increase code complexity compared to simpler and more direct approaches, especially in small or less complex programs.</li><li>Increased code size: Using typeclasses and additional abstractions, the Tagless Final pattern may require a greater amount of code than simpler and more direct approaches, making the code harder to read and maintain.</li><li>Lower performance: Because the Tagless Final pattern uses typeclasses and additional abstractions, there may be a small decrease in performance compared to simpler and more direct approaches.</li></ol><h3>And when we need to use this pattern? 💪</h3><p>The pattern is particularly useful for use cases involving functional programming and effects in Scala. Some common use cases for this pattern include:</p><ol><li>Programs involving side effects, such as I/O or database calls, that require high modularity and code reuse.</li><li>Programs that require high testability, especially those involving side effects, where it is important to write automated tests to ensure that the program functions correctly.</li><li>Programs that need to be highly scalable and involve the combination and composition of multiple effects.</li><li>Programs that need to be highly maintainable and flexible over time.</li></ol><p>In general, the Tagless Final pattern is useful in any situation where greater flexibility, testability, composability, and maintainability are required in functional programming in Scala.</p><h3>Implementation Example 👨🏻‍🔬</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4bab9eda2b48e9f8711d36137e6582f3/href">https://medium.com/media/4bab9eda2b48e9f8711d36137e6582f3/href</a></iframe><p>In this example we can appreciate the 3 parts that make up the pattern.</p><blockquote><em>1. Definition of algebra / Behavior.<br>2. Implementation / Interpreter of the behavior in a specific way.<br>3. The starting point where the interpreter will be run.</em></blockquote><p>This example can be very simple and concise, so I am leaving you some implementation examples that I did some time ago.</p><ul><li><a href="https://github.com/FranTheHuman/PokerHand">GitHub - FranTheHuman/PokerHand: Functional exercise in Scala.</a></li><li><a href="https://github.com/FranTheHuman/TrafficLight">GitHub - FranTheHuman/TrafficLight: Personal challenge to implement Fs2 &amp; Akka Strams</a></li><li><a href="https://github.com/FranTheHuman/Relay-TCP">GitHub - FranTheHuman/Relay-TCP</a></li></ul><h3>My Conclusions 🤹🏼</h3><ul><li>The Tagless Final pattern separates the definition of the DSL from its interpretation, making it easier to build and maintain complex systems.</li><li>The best property about using an effect library is to continue to reason about our programs as pure functions.</li><li>The pattern’s main aim is to use interfaces as much as possible. In the pattern jargon, we call such interfaces algebras.</li><li>The pattern is flexible, highly testable, highly composable and type safe.</li><li>In general, the Tagless Final pattern is useful in any situation where greater flexibility, testability, composability, and maintainability are required in functional programming in Scala.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://typelevel.org/cats-effect/">Official Cats Effect documentation</a></li><li><a href="https://blog.softwaremill.com/final-tagless-seen-alive-79a8d884691d">Final Tagless seen alive</a></li><li><a href="https://wiki.haskell.org/wikiupload/8/85/TMR-Issue13.pdf">The Monad.Reader Issue 13</a></li><li><a href="https://www.baeldung.com/scala/tagless-final-pattern">The Tagless Final Pattern in Scala</a></li><li>An excellent explanation is that of Daniel Ciocîrlan, who kicks the expression problem — <a href="https://blog.rockthejvm.com/tagless-final/">Tagless Final in Scala</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b0b0c9fbf274" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects —Managing resources ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-managing-resources-f62fe7c6d245?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f62fe7c6d245</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[cats-effect]]></category>
            <category><![CDATA[resourcetype]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Fri, 31 Mar 2023 21:04:27 GMT</pubDate>
            <atom:updated>2023-03-31T21:04:27.814Z</atom:updated>
            <content:encoded><![CDATA[<p>In this eighth post about cats effects we will continue writing down the most interesting things from the book “Essential Effects” by Aleksandar Prokopec.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library but the book is based on both version 2.2.0 and 3 ⚠️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*j0xO833T5eDqs086.jpg" /></figure><p>It’s often the case that state needs to be managed. For example, a thread pool needs to be allocated and configured before it can be used, and once we’re done with it the threads need to be shut down. Many other kinds of state have the same need for lifecycle management.</p><blockquote>In Cats Effect, the Resource data type represents this acquire-use-release pattern to manage state.</blockquote><h3>Creating a Resource to manage state 👩‍🏭</h3><p>We’ll use <em>Resource.make</em>. It takes two effectful arguments: one to<br>produce (acquire) the state, and another to release it:</p><blockquote><strong>def make[A](acquire: IO[A])(release: A =&gt; IO[Unit]): Resource[IO, A]</strong></blockquote><p><em>Example:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b1cbcccd016c9e20c35a352b8f296e9/href">https://medium.com/media/5b1cbcccd016c9e20c35a352b8f296e9/href</a></iframe><p><em>We build a Resource[IO, String] with Resource.make, passing it two<br>effectful functions. In this example, we add additional debug logging to see<br>when these “lifecycle” effects execute.</em></p><p>Running BasicResource produces the output:</p><blockquote><strong>[ioapp-compute-0] &gt; acquiring stringResource<br>[ioapp-compute-0] String is so cool!<br>[ioapp-compute-0] &lt; releasing stringResource</strong></blockquote><p>It’s important to note that Resource doesn’t perform any effects itself. When we use it, it produces a new effect. You can think of Resource as a tiny DSL to describe resource management in terms of individual IO effects and the use method then “compiles” those instructions into a single IO value. Additionally the release effect will not only be executed if the use effect completes successfully, but also if it raises an error.</p><h4><strong>First Example: Ensuring a file handle is closed</strong></h4><p>Here’s a more practical example of defining a Resource to read from an open file, with the usual post-condition of ensuring the file is closed:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4a9ee38a13ac50f7aff5c4c68b02f0d/href">https://medium.com/media/a4a9ee38a13ac50f7aff5c4c68b02f0d/href</a></iframe><ul><li>Our Resource will manage a hidden java.io.RandomAccessFile wrapped in the FileBufferReader data type.</li><li>FileBufferReader exposes only one method: an effect to read a buffer from an offset.</li><li>Since we want the Resource to manage the hidden state, we make the close method inaccessible from outside callers.</li><li>We make our Resource by creating the FileBufferReader in an IO effect,<br>ensuring that we close the state when the Resource is released.</li></ul><h4>Second Example: Canceling a background task</h4><p>A perhaps less obvious use of a Resource is to manage the lifecycle of a background task. For example, we may want to fork some (often non-terminating) effect, and later cancel it when it isn’t required to run anymore. This suggests we can combine use of a Fiber with a Resource.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41f917435690f52fc7d66eb9a5c6391f/href">https://medium.com/media/41f917435690f52fc7d66eb9a5c6391f/href</a></iframe><ul><li>The background task will only be running during our use effect. We sleep a little bit, to ensure the background task does some work.</li><li>The background task itself is a loop that prints and sleeps. We use the<br>foreverM combinator, which is equivalent to → step.flatMap(_ =&gt; loop)</li><li>The acquire effect forks a Fiber and the release effect cancels it.</li><li>In this example we don’t give the user of the Resource access to the Fiber, although you could imagine where this may be useful.</li></ul><h3>Composing managed state 🎼</h3><p>We can define individual Resource values, but building a Resource from another Resource? No problem, we can compose in multiple ways.</p><p>➙ Resource is a functor; we can map over it.</p><p>➙ Resource is an applicative; we can mapN over two or more values.</p><p>➙ Resource is a monad; we can flatMap over it.</p><h4>Parallel resource composition 🎼 🎼</h4><p>Resource is a monad, so we can compose it with various combinators. But it also has a Parallel typeclass instance, so resource management can occur in parallel rather than sequentially!</p><p>All we need to do is use the par-prefixed combinators.</p><h3>Resources for dependency management 🧩</h3><p>Most of the code we’ve seen so far have been short examples that demonstrate focused topics like parallelism, concurrency, and so on. While these examples have been written using IOApp, we haven’t discussed how to build larger applications with IOApp, or what issues may arise as we try to compose sets of effects into a larger program.</p><p>Since a Resource perfectly encapsulates the effectful allocation and clean-up of a value, we can use it to manage our application’s dependencies for us. Our IOApp-based application will then be structured into three distinct concerns:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40f03a5f446da2a444ad69cf701b3f41/href">https://medium.com/media/40f03a5f446da2a444ad69cf701b3f41/href</a></iframe><ul><li>We compose a set of managed dependencies into a single Resource value.</li><li>The application logic uses the dependencies — but does not manage them.</li><li>At the beginning of our application we use our managed dependencies,<br>providing them to the application logic. The dependencies only exist<br>within the scope of the use block.</li></ul><h3>My Conclusions 🤹🏼</h3><ul><li>The Resource data type captures the pattern where the code for state<br>acquisition and release is separated from code that uses the state.</li><li>A Resource can be composed into other Resource values, both serially and in parallel.</li><li>We can use Resource to represent the lifecycle of our application dependencies. We then use them in our IOApp to acquire them during the execution of the dependent code, and to ensure they are released.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f62fe7c6d245" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects — Integrating Asynchrony ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-integrating-asynchrony-36892f44e80f?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/36892f44e80f</guid>
            <category><![CDATA[cats-effect]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Wed, 25 Jan 2023 13:02:31 GMT</pubDate>
            <atom:updated>2023-01-25T13:02:31.280Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning Cats Effects — Integrating Asynchrony 😼</h3><p>In this seventh post about cats effects we will continue writing down the most interesting things from the book “Essential Effects” by Aleksandar Prokopec.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library but the book is based on both version 2.2.0 and 3 ⚠️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*If8shXapur_ftwQR.jpg" /></figure><h4>Continuation</h4><p>Code that will be executed “next”. When viewed as part of a larger sequence of steps, the continuation represents an unexecuted portion of the whole. For example: callbacks; the resumption of an effect after an asynchronous boundary.</p><h3>Asynchronous callbacks 🏞</h3><p>How can we integrate with any possible asynchronous interface? 😕 For that we’re going to use the IO.async method to construct an IO value from a callback-based API.</p><p><em>Cats Effect IO.async method, uses the general pattern of continuation passing to integrate any kind of asynchronous processing interface.</em></p><blockquote><strong><em>def async[A](k: (Either[Throwable, A] =&gt; Unit) =&gt; Unit): IO[A]</em></strong></blockquote><p><em>async</em> is a higher-order function, in this case a function that takes another function as an argument.</p><p>Breaking down its signature, we provide async a function k, and async invokes it, providing a callback cb. Code within our function k then will invoke cb when, and only when, we have a result of the asynchronous computation.</p><h4>Tracing an asynchronous execution 🕵️‍♂️</h4><p>To demonstrate IO.async let’s create a new asynchronous IO value that uses some callback-based API — in this case, Future. We’ll reproduce what IO.fromFuture does to adapt to the Future type using IO.async</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05a55f9c6866da73247bea817762dc7f/href">https://medium.com/media/05a55f9c6866da73247bea817762dc7f/href</a></iframe><blockquote><em>When the API computes the result it provides it to the callback.</em></blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/579/1*R9AJfNav5NZ4-icv2tFvBQ.png" /></figure><p><em>☢️☢️ The callback may be executing on some non-IO thread, so we guarantee that the next effect to execute will be on our own threads by shift-ing onto our context. But, in cats effects 3, there is no longer a need for shifting back, because interop with callback-based libraries is done through methods in Async, </em><strong>which now switch back to the appropriate thread pool automatically</strong><em>. ☢️☢️</em></p><h3>Integrating with Future 🔆</h3><p>scala.concurrent.Future is the most common legacy data type for asynchronous computation in Scala, and as we’ve seen we can use IO.async to implement an IO value in terms of an asynchronously executing Future. Since it’s so common, Cats Effect provides a built-in method: <em>IO.fromFuture</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd246469dbc9b7572e752fbc3f647007/href">https://medium.com/media/bd246469dbc9b7572e752fbc3f647007/href</a></iframe><p>Let’s integrate a method that returns a Future. In real code this method would live in some API defined elsewhere. IO.fromFuture translates a Future[A], itself wrapped in an IO, into an IO[A].</p><h3>My Conclusions 🤹🏼</h3><ul><li>IO.async allows us to build effects that can start asynchronous processes.</li><li>Asynchronous effects fundamentally rely upon continuation passing, where the actual asynchronous computation is given code to run when the computation completes.</li><li>fromFuture transforms a Future into a referentially-transparent effect.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=36892f44e80f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects — Concurrency pt. 2]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-concurrency-pt-2-bd9a725ad932?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/bd9a725ad932</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[cats-effect]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Wed, 18 Jan 2023 13:02:27 GMT</pubDate>
            <atom:updated>2023-01-18T13:02:27.304Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning Cats Effects — Concurrency pt. 2😼</h3><p>In this sixth post about cats effects we will continue writing down the most interesting things from the book “Essential Effects” by Aleksandar Prokopec.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library but the book is based on both version 2.2.0 and 3 ⚠️ In this post we will mainly focus on Raas Ahsan’s blog at typelevel.org <br>(Link in the bibliography)</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*a30eKMgRMXoIK4D2.jpg" /></figure><h3>Race Effects 🏃🏾🏃🏿‍♀️🏃🏻🏃🏿‍♂️🏃🏼‍♀️🏃‍♂️🚶‍♀️</h3><p>When we compose multiple effects concurrently with parMapN, we provide a function to transform the gathered output of every concurrently executing effect.</p><p>What if instead we were only interested in the effect that completed first, relating them temporally. We call this a race, and can have one using the IO.race combinator:</p><blockquote><strong>def race[A, B](lh: IO[A], rh: IO[B])(implicit cs: ContextShift[IO]): IO[Either[A, B]]</strong></blockquote><p>Cats Effect exposes several utility functions for racing IO actions (or their fibers) against eachother.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a963a33d6d427fa1188bf88c59da5c5/href">https://medium.com/media/7a963a33d6d427fa1188bf88c59da5c5/href</a></iframe><ol><li>racePair races two fiber and returns the outcome of the winner along with a FiberIO handle to the loser.</li><li>race races two fibers and returns the successful outcome of the winner after canceling the loser.</li><li>both races two fibers and returns the successful outcome of both (in other words, it runs both fibers concurrently and waits for both of them to complete).</li></ol><p>You can think of race in relation to parTupled — both run the effects concurrently, but parTupled gives you both results (the first and the second), whereas race gives you only one (the first or the second)</p><p>Race exacmple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e12cf23b4e9132cfeaf554bb8b8ea8e/href">https://medium.com/media/5e12cf23b4e9132cfeaf554bb8b8ea8e/href</a></iframe><p><strong><em>Running this program with many iterations demonstrates that either fiber can win.</em></strong></p><h3>Fibers Communications 📢</h3><p>We have seen how fibers can directly communicate with each other via start, join, and cancel. But… is there a form of communication that is during the life cycle of a fiber? 🧐</p><p><em>Shared memory is an alternative means by which fibers can indirectly communicate and synchronize with each other. Cats Effect exposes two primitive concurrent data structures that leverage shared memory:</em><strong><em> Ref and Deferred.</em></strong></p><p>🔹<strong>REF</strong></p><p>Ref is a concurrent data structure that represents a mutable variable. It is used to hold state that can be safely accessed and modified by many contending fibers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1bd0cba18f04f6fd6496edddc7b81d0/href">https://medium.com/media/e1bd0cba18f04f6fd6496edddc7b81d0/href</a></iframe><ol><li>The main fiber starts 100 fibers, each of which attempts to concurrently update the state by atomically incrementing its value.</li><li>The main fiber joins on each spawned fiber one after the other, waiting for their collective completion.</li><li>After the spawned fibers are complete, the main fiber retrieves the final value of the state.</li></ol><p>🔹<strong>DEFERRED</strong></p><p>Deferred is a concurrent data structure that represents a condition variable. It is used to semantically block fibers until some arbitrary condition has been fulfilled.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1394b2d19d6e44ab3699219acd6ccf47/href">https://medium.com/media/1394b2d19d6e44ab3699219acd6ccf47/href</a></iframe><ol><li>The main fiber spawns a fiber that initiates a countdown.</li><li>When the countdown reaches 5, it waits on a Deferred which is completed 5 seconds later by the main fiber.</li><li>The main fiber then waits for the countdown to complete before exiting</li></ol><p>The program should produce the following output:</p><blockquote><strong>10<br>9<br>8<br>7<br>6<br>5<br>paused…<br>4<br>3<br>2<br>1<br>0<br>blast off!</strong></blockquote><p><strong><em>Ref and Deferred are often composed together to build more powerful and more complex concurrent data structures. Most of the concurrent data types in the std module in Cats Effect are implemented in terms of Ref and/or Deferred.</em></strong></p><h3><strong>SCHEDULING </strong>🕑</h3><p>How do fibers actually execute? 🤔</p><p>Our Scala applications ultimately run on the JVM, so fibers must ultimately be mapped to native threads in order for them to actually run.</p><p><strong><em>The scheduler is responsible for determining how this mapping takes place.</em></strong></p><p>On the JVM, Cats Effect uses an M:N scheduling model to map fibers to threads. In practice, this means that a large number of fibers is multiplexed onto much smaller pools of native threads.</p><p><strong><em>Throughout its lifetime, a fiber will migrate between fixed-size pools to accomplish its various tasks.</em></strong></p><blockquote><strong><em>A scala.concurrent.ExecutionContext represents a scheduling queue along with a set of threads used for computation.</em></strong></blockquote><h4>👉 Shifting Contexts 🥃</h4><p>We can only execute at most <em>numCpus</em> effects in parallel. This makes sense since we only have a <em>numCpus</em> CPUs! But at the same time, our computers regularly do more than numCpus things at the same time.</p><p>How can we reconcile these disparate ideas?</p><ul><li>We can have many threads running, and their execution is multiplexed across the available cores available from the operating system. And we can pool those threads into logical groups with data types like ExecutionContext, where threads in one pool are isolated from those in another.</li><li>To ensure our programs make progress — ensuring work proceeds when I/O-bound work is blocked — we’ll isolate the CPU-bound work from any I/O-bound tasks by having separate pools. The Cats Effect library supports this pattern by encouraging separate contexts.</li></ul><ol><li><strong><em>CPU-bound</em></strong> work will be scheduled on a fixed-size thread pool, where the number of threads is the number of cores available to the JVM. All things being equal, you can’t compute more than &lt;number of CPUs&gt; things at a time, so don’t try to do more.</li><li><strong><em>I/O-bound</em></strong> work will be scheduled on an unbounded thread pool so that blocked threads merely take up memory instead of stopping the progress of other tasks.</li></ol><blockquote>In an IOApp on the JVM the default ExecutionContext is configured for CPU-bound work.</blockquote><h4>👉 <strong>Contexts for I/O-bound actions</strong></h4><p>We could instantiate our own ExecutionContext to use for blocking I/O effects, configuring it to use an unbounded thread pool. But it would be somewhat difficult to properly use since it has the same type as any other ExecutionContext value, like the default context provided for CPU-bound work by IOApp. We could easily pass the wrong context to a method. If instead we had a context with a different typew e couldn’t make that mistake. Luckily Cats Effect provides the exact solution: <strong><em>Blocker</em></strong>.</p><p><strong><em>But instead of using a Blocker backed by a special ExecutionContext, Cats Effect 3 gives us a dedicated effect constructor to declare an effect as blocking as early as possible:</em></strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/057c7f05d7705615171f2cdac3ce46b2/href">https://medium.com/media/057c7f05d7705615171f2cdac3ce46b2/href</a></iframe><h4>👉 Cede function 🤸🏿</h4><p>Another aspect of scheduling is how context switching of fibers takes place, which has many implications around the fairness and throughput properties of a concurrent application. Like many other lightweight thread runtimes, Cats Effect’s default scheduler exhibits cooperative multitasking in which fibers can explicitly “yield” back to the scheduler, allowing a waiting fiber to run. The yielded fiber will be scheduled to resume execution at some later time. This is achieved with the IO.cede function.</p><p>Cats Effect’s default scheduler also supports autoyielding which is a form of preemptive multitasking. This is particularly important on runtimes that run with 1 or 2 native threads; a fiber that runs forever but never cedes will starve other fibers of compute time.</p><h3>My Conclusions 🤹🏼</h3><ul><li>We were interested in running effects concurrently and keeping the results of each effect, but in the case that we only need the first effect to complete we have the option of using the race method.</li><li>We can share memory between fibers during their life cycle (to coordinate them) thanks to the Ref and Deferred methods.</li><li>Throughout its lifetime, a fiber will migrate between fixed-size pools to accomplish its various tasks.</li><li>IOApp provides a default ExecutionContext with a fixed number — the number of CPUs on the machine — of threads. This is meant for CPU-bound work.</li><li>I/O-bound work, which is usually slower than CPU-bound work because it will block the thread it uses, should run in a pool separate from CPU-bound work.</li><li>Blocking I/O-bound work should be run in an unbounded thread pool.</li><li>We can cede control of the fiber to another context thanks to the cede method.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li><li><a href="https://typelevel.org/blog/2020/10/30/concurrency-in-ce3.html">CONCURRENCY IN CATS EFFECT 3</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bd9a725ad932" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects —Concurrency pt. 1 ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-concurrency-pt-1-c9fe33faad04?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/c9fe33faad04</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[cats-effect]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Wed, 11 Jan 2023 13:01:45 GMT</pubDate>
            <atom:updated>2023-01-11T13:01:45.479Z</atom:updated>
            <content:encoded><![CDATA[<p>In this fifth post about cats effects we will continue writing down the most interesting things from the book “Essential Effects” by Aleksandar Prokopec.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library but the book is based on both version 2.2.0 and 3 ⚠️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*gestE5MJncfOVmrQ.jpg" /></figure><p>We already investigated concurrency in my series of posts <a href="https://medium.com/@PerrottaFrancisco/learning-concurrent-programming-in-scala-9c59fcbc6e10"><em>Learning Concurrent Programming in Scala</em></a>, but here the idea is to learn how to handle concurrency with cats effects.</p><h4>Concurrency vs Parallelism 🤛</h4><p>Although they are often conflated, concurrent and parallel are disparate<br>concepts</p><p><strong><em>Concurrent</em></strong><em>: </em>Computations are concurrent when their execution lifetimes overlap.<br><strong><em>Parallel</em></strong><em>: </em>Computations are parallel when their executions occur at the same instant in time.</p><p><strong><em>That is to say, concurrency is about the looking at the structure of the<br>computations and how their lifetimes align, whereas parallelism is more<br>about the operational utilization of resources during the execution.</em></strong></p><h3>Cats Effect Concurrency 😻</h3><p>Cats Effect and other libraries provide higher-level and safer abstractions that users can exploit to achieve concurrency without having to understand what’s happening under the hood.</p><p>For example, <a href="https://http4s.org/">http4s</a> achieves concurrency on requests by spawning a fiber for every request it receives; users need only specify the request handling code that is eventually run inside the <strong><em>fiber</em></strong>.</p><h4>FIBERS 🧬</h4><p>Cats Effect chooses fibers as the foundation for its concurrency model.</p><p><em>The main benefit of fibers is that they are conceptually similar to native threads: both are types of logical threads that describe a sequence of computations.</em></p><p><strong><em>Unlike native threads, fibers are not associated with any system resources.</em></strong></p><p><strong>Benefits</strong>:</p><ul><li>Fibers are incredibly cheap in terms of memory overhead so we can create hundreds of thousands of them without thrashing the process. (This means that we also don’t need to pool fibers)</li><li>Context switching between fibers is orders of magnitude faster than context switching between threads.</li><li>Blocking a fiber doesn’t necessarily block a native thread; this is called semantic blocking. This is particularly true for nonblocking I/O and other asynchronous operations.</li></ul><p><strong><em>A fiber is a logical thread that encapsulates the execution of an IO[A] program, which is a sequence of IO effects (actions) that are bound together via flatMap.</em></strong></p><p>The active execution of some fiber of an effect IO[A] is represented by the type FiberIO[A]. The execution of a FiberIO[A] terminates with one of three possible outcomes, which are encoded by the datatype OutcomeIO[A]:</p><ol><li><em>Succeeded: indicates success with a value of type A</em></li><li><em>Errored: indicates failure with a value of type Throwable</em></li><li><em>Canceled: indicates abnormal termination via cancellation</em></li></ol><p><strong><em>Additionally, a fiber may never produce an outcome, in which case it is said to be nonterminating.</em></strong></p><blockquote>⚠️ A Fiber is a very “low-level” mechanism for concurrent control. While it’s<br>absolutely necessary for implementing the concurrency and parallelism of Cats Effect, as a developer you can often better achieve your goals by using higher-level abstractions and operations. ⚠️</blockquote><h3>Decomposing the behavior of parMapN 🧫</h3><blockquote><strong>def myParMapN[A, B, C](ia: IO[A], ib: IO[B])(f: (A, B) =&gt; C): IO[C] =<br>???</strong></blockquote><p>myParMapN, just like parMapN, needs to:</p><ul><li>Start both the ia and ib computations so they run concurrently (“fork” them)</li><li>Wait for each result</li><li>Cancel the “other” effect if ia or ib fails</li><li>Combine the results with the f function.</li></ul><h4>FORK (STARTING) 🧪</h4><p><em>In Cats Effect, to </em><strong><em>fork</em></strong><em> an effect we’ll use the start method. Let’s make a simple program and examine its behavior:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22b588448f3652607247f52660dc9ea0/href">https://medium.com/media/22b588448f3652607247f52660dc9ea0/href</a></iframe><p>With the start method, we start the effect to fork its execution from the current effect. Immediately after we start the task we print something to the console.</p><p>Running Start we see the following output:</p><blockquote><strong>[ioapp-compute-1] task 1<br>[ioapp-compute-0] task was started</strong></blockquote><p><strong><em>When you start an effect its execution is “forked”: it is shifted to a different thread.</em></strong></p><p>Here’s the (simplified) signature of start:</p><blockquote>def start: IO[Fiber[IO, A]]</blockquote><ol><li>it returns a Fiber, a data type which lets us act on the start-ed effect.</li><li>It returns a Fiber inside an IO because if it instead produced, directly, a Fiber, that would mean our original IO is running right now, but in reality it isn’t. (The source IO only executes when we explicitly run it, so we need to delay access to this fiber — by wrapping it in an effect — until the source IO is executed.)</li></ol><p>👨🏽‍🔬 <em>We can use start to fork a concurrent effect, so let’s use it for our myParMapN function:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05f95daf574a25fb65ed9b8be25dd89d/href">https://medium.com/media/05f95daf574a25fb65ed9b8be25dd89d/href</a></iframe><p>We start each effect to run them concurrently and we don’t yet know how to gather their results or possibly cancel them.</p><h4>JOINING 🧪</h4><p>After a fiber is started, it can be joined which semantically blocks the joiner until the joinee has terminated, after which join will return the outcome of the joinee.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/904e149ceb939d97ff4e10e71a815849/href">https://medium.com/media/904e149ceb939d97ff4e10e71a815849/href</a></iframe><p>After we print our pre-join message, we invoke join.</p><p><em>&gt; extension method is equivalent to using mapN with two effects but only the second effect’s value is produced</em></p><p>Running JoinAfterStart outputs</p><blockquote><strong>[ioapp-compute-0] pre-join<br>[ioapp-compute-1] task <br>[ioapp-compute-1] task<br>[ioapp-compute-1] post-join</strong></blockquote><p><em>(Notice that task is on a different thread than the “pre-join” output.)</em></p><p>We also see task printed twice, once for the IO(“task”).debug and once for the fiber.join.debug. When we join a Fiber, execution continues on the thread the Fiber was running on</p><p>👨🏽‍🔬 <em>Now that we know we can await the results of a concurrent effect with join, we can update our myParMapN method.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ecea8d943e6581370cd0eed85997e7f/href">https://medium.com/media/1ecea8d943e6581370cd0eed85997e7f/href</a></iframe><blockquote>⚠️ ️join method return an Outcome value, and you must match the value of the result to know which of the 3 Outcome types options is the value (Succeeded(fa) — Errored(e) — Canceled()) .</blockquote><p>An alternative is use joinWithNever method. Is a variant of join that asserts the fiber has an outcome of Succeeded and returns the associated value.</p><h4>CANCELING 🧪</h4><p>A fiber can be canceled after its execution begins with the FiberIO#Cancel function. This semantically blocks the current fiber until the target fiber has finalized and terminated, and then returns.</p><p>Why might we want to stop a running task? 😕</p><p>Usually it is because we’ve learned some information that tells us the computation isn’t needed any longer. For example, we might start a fetch from a (relatively slow) datastore, but if the user decides to cancel the overall operation, we should cancel the fetch to the underlying datastore.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/781880b8ac98dcaebe85da80c13cf5cb/href">https://medium.com/media/781880b8ac98dcaebe85da80c13cf5cb/href</a></iframe><p><em>onCancel is a “finalizer”, allowing you to free up resources in case you get canceled</em></p><p>Running Cancel outputs:</p><blockquote><strong>[ioapp-compute-0] pre-cancel<br>[ioapp-compute-1] task<br>[ioapp-compute-0] i was cancelled<br>[ioapp-compute-0] canceled</strong></blockquote><p>Note that cancel is idempotent. Invoking it more than once has the same effect as invoking it once — a canceled task will continue to be canceled.<br>However, if you join after you cancel, the join will never finish, because no result will ever be produced.</p><p>👨🏽‍🔬 <em>If an error occurs during one of our effects, we need to cancel “the other” fiber. Let’s use the onError combinator to handle each effect:</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3d4a4d33a41c9e4c36d5f4e0322b750/href">https://medium.com/media/b3d4a4d33a41c9e4c36d5f4e0322b750/href</a></iframe><p>If the computation of fiberA has an error, cancel fiberB. And if the computation of fiberB has an error, cancel fiberA.</p><p>However, there is a critical bug here. 😧</p><p><em>The</em><strong><em> issue</em></strong><em> is that registering an onError handler is itself an effect, so in the code above the handler would only be registered if we couple it to the result of fiberA.join. But if we do that, then we won’t be registering the onError handler with the result of fiberB until after fiberA has actually finished.</em></p><p><em>We need to instead ensure that both onError handlers are registered.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e516f0ba17721764b20948cf0432c87b/href">https://medium.com/media/e516f0ba17721764b20948cf0432c87b/href</a></iframe><p>If one of the effects is cancelled, then a subsequent join will never complete. We’re stuck: we need to avoid doing a join on a potentially cancelled effect, buther me either effect could be cancelled first — we don’t know which.</p><p>The Fiber API isn’t expressive enough to give us the information we need. To solve the problem, we need a different “primitive” operation: <em>we’ll instead race two effects, which will let us know which effect finishes first so that we can subsequently join the other effect.</em></p><h3>My Conclusions 🤹🏼</h3><ul><li>Concurrency allows us to control running computations.</li><li>Computations are concurrent when their execution lifetimes overlap.</li><li>A Fiber is our handle to concurrency.</li><li>Fibers are logical threads, so they can run concurrently.</li><li>After we start a concurrent computation, we can cancel or join it (wait for completion).</li><li>Concurrently executing effects can be cancelled.</li><li>Cancelled effects blocks the current fiber until the target fiber has finalized and terminated.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li><li><a href="https://typelevel.org/blog/2020/10/30/concurrency-in-ce3.html">CONCURRENCY IN CATS EFFECT 3</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c9fe33faad04" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects — Parallel execution ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-parallel-execution-f617f883e390?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/f617f883e390</guid>
            <category><![CDATA[cats-effect]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Wed, 04 Jan 2023 13:01:53 GMT</pubDate>
            <atom:updated>2023-01-04T13:01:53.036Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning Cats Effects — Parallel execution 😼</h3><p>In this third post about cats effects we will continue writing down the most interesting things from the book “Essential Effects” by Aleksandar Prokopec.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library but the book is based on both version 2.2.0 and 3 ⚠️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/728/1*w7UUCLxa0YPkhSG9yNet7A.jpeg" /></figure><p>Remembering what I saw in the post ‘<a href="https://medium.com/@PerrottaFrancisco/concurrent-programming-data-parallel-parallel-collections-with-scala-903f13988c1e">Concurrent programming — Data Parallel</a>’ I can say that concurrency and parallelism are not the same.</p><p><em>Parallelism is about simultaneous execution whereas concurrency is about interleaved execution.</em></p><p>Parallelism is typically achieved by exploiting multiple CPU cores or even multiple, independent machines to run a set of tasks much faster than they would run on a single CPU or machine.</p><h4>Does IO support parallelism?</h4><p>IO doesn’t provide any support for the effect of parallelism! And this is by design, because we want different effects to have different types, as per our Effect Pattern.</p><h3>The Parallel typeclass 🙆🏾‍♀️</h3><p>IOs are usually sequential, and doesn’t support parallelism. For this we need a new type that does. In cats.effect, this type is named IO.Par (Par for “parallel”).</p><blockquote>sealed abstract class IO[+A] { … }<br>object IO {<br> class Par[+A] { … }<br> object Par {<br> … def apply[A](ioa: IO[A]): Par[A] = ??? <br> … def unwrap[A](pa: Par[A]): IO[A] = ???<br> }<br>}</blockquote><p>Par will not have a Monad instance, because we do not want to be able to serialize the execution of multiple actions. Instead it will have an Applicative instance, to compose independent IO.Par values</p><blockquote>The Parallel typeclass from the Cats library (not Cats Effect) captures the concept of translating between two related data types.</blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3e2f312b72aa8e840d1872cdb0ba81e/href">https://medium.com/media/b3e2f312b72aa8e840d1872cdb0ba81e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/346/1*fu4xHTemYo8nq5npK_dAYg.png" /></figure><p>The Parallel typeclass encodes transformations between a sequential type S and a parallel type P.</p><blockquote><em>We can convert a sequencial IO to parallel IO in this way:</em></blockquote><blockquote><strong>val parValue: IO.Par[Int] = Parallel[IO].parallel(IO.delay(42))</strong></blockquote><h3>parMapN 🤝</h3><p>parMapN is the parallel version of the applicative mapN method. It lets us combine multiple effects into one, in parallel, by specifying how to combine the outputs of the effects:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/74171d22050e8d04fb13f16d96642e7f/href">https://medium.com/media/74171d22050e8d04fb13f16d96642e7f/href</a></iframe><p>mapN and parMapN act on tuples of any arity, so we can combine any number of effects together in a consistent way. For example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9d9fb68e8b68001caa6cb570aff05bf/href">https://medium.com/media/f9d9fb68e8b68001caa6cb570aff05bf/href</a></iframe><blockquote>The execution order of parallel tasks is non-deterministic</blockquote><p><em>What happens if there are failures during parMapN? The first failure to happen is used as the failure of the composed effect.</em></p><h3>parTraverse ✌🏽</h3><p>parTraverse is the parallel version of traverse; both have the type signature:</p><blockquote>F[A] =&gt; (A =&gt; G[B]) =&gt; G[F[B]]</blockquote><p>For example, if F is List and G is IO, then (par)traverse would be a function from a List[A] to an IO[List[B]] when given a function A ⇒ IO[B].</p><p>The most common use case of (par)traverse is when you have a collection of work to be done, and a function which handles one unit of work. Then you get a collection of results combined into one effect:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3877655d6f68096cf61cefb355716a1/href">https://medium.com/media/e3877655d6f68096cf61cefb355716a1/href</a></iframe><p><em>parTraverse is actually written in terms of traverse, where it transforms every IO into IO.Par. Since traverse only requires the effect to have an Applicative instance, the Applicative[IO.Par] is where the parallelism “happens”.</em></p><h3>parSequence 🤙🏻</h3><p>parSequence turns a nested structure “inside-out”:</p><blockquote>F[G[A]] =&gt; G[F[A]]</blockquote><p>For example, if you have a List of IO effects, parSequence will, in parallel, transform it into one IO effect that produces a List of outputs:</p><blockquote>List[IO[A]] =&gt; IO[List[A]]</blockquote><h3>My Conclusions 🤹🏼</h3><ul><li>IO does not support parallel operations itself.</li><li>The Parallel typeclass specifies the translation between a pair of effect types: one that is a Monad and the other that is “only” an Applicative.</li><li>Parallel[IO] connects the IO effect to its parallel counterpart, IO.Par.</li><li>parMapN, parTraverse, parSequence are the parallel versions of (the sequential) mapN, traverse, and sequence.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li><li><a href="https://typelevel.org/blog/2020/10/30/concurrency-in-ce3.html">CONCURRENCY IN CATS EFFECT 3</a></li><li><a href="https://github.com/rockthejvm/cats-effect">cats-effect</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f617f883e390" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects — IO ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-io-2763f592386e?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/2763f592386e</guid>
            <category><![CDATA[cats-effect]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Wed, 28 Dec 2022 13:01:31 GMT</pubDate>
            <atom:updated>2022-12-28T13:01:31.835Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning Cats Effects — IO 😼</h3><p>In this second post about cats effects we will continue writing down the most interesting things from the book “Essential Effects” by Aleksandar Prokopec.</p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library but the book is based on both version 2.2.0 and 3 ⚠️</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WAV-G1hg84a5rxvM.jpg" /></figure><p>We’ve seen the Option effect type, which doesn’t involve side effects, and we’ve examined why Future isn’t an effect. So what about an effect that does involve side effects, but safely?</p><p>This is the purpose of the IO effect type in cats.effect. It is a data type that allows us to capture any side effect, but in a safe way, following our Effect Pattern.</p><h3>Own version of IO 👨🏽‍🍳</h3><p>Let’s create our first effect: we want to capture arbitrary side effects. We’ll<br>demonstrate it by describing an effect to print a string to the console, then<br>subsequently execute it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c591cd93a04ceb8bf769e7490353e5d5/href">https://medium.com/media/c591cd93a04ceb8bf769e7490353e5d5/href</a></iframe><p>If we run the Printing program it outputs:</p><blockquote>hello!</blockquote><p>For the Printing program, let’s check that MyIO maintains substitution, by replacing each expression with its definition, recursively.</p><ul><li>Replace the value hello with its definition MyIO.putStr(“hello!”):</li></ul><blockquote><strong>-</strong> MyIO(() =&gt; println(“hello!”)).unsafeRun()<br><strong>+</strong> println(“hello!”)</blockquote><p>After recursively replacing the program’s expressions with its definitions, the body of the Printing program is equivalent to the expression println(“hello!”). So, yes, MyIO maintains substitution: after every evaluation, the meaning of the program is preserved.</p><h3>Cats Effect IO 🐱</h3><p>Most often we’ll use IO.delay to capture a side effect as an IO value:</p><blockquote>def delay[A](a: =&gt; A): IO[A]</blockquote><p>IO.delay takes a call-by-name (lazily-evaluated) argument, delaying the evaluation of the code:</p><blockquote>val hw: IO[Unit] = IO.delay(println(“Hello world!”))</blockquote><p><em>Delay the evaluation of this expression so that when it is (later) executed, it<br>prints Hello world to the console, and produces the value ().</em></p><p>IO.apply is an alias for IO.delay. This lets us write IO(…) instead of IO.delay(…), which is shorter:</p><p>When the effect is executed, what happens if the side effect throws an exception? For example, what happens if you create an IO value like this?</p><blockquote>val ohNoes: IO[Int] = IO.delay(throw new RuntimeException(“oh noes!”))</blockquote><p>Although it is less common, we can also construct IO values from existing “pure” values:</p><blockquote>val twelve: IO[Int] = IO.pure(12)</blockquote><p>Be careful! Do not perform any side effects when calling IO.pure, because they will be eagerly evaluated and that will break substitution. If you aren’t sure, use IO.delay or IO.apply to be safe.<br>We can also “lift” an exception into IO, as long as we provide the “expected” type of the IO had it succeeded, either explicitly or through type inference:</p><blockquote>val ohNoes: IO[Int] = IO.raiseError(new RuntimeException(“oh noes!”))</blockquote><h3>Transforming IO values 🌀</h3><p>Once we have IO values, we can call various methods to produce new IO values. These methods are often called combinators.</p><p><strong><em>IO is a functor; we can map over it:</em></strong></p><blockquote>IO(12).map(_ + 1)</blockquote><p><strong><em>IO is an applicative; we can mapN over two or more values:</em></strong></p><blockquote>(IO(12), IO(“hi”)).mapN((i, s) =&gt; s”$s: $i”)</blockquote><p><em>When executed, produces “hi: 12”</em></p><p><strong><em>IO is a monad; we can flatMap over it, or more conveniently, we can use a for-comprehension:</em></strong></p><blockquote>for {<br> i &lt;- IO(12)<br> j &lt;- IO(i + 1)<br>} yield j.toString</blockquote><h4>Error handling</h4><p>An IO computation can fail, either by throwing an exception during execution, or by capturing an existing exception via IO.raiseError. We can,<br>however, detect these failures and do something about it.</p><p><strong><em>Error-handling Decision Tree: </em></strong>If an error occurs in your IO[A] do you want to…</p><ul><li><em>Perform an effect?</em><br> Use onError(pf: PartialFunction[Throwable, IO[Unit]]): IO[A].</li><li><em>Transform any error into another error?</em><br> Use adaptError(pf: PartialFunction[Throwable, Throwable]): IO[A].</li><li><em>Transform any error into a successful value?</em><br> Use handleError(f: Throwable ⇒ A): IO[A].</li><li><em>Transform some kinds of errors into a successful value?</em><br> Use recover(pf: PartialFunction[Throwable, A]): IO[A].</li><li><em>Transform some kinds of errors into another effect?</em><br> Use recoverWith(pf: PartialFunction[Throwable, IO[A]]): IO[A].</li><li><em>Make errors visible but delay error-handling?</em><br> Use attempt: IO[Either[Throwable, A]].</li><li><em>Make errors visible but delay error-handling?</em><br> Use attempt: IO[Either[Throwable, A]].</li></ul><h3>Executing IO values 👨🏽‍🔧</h3><p>We’ve delayed any side effects by encapsulating them into an IO value. When we’re done composing our program we’ll finally execute the effects.</p><p>The most common unsafe method you’ll encounter is <strong>unsafeRunSync</strong>. Run means execute, and sync means synchronous execution; together they run the effects synchronously and return the result.</p><blockquote>def unsafeRunSync: A</blockquote><p>There is naturally an unsafeRunAsync, and other methods, but they are less frequently used. More common, however, is the unsafeToFuture method, which you may use to integrate your effectful code with legacy interfaces that may consume <em>scala.concurrent.Future:</em></p><blockquote>def unsafeToFuture: Future[A]</blockquote><p><strong><em>As a general rule, you should not be invoking any unsafe method in your code. When experimenting in the REPL or some other throw-away code, sure. Otherwise, don’t do it! Instead, you’ll delegate this responsibility to types like IOApp.</em></strong></p><h4>IO as an effect? 🤔</h4><ol><li>Does the type of the program tell us</li></ol><ul><li>what kind of effects the program will perform; -&gt;<strong><em> An IO represents a (possibly) side effecting computation.</em></strong></li><li>what type of value it will produce? -&gt;<strong><em> A value of type A, if the computation is successful.</em></strong></li></ul><p>2. When externally-visible side effects are required, is the effect description<br>separate from the execution?</p><ul><li>Externally-visible side effects are required: when executed, an IO<br>can do anything, including side effects.</li><li>We describe IO values by various constructors, and describe<br>compound effects by composing them with methods like map, mapN,<br>flatMap, and so on. The execution of the effect only happens when<br>an unsafe* method is called.</li></ul><blockquote>Therefore, IO is an effect!</blockquote><h4>Executing effects in applications with IOApp 🧚‍♀️</h4><p>Cats Effect provides the <strong>IOApp</strong> type for applications. IOApp is an executable Scala type — something that has a main method — that requires you to declare your effects as a single IO value, and it performs the task of executing those effects.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d4af95ee333b054c6eabc2296320c77f/href">https://medium.com/media/d4af95ee333b054c6eabc2296320c77f/href</a></iframe><p>How does IOApp execute the effects? Since an IO only executes if an unsafe method like unsafeRunSync is called, IOApp must invoke it for us.</p><p>Our application’s effect produces an ExitCode, an enumeration that abstracts over the exit code to return to the operating system when the application ends. Most applications will return ExitCode.Success, but<br>you can always return ExitCode.Error or construct an ExitCode with a numeric value, where non-zero values denote an error.</p><h3>My Conclusions 🤹🏼</h3><ul><li>IO effect type allows us to capture any side effect, but in a safe way, following our Effect Pattern.</li><li>Once we have IO values, we can call various methods to produce new IO values. These methods are often called combinators.</li><li>An IO computation can fail, either by throwing an exception during execution, or by capturing an existing exception.</li><li>As a general rule, you should not be invoking any unsafe method in your code.</li><li>Cats Effect provides the IOApp type for applications. Since an IO only executes if an unsafe method like unsafeRunSync is called, IOApp must invoke it for us.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2763f592386e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Learning Cats Effects — Undertstanding Effects ]]></title>
            <link>https://medium.com/@PerrottaFrancisco/learning-cats-effects-undertstanding-effects-e9291c15f9ba?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e9291c15f9ba</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[cats-effect]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Wed, 21 Dec 2022 13:01:51 GMT</pubDate>
            <atom:updated>2022-12-21T13:01:51.977Z</atom:updated>
            <content:encoded><![CDATA[<h3>Learning Cats Effects — Undertstanding Effects 😼</h3><p>Welcome to these notes that I will take on cats effects. My personal goal is to understand how cats effects handles concurrency and parallelism in order to learn ZIO with a good knowledge base. But for that i need tounderstand the complete library and and in this new series of posts we will delve into a book called Essential Effects.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XIRirqte5WBZ46RJ.jpg" /></figure><h3>What is cats Effects? 🧐</h3><p>In their documentation they are defined as the pure asynchronous runtime for Scala.</p><p><strong>Cats Effect is a Scala library that allows us to write composable and high-performance applications with pure functional programming.</strong></p><blockquote>We are using the <a href="https://typelevel.org/cats-effect/docs/getting-started">Cats Effects 3</a> library ⚠️</blockquote><h4>The keys for the library to achieve its goal</h4><ol><li><strong>Fast</strong>: Cats Effect provides lightweight fibers for asynchronous, highly concurrent applications.</li><li><strong>Reliable</strong>: Keep your applications up and running under high resource contention with automatic resource safety, backpressure, and cancellation of unnecessary work.</li><li><strong>Ecosystem</strong>: Cats Effect powers a thriving ecosystem of streaming frameworks, database layers, HTTP servers, and more…</li><li><strong>Community</strong>: Cats Effect is open source software maintained by the Typelevel community. You can join them on github or discord</li></ol><blockquote><em>“org.typelevel” %% “cats-effect” % “3.4.0”</em></blockquote><p>Cats Effects builds on a common set of functional programming techniques: functors, applicatives, and monads. If you are not familiar with these techniques you can review my series of posts on <a href="https://medium.com/@PerrottaFrancisco/introduction-at-category-theory-with-scala-and-cats-904753f04f52">Category Theory</a>.</p><p>To explore what effects are, and how we can leverage them, we’ll distinguish two aspects of code: <strong><em>computing values</em></strong> and<strong><em> interacting with the environment</em></strong>. At the same time, we’ll talk about how transparent, or not, our code can be in describing these aspects, and what we as programmers can do about it.</p><h3>Computing values 😳</h3><p>As programmers we write some code, say a method, and it computes a value that gets returned to the caller of that method:</p><blockquote><em>def plusOne(i: Int): Int = i + 1</em></blockquote><blockquote><em>val x = plusOne(plusOne(12))</em></blockquote><ol><li>plusOne has the type signature Int ⇒ Int, pronounced “Int to Int” or “plusOne is a function from Int to Int”.</li><li>x is a value. It is defined as the result of evaluating the expression plusOne(plusOne(12)).</li></ol><p>Let’s use <strong><em>substitution</em></strong> to evaluate this code.</p><blockquote><strong>val x = 14</strong></blockquote><p>To understand what plusOne does, you don’t have to look anywhere except the (literal) definition of plusOne. There are no references to anything outside of it. This is sometimes referred to as <strong><em>local reasoning</em></strong><em>.</em></p><p>Under substitution, programs mean the same thing if they evaluate to the same value. 13 + 1 means exactly the same thing as 14. So does plusOne(12 + 1), or even (12 + 1) + 1. This is known as <strong><em>referential transparency</em></strong>.</p><p><strong><em>Substitution</em></strong> is only a model of how actual evaluation occurs. It doesn’t handle every kind of expression.</p><p>When does substitution break down? 🤔</p><ul><li><strong><em>When printing to the console.</em></strong></li></ul><blockquote>- val x = println(“Hello world!”)</blockquote><blockquote>+ val x = ()</blockquote><ul><li><strong><em>When reading values from the outside world.</em></strong></li></ul><blockquote>- val name = readLine</blockquote><blockquote>+ val name = &lt;whatever you typed in the console&gt;</blockquote><ul><li><strong><em>When expressions refer to mutable variables.</em></strong></li></ul><blockquote>var i = 12</blockquote><blockquote>— val x = { i += 1; i }</blockquote><blockquote>+ val x = 13</blockquote><h3>Interacting with the environment 🌄</h3><p>The second aspect of effects, after computing values, is interacting with the environment. And as we’ve seen, this can break substitution. Environments can change, they are non-deterministic, so expressions involving them do not necessarily evaluate to the same value.</p><p>One way we can maintain the ability to reason about code is to localize the “impure” code that breaks substitution.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f531add11791062efbeecd88afcca93/href">https://medium.com/media/1f531add11791062efbeecd88afcca93/href</a></iframe><ul><li>We’ve used a mutable variable. The horrors! But nothing outside of sum can ever affect it. Its existence is localized to a single invocation.</li><li>When we evaluate the expression that uses sum, we get a deterministic answer. Substitution works at this level.</li></ul><p>Localization is a nice trick, but won’t work for everything that breaks substitution. We need side effects to actually do something in our programs.</p><h3>The Effect Pattern 🔮</h3><p>If we impose some conditions, we can tame the side effects into something safer; we’ll call these effects.</p><ol><li>The type of the program should tell us what kind of effects the program will perform, in addition to the type of the value it will produce.</li><li>If the behavior we want relies upon some externally-visible side effect, we separate describing the effects we want to happen from actually making them happen. We can freely substitute the description of effects until the point we run them.</li></ol><p><strong><em>We’ll call these conditions the Effect Pattern, and apply it to studying and describing the effects we use every day, and to new kinds of effects.</em></strong></p><h4>Effect Pattern Checklist</h4><ol><li>Does the type of the program tell us a. what kind of effects the program will perform; and b. what type of value it will produce?</li><li>2. When externally-visible side effects are required, is the effect description separate from the execution?</li></ol><h4>Example: Is Option an effect?</h4><p>Many languages, including Scala, allow the use of the null value to mean a value is missing. The programmer (you!) is then required to check if a value is null or not, otherwise the dreaded NullPointerException is thrown at runtime.</p><p>To prevent us from forgetting to check which case we are in, Scala offers another way to encode optionality, as an algebraic data type: <strong>Option</strong></p><ul><li>Does Option[A] tell us what kind of effects the program will perform, in addition to the type of the value it will produce?</li></ul><p>Yes: if we have a value of type Option[A], we know the effect is optionality from the name Option, and we know it may produce a value of type A from the type parameter A</p><ul><li>Are externally-visible side effects required?</li></ul><p>Not really. The Option data type is an interface representing optionality that maintains substitution. We can replace a method call with its implementation and the meaning of the program won’t change.</p><p>There is one exception — pun intended — where an externally-visible side effect might occur: Calling get on a None is a programmer error, and raises an exception which in turn may result in a stack trace being printed.</p><p>With these two criteria satisfied, we can say yes, Option[A] is an effect!</p><h4>Example: Is Future an effect?</h4><p>Future is known to have issues that aren’t easily seen. For example, look at this code, where we reference the same Future to run it twice:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/603c6bc3797f81f86e509959ed063474/href">https://medium.com/media/603c6bc3797f81f86e509959ed063474/href</a></iframe><p>What output is produced?</p><p>Hello World!</p><p>It is only printed once! Why is that? The reason is that the Future is scheduled to be run immediately upon construction. So the side effect will happen (almost) immediately, even when other “descriptive” operations — the subsequent print in the flatMap — happen later</p><p>is, we describe performing print twice, but the side effect is only executed once!</p><p>Compare this to what happens when we substitute the definition of print into twice:</p><blockquote>val twice = Future(println(“Hello World!”)) .flatMap(_ =&gt; Future(println(“Hello World!”)))</blockquote><p>Running it, we then see:</p><p>Hello World!</p><p>Hello World!</p><p><strong><em>This is why we say Future is not an effect: the substitution of expressions with their definitions doesn’t have the same meaning (Future does not separate effect description from execution: it is unsafe)</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*QGcFZSfFXJioMCmwsmOeiw.png" /></figure><h3>My Conclusions 🤹🏼</h3><ul><li>Cats Effects is a pure asynchronous runtime for Scala</li><li>To understand what an effect is we must understand 2 things, computing values and interacting with the environment.</li><li>When you have no references to anything outside of it, is sometimes referred to as local reasoning.</li><li>Under substitution, programs mean the same thing if they evaluate to the same value. This is known as referential transparency.</li><li>Substitution is only a model of how actual evaluation occurs. It doesn’t handle every kind of expression.</li><li>There are two conditions that the effects must meet.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://typelevel.org/cats-effect/">Cats Effect</a></li><li><a href="https://www.goodreads.com/book/show/57106498-essential-effects">Essential Effects</a></li><li><a href="https://rockthejvm.com/p/cats-effect">Rock the JVM</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e9291c15f9ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrent programming — Actor model (with Scala & Akka)]]></title>
            <link>https://medium.com/@PerrottaFrancisco/concurrent-programming-actor-model-with-scala-akka-85b8baf094ce?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/85b8baf094ce</guid>
            <category><![CDATA[actor-model]]></category>
            <category><![CDATA[concurrent-programming]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[akka]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Tue, 22 Nov 2022 13:01:34 GMT</pubDate>
            <atom:updated>2022-11-22T13:01:34.336Z</atom:updated>
            <content:encoded><![CDATA[<h3>Concurrent programming — Actor model (with Scala &amp; Akka🐶)</h3><p>In this fourteenth and last post about concurrent programming with scala we will continue writing down the most interesting things from the book “Learning concurrent programming with Scala” by Aleksandar Prokopec.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Agz5M131kRLPcf3X.png" /></figure><p>Throughout this series of posts (book 😬), we have concentrated on many different abstractions for concurrent programming. Most of these abstractions assume the presence of shared memory. Futures and promises, concurrent data structures, and software transactional<br>memory are best suited for shared memory systems.</p><p>While the shared memory assumption ensures that these facilities are efficient, it also limits them to applications running on a single computer.</p><p>In this last topic, we consider a programming model that is equally applicable to a shared-memory machine or a distributed system, namely,<br>the actor model.</p><h3>The actor model 🦸‍♂️</h3><p>In the actor model, the program is represented by a large number<br>of entities that execute computations independently, and communicate by passing messages. These independent entities are called <strong>actors.</strong></p><p><em>The actor model aims to resolve issues associated with using shared memory, such as data races or synchronization, by eliminating the need for shared memory altogether. </em>🤯</p><blockquote>The true advantage of the actor model becomes apparent when we start distributing the application across multiple computers.</blockquote><p>Implementing programs that span across multiple machines and devices that communicate through a computer network is called <strong><em>distributed programming</em></strong>. 🤯</p><p>The actor model allows you to write programs that run inside a single process, multiple processes on the same machine, or on multiple machines that are connected with a computer network.</p><p>Creating actors and sending messages is oblivious and independent of the location of the actor. In distributed programming, this is called <strong>location transparency</strong>. Location transparency allows you to design distributed systems without having the knowledge about the relationships in the computer network.</p><h3>Explaining the pattern as if it were a company 📊</h3><p>In the actor programming model, the program is run by a set of concurrentlyexecuting entities called actors.</p><p>Actor systems resemble human organizations, such as companies, governments, or other large institutions. To understand this similarity,<br>we consider the example of a large software company.</p><p>In a software company like Google, Microsoft, Amazon, or Typesafe, there are many goals that need to be achieved concurrently. Hundreds or thousands of employees work towards achieving these goals, and are usually organized in a hierarchical structure.</p><p>We will now identify these similarities by defining the parts of an actor system, and relating them to their analogs in the software company.</p><ul><li>An <strong><em>actor system</em></strong> is a hierarchical group of actors that share common configuration options. An actor system is responsible for creating new actors, locating actors within the actor system, and logging important events. <em>An actor system is an analog of the software company itself.</em></li><li>An <strong><em>actor class</em></strong> is a template that describes a state internal to the actor, and how the actor processes the messages. Multiple actors can be created from the same actor class. <em>An actor class is an analog of a specific position within the company, such as a software engineer, a marketing manager, or a recruiter.</em></li><li>An <strong><em>actor instance</em></strong> is an entity that exists at runtime and is capable of receiving messages. An actor instance might contain mutable state, and can send messages to other actor instances. <em>In the context of the software company example, an actor instance is analogous to a specific employee.</em></li><li>A <strong><em>message</em></strong> is a unit of communication that actors use to communicate. In Akka, any object can be a message. Messages are analogous to e-mails sent within the company. When an actor sends a message, it does not wait until some other actor receives the message. <em>Similarly, when an employee sends an e-mail</em></li><li>The <strong><em>mailbox</em></strong> is a part of memory that is used to buffer messages, specific to each actor instance. This buffer is necessary, as an actor instance can process only a single message at a time. <em>The mailbox corresponds to an e-mail client used by an employee.</em></li><li>An <strong><em>actor reference</em></strong> is an object that allows you to send messages to a specific actor. This object hides information about the location of the actor from the programmer. Actor might run within separate processes or on different computers. The actor reference allows you to send a message to an actor irrespective of where the actor is running. <em>From the software company perspective, an actor reference corresponds to the e-mail address of a specific employee. The employee might be in his office, on a business trip, or on a vacation, but the e-mail will eventually reach him no matter where he goes.</em></li><li>A <strong><em>dispatcher</em></strong> is a component that decides when actors are allowed to process messages, and lends them computational resources to do so. In Akka, every dispatcher is at the same time an execution context. The dispatcher ensures that actors with non-empty mailboxes eventually get run by a specific thread, and that these messages are handled serially. <em>A dispatcher is best compared to the e-mail answering policy in the software company</em></li></ul><h3>Creating actor systems and actors in Akka🔬</h3><blockquote>But… What is akka?</blockquote><p><em>Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala.</em></p><p>⚠️<em>(In the examples we will use akka classic)</em></p><p>Creating an actor instance in Akka roughly follows the same steps as creating an object instance.</p><ol><li>We need to define an actor class, which defines the behavior<br>of the actor. <em>An actor class is used to specify the behavior of an actor: it describes how the actor responds to messages and communicates with other actors, encapsulates actor state, and defines the actor’s startup and shutdown sequences.</em></li><li>We need to specify the configuration for a specific actor instance. <em>Declaring an actor class does not create a running actor instance. Instead, the actor class serves as a blueprint for creating actor instances. The same actor class can be shared by many actor instances.</em></li><li>We need to tell the actor system to instantiate the actor using the given<br>configuration. <em>To instantiate an actor, we pass an actor configuration to the actorOf method of the actor system.</em></li><li>The actor system then creates an actor instance and returns an actor<br>reference to that instance. <em>When creating a new actor, we can specify a unique name for the actor instance with the argument called name . Without explicitly specifying the name argument, the actor system automatically assigns a unique name to the new actor instance.</em></li></ol><h3><em>Akka Examples </em>🧐</h3><p>So, taking the previous points as a reference, we have:</p><h4>1. Definition</h4><p>We declare a new actor class by extending the Actor trait from the akka.actor package. This trait comes with a single abstract method receive. This partial function is used when an actor receives a message of Any type. If the partial function is not defined for the message, the message is discarded.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0480660b5bca19dd06f944a7020e2d84/href">https://medium.com/media/0480660b5bca19dd06f944a7020e2d84/href</a></iframe><h4>2. Configurations for Instances</h4><p>To create an actor instance in Akka, we need to pass information about the actor class to the actor system. However, an actor class such as HelloActor is not sufficient for creating an actor instance; we also need to specify the hello argument. To bundle the information required for creating an actor<br>instance, Akka uses objects called <strong>actor configurations</strong>.</p><p>In Akka, an actor configuration is represented with the <strong>Props</strong> class. A Props object encapsulates all the information required to create an actor instance, and can be serialized or sent over the network.</p><p>To create Props objects, it is a recommended practice to declare factory methods in the companion object of the actor class.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dcc2b04e2916b0d486e0b945645c9813/href">https://medium.com/media/dcc2b04e2916b0d486e0b945645c9813/href</a></iframe><p>The props method uses an overload of the Props.apply factory method, which takes a block of code by creating the HelloActor class. This block of code is invoked every time an actor system needs to create an actor instance. The propsAlt method uses another Props.apply overload, which creates an actor instance from the Class object of the actor class, and a list of constructor arguments. The two declarations are semantically equivalent.</p><h4>3. Initialization of the actor system</h4><p>Throughout this lasts points, we will use our custom actor system<br>instance called ourSystem . We define ourSystem using the ActorSystem.apply factory method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5d97fa93ac9cc0752a241dee24aacd9/href">https://medium.com/media/d5d97fa93ac9cc0752a241dee24aacd9/href</a></iframe><h4>4. Initialization of the actor system</h4><p>We can now create and run HelloActor by calling the actorOf method on the actor system.</p><p>The actorOf method does not return an instance of the HelloActor class. Instead, it returns an actor reference object of the type ActorRef .</p><p>After creating a HelloActor instance hiActor , which recognizes the hi messages, we send it a message hi . To send a message to an Akka actor, we use the ! operator</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c5c699190573bab9399c62d996e0c10/href">https://medium.com/media/2c5c699190573bab9399c62d996e0c10/href</a></iframe><p>Upon running this program, the hiActor instance first prints that it received a hi message. After one second, it prints that it received a hola , an unexpected message, and terminates.</p><h3>My Conclusions 🤹🏼</h3><ul><li>In the actor model, the program is represented by a large number<br>of entities that execute computations independently, and communicate by passing messages. These independent entities are called actors<strong>.</strong></li><li>The actor model aims to resolve issues associated with using shared memory, such as data races or synchronization, by eliminating the need for shared memory altogether.</li><li>Implementing programs that span across multiple machines and devices that communicate through a computer network is called distributed programming.</li></ul><h3><strong>Closing this series </strong>😁</h3><p>I will not delve into it because I am already very familiar with this pattern and as I mentioned in several places in this series of posts, I wanted to finish understanding everything that happens in a concurrent system in the JVM.</p><p>⚠️ <strong><em>If you want to go deeper into this widely used and powerful pattern, I recommend the posts and videos by </em></strong><a href="https://rockthejvm.com/"><strong><em>DANIEL CIOCÎRLAN</em></strong></a></p><p>You can also continue reading the book where there are many things that I did not mention.</p><p>I hope it has helped you as much as it has helped me to understand many key concepts and thanks to everyone who supported this series of posts that work like a notebook where I write down what I was learning.</p><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.amazon.com/Learning-Concurrent-Programming-Aleksandar-Prokopec/dp/1783281413">Learning Concurrent Programming in Scala</a></li><li><a href="https://akka.io/">Akka</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=85b8baf094ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Concurrent programming — Software Transactional Memory (with Scala )]]></title>
            <link>https://medium.com/@PerrottaFrancisco/concurrent-programming-software-transactional-memory-with-scala-e1cf05af43d4?source=rss-5b40647aa7a0------2</link>
            <guid isPermaLink="false">https://medium.com/p/e1cf05af43d4</guid>
            <category><![CDATA[smt]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[concurrent-programming]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Francisco Perrotta]]></dc:creator>
            <pubDate>Tue, 15 Nov 2022 13:02:39 GMT</pubDate>
            <atom:updated>2022-11-15T13:02:39.849Z</atom:updated>
            <content:encoded><![CDATA[<h3>Concurrent programming — Software Transactional Memory (with Scala 🐶)</h3><p>In this thirteenth post about concurrent programming with scala we will continue writing down the most interesting things from the book “Learning concurrent programming with Scala” by Aleksandar Prokopec.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Vcg4UhYApiGydHSu.jpg" /></figure><h3>The trouble with atomic variables 😨</h3><p>Atomic variables are one of the fundamental synchronization mechanisms.</p><p>Atomic variables can ensure that no thread concurrently modifies the variable between a read and a write. At the same time, atomic variables reduce the risk of deadlocks. Regardless of their advantages, there<br>are situations when using atomic variables is not satisfactory.</p><p>As we have learned in the others posts, we need to use atomic operations<br>on atomic variables to ensure that their values consistently change from one state to another.</p><p>Although specific atomic operations are themselves atomic and occur at a single point in time, invoking multiple atomic operations is typically not atomic.</p><p>When multiple threads simultaneously execute multiple atomic operations, the operations might interleave in unforeseen ways and lead to the same kind of race conditions that result from using volatile variables.</p><p><strong><em>Reading multiple atomic variables is not an atomic operation and<br>it can observe the program data in an inconsistent state.</em></strong></p><p>Unlike atomic variables, multiple synchronized statements can be used<br>together more easily. We can modify multiple fields of an object when we use the synchronized statement, and we can even nest multiple synchronized statements. We are thus left with a dilemma.</p><p><strong><em>We can use atomic variables and risk race conditions when composing larger programs, or we can revert to using the synchronized statement, but risk deadlocks. Luckily, the STM is a technology that offers the best of both worlds: it allows you to compose simple atomic operations into more complex atomic operations, without the risk of deadlocks.</em></strong></p><h3>Software Transactional Memory 🗂</h3><p>Software Transactional Memory (STM), a concurrency control mechanism for controlling access to shared memory, which greatly reduces the risk of deadlocks and races.</p><p>An STM is used to designate critical sections of the code. Instead of using locks in order to protect critical sections, STM tracks the reads and writes to shared memory, and serializes critical sections with interleaving reads and writes. The synchronized statement is replaced with the atomic blocks that express segments of the program that need to be executed in isolation. STM is safer and easier to use, and at the same time, guarantees relatively<br>good scalability.</p><p>Since STM as a part of the programming language requires intrusive changes to a language, many software transactional memories are implemented as libraries. ScalaSTM is one such example.</p><ul><li>ScalaSTM was authored by a group of STM experts that agreed on a standardized set of APIs and features.</li><li>ScalaSTM API is designed for multiple STM implementations, and comes with an efficient default implementation.</li></ul><p>The <strong><em>atomic statement</em></strong> is a fundamental abstraction at the core of every STM. When the program executes a block of code marked with atomic , it starts a memory transaction: a sequence of reads and writes to memory which occur atomically for other threads in the program. The atomic statement is similar to the synchronized statement, and ensures that a block of code executes in isolation, without the interference of other<br>threads, thus avoiding race conditions. Unlike the synchronized statement, the atomic statement does not cause deadlocks.</p><p>The ways in which an STM implements deadlock-freedom, and ensures that no two threads simultaneously modify the same memory locations are quite complex.</p><h3><strong>Using Software Transactional Memory </strong>📝</h3><p><em>swap</em> and <em>inc</em> , show how to use the atomic statement on a high level. The swap method atomically exchanges the contents of two memory locations, a and b . Between the time that a thread reads the memory location a (or b )<br>and the time that the atomic statement ends, no other thread can effectively modify the value at location a (or b ). Similarly, the inc method atomically increments the integer value at the memory location a . When a thread, which calls inc , reads the value of a in the atomic statement, no other thread can change the value of the memory location a until the atomic statement ends.</p><p>To illustrate how a memory transaction works, we consider the scenario in which two threads, T1 and T2, simultaneously call the swap and inc methods. Since boththe a tomic statements in these methods modify the memory location a , the executionresults in a runtime transactional conflict. During the execution of the program, the STM detects that the entries in the transactional logs overlap: the transaction associated with the swap method has both memory locations a and b in its read and<br>write sets, while the inc method has a in its read and write sets. This indicates a potential conflict. Both the transactions can be rolled back, and then executed serially one after another, as shown in the following diagram:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*9VgBoYuKDD5rrVx0uBNUtw.png" /></figure><p>In some STMs, the atomic statement tracks all the reads and writes to the memory.</p><p>ScalaSTM only tracks specially marked memory locations within transactions.</p><p>In ScalaSTM, the effects of the atomic statement are limited to special objects called transactional references.</p><p>Before we begin using STM in Scala, we need to add an external dependency to our project, since ScalaSTM is not a part of the Scala standard library:</p><pre>libraryDependencies += &quot;org.scala-stm&quot; %% &quot;scala-stm&quot; % &quot;0.11.0&quot;</pre><h3>Transactional references 📌</h3><p><strong><em>A transactional reference is a memory location that provides transactional read and write access to a single memory location.</em></strong></p><p>In ScalaSTM, transactional references to the values of type T<br>are encapsulated within the objects of type <strong><em>Ref[T]</em></strong></p><p>To instantiate a Ref object, we use the Ref.apply factory method on the Ref<br>companion object.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10cd408c98ebf52f135cb67958132b12/href">https://medium.com/media/10cd408c98ebf52f135cb67958132b12/href</a></iframe><p>Calling the apply method on a transactional reference returns its value, and calling the update method modifies it. However, we cannot call these methods from outside of a transaction. The apply and update methods take an implicit argument of type InTxn (which stands for “in transaction”), which designates that a transaction is under way.</p><p>This constraint protects us from accidentally circumventing the ScalaSTM safety mechanisms.</p><h3><strong>Using the atomic statement </strong>🔍</h3><p>In ScalaSTM, the atomic statement takes a block of type InTxn =&gt; T , where InTxn is the type of the aforementionedtransaction object, and T is the type of the return value of the transaction.</p><h4>Let’s see an example</h4><p>In this example we are going to add a new url to a list of urls having a variable called clen that serves as a quick way to get the required size of the array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/24c596ddf27cd0de9134bc05c58362e3/href">https://medium.com/media/24c596ddf27cd0de9134bc05c58362e3/href</a></iframe><ol><li>It first reads the value of the urls list, prepends a new url to the list, and assigns the updated list back to urls .</li><li>it reads the current value of the total character length clen , increments it by the length of the new URL, and assigns the new value back to clen .</li></ol><p>Note that looks almost identical to a single-threaded implementation.</p><p>An important limitation of the atomic statement in ScalaSTM is that it does not track reads and writes to ordinary local variables and object fields. As we will see later, these are considered as arbitrary side effects, and are not allowed inside the transaction.</p><p><strong><em>When encoding a complex program state, use multiple transactional<br>references. To atomically perform multiple changes on the program<br>state, use the atomic statement.</em></strong></p><h3>My Conclusions 🤹🏼</h3><ul><li>Reading multiple atomic variables is not an atomic operation and<br>it can observe the program data in an inconsistent state.</li><li>We can use atomic variables and risk race conditions when composing larger programs, or we can revert to using the synchronized statement, but risk deadlocks.</li><li>STM is a concurrency control mechanism for controlling access to shared memory, which greatly reduces the risk of deadlocks and races.</li><li>STM tracks the reads and writes to shared memory, and serializes critical sections with interleaving reads and writes.</li><li>The synchronized statement is replaced with the atomic blocks that express segments of the program that need to be executed in isolation.</li><li>The <strong><em>atomic statement</em></strong> is a fundamental abstraction at the core of every STM.</li><li>The<strong> atomic statement</strong> is similar to the synchronized statement, and ensures that a block of code executes in isolation, without the interference of other threads, thus avoiding race conditions.</li><li>Unlike the synchronized statement, <strong>the atomic statement</strong> does not cause deadlocks.</li><li>A <strong>transactional reference</strong> is a memory location that provides transactional read and write access to a single memory location.</li><li>When encoding a complex program state, use multiple transactional<br>references. To atomically perform multiple changes on the program<br>state, use the atomic statement.</li></ul><h3>Bibliography 🕵🏿‍♂️</h3><ul><li><a href="https://www.amazon.com/Learning-Concurrent-Programming-Aleksandar-Prokopec/dp/1783281413">Learning Concurrent Programming in Scala</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e1cf05af43d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>