<?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 Amitay Horwitz on Medium]]></title>
        <description><![CDATA[Stories by Amitay Horwitz on Medium]]></description>
        <link>https://medium.com/@amitayh?source=rss-7b83bce94554------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*g5OBU7gZTmbnhZqz.jpg</url>
            <title>Stories by Amitay Horwitz on Medium</title>
            <link>https://medium.com/@amitayh?source=rss-7b83bce94554------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:16:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amitayh/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[Creating a dead simple CountDownLatch with ZIO]]></title>
            <link>https://medium.com/wix-engineering/creating-a-dead-simple-countdownlatch-with-zio-c4212ecf9198?source=rss-7b83bce94554------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4212ecf9198</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[zio]]></category>
            <category><![CDATA[concurrency]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Amitay Horwitz]]></dc:creator>
            <pubDate>Sun, 16 Feb 2020 08:30:55 GMT</pubDate>
            <atom:updated>2020-02-16T08:30:55.661Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*yeo6n0c1mwTsSp08" /><figcaption>Photo by <a href="https://unsplash.com/@cbarbalis?utm_source=medium&amp;utm_medium=referral">Chris Barbalis</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In this post I’ll explain what a CountDownLatch is, and how <a href="https://zio.dev/">ZIO</a> enables you to create concurrency primitives which are efficient, non-blocking and simple.</p><ul><li><em>Note: I assume you have basic understanding of what ZIO is</em></li></ul><h3>Background</h3><p>At <a href="https://www.wix.com/">Wix</a>, we love testing. But testing concurrent code is often more difficult than writing it.</p><p>I recently needed to write a test which involves waiting until a number of async callbacks are called before making my assertion. More precisely, the system I’m testing involves async producer / consumer (or publish / subscribe) model, and the test looks something like this (with <a href="https://zio.dev/docs/usecases/usecases_testing">zio-test</a>):</p><pre><em>testM</em>(&quot;consume produced records&quot;) {<br>  for {<br>    records &lt;- Ref.<em>make</em>(<em>Set</em>.empty[Record])<br>    _ &lt;- consumeFrom(topic1) { record =&gt;<br>      records.update(_ + record)<br>    }.fork<br>    _ &lt;- consumeFrom(topic2) { record =&gt;<br>      records.update(_ + record)<br>    }.fork<br>    _ &lt;- produceTo(topic1 -&gt; record1, topic2 -&gt; record2)<br>    consumed &lt;- records.get<br>  } yield <em>assert</em>(consumed, <em>contains</em>(record1) &amp;&amp; <em>contains</em>(record2))<br>}</pre><p>The problem here is that if the produceTo and consumeFrom functions actually run asynchronously my test will be flaky, because by the time I call records.get the result might still be empty.</p><p>We don’t like flaky tests. The simple solution is to poll, or retry the assertion at some constant interval until it succeeds. ZIO provides wonderful tools for such use cases, like the repeat combinator:</p><pre><em>testM</em>(&quot;consume produced records&quot;) {<br>  for {<br>    records &lt;- Ref.make(Set.empty[Record])<br>    _ &lt;- consumeFrom(topic1) { record =&gt;<br>      records.update(_ + record)<br>    }.fork<br>    _ &lt;- consumeFrom(topic2) { record =&gt;<br>      records.update(_ + record)<br>    }.fork<br>    _ &lt;- produceTo(topic1 -&gt; record1, topic2 -&gt; record2)<br><strong>    (_, consumed) &lt;- records.get.repeat(<br>      Schedule.spaced(100.millis) &amp;&amp;<br>        Schedule.doUntil(_.size == 2))</strong><br>  } yield assert(consumed, contains(record1) &amp;&amp; contains(record2))<br>}</pre><p>But this made the test much more cumbersome, and the polling strategy feels somewhat messy.</p><h3>Looking back to Java</h3><p>Java’s primary way of tackling concurrency is by using threads. However, writing multithreaded code is notoriously difficult and error prone. To make writing concurrent code more sane, there are many utilities available in the java.util.concurrent package. One of these is the CountDownLatch.</p><p>As described in the <a href="https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/concurrent/CountDownLatch.html">JavaDoc</a>:</p><blockquote>A synchronization aid that allows one or more threads to wait until a set of operations being performed in other threads completes.</blockquote><blockquote>A <em>CountDownLatch</em> is initialized with a given count. The <em>await</em> method block until the current count reaches zero due to invocations of the <em>countDown</em> method, after which all waiting threads are released and any subsequent invocations of <em>await</em> return immediately.</blockquote><p>My use case seems like a perfect fit for a CountDownLatch, however, ZIO doesn’t provide one out of the box. It does provide other primitives that we can build on top of, such as Promise, Ref, Queue, Semaphore and others. So let’s try to create our own concurrent and non-blocking CountDownLatch using ZIO!</p><h3>The API</h3><p>The API we need is more simplified compared to Java’s CountDownLatch and looks something like this:</p><pre>trait CountDownLatch {<br>  def countDown: UIO[Unit]<br>  def await: UIO[Unit]<br>}</pre><p>We can omit some of the original methods, such as await with timeout:</p><pre>public boolean await(long timeout, TimeUnit unit)<br>  throws InterruptedException</pre><p>Because we could easily achieve these semantics using ZIO’s built-in combinators, which demonstrate the power of having a composable functional API:</p><pre>latch.await.timeout(1.second)</pre><p>For the actual implementation, all we need is a Ref to keep the current count, and a Promise to signal when the count reached 0. Creating a Ref and a Promise is an effectful operation, which means creating our CountDownLatch will also be effectful (which makes sense, because it keeps internal state).</p><pre>object CountDownLatch {<br>  def make(count: Int): UIO[CountDownLatch] = for {<br>    ready &lt;- Promise.<em>make</em>[Nothing, Unit]<br>    ref &lt;- Ref.<em>make</em>(count)<br>  } yield new CountDownLatch {<br>    override def countDown: UIO[Unit] =<br>      ref.update(_ - 1).flatMap {<br>        case 0 =&gt; ready.succeed(()).unit<br>        case _ =&gt; ZIO.<em>unit<br>      </em>}<br><br>    override def await: UIO[Unit] = ready.await<br>  }<br>}</pre><p>With just a few lines of code, we have created our own CountDownLatch! Let’s rewrite our original test using our new utility:</p><pre><em>testM</em>(&quot;consume produced records&quot;) {<br>  for {<br>    records &lt;- Ref.<em>make</em>(<em>Set</em>.empty[Record])<br>    latch &lt;- CountDownLatch.<em>make</em>(2)<br>    _ &lt;- consumeFrom(topic1) { record =&gt;<br>      records.update(_ + record) *&gt; <strong>latch.countDown</strong><br>    }.fork<br>    _ &lt;- consumeFrom(topic2) { record =&gt;<br>      records.update(_ + record) *&gt; <strong>latch.countDown</strong><br>    }.fork<br>    _ &lt;- produceTo(topic1 -&gt; record1, topic2 -&gt; record2)<br>    _ &lt;- <strong>latch.await</strong><br>    consumed &lt;- records.get<br>  } yield <em>assert</em>(consumed, <em>contains</em>(record1) &amp;&amp; <em>contains</em>(record2))<br>}</pre><p>Now we can easily synchronize the behaviour of our tests, leading to a more reliable and consistent test suite.</p><h3>Conclusion</h3><p>Once again ZIO proves its value when writing asynchronous and concurrent code. We created a simple and non-blocking CountDownLatch with very little code, which is valuable for making our test suite more robust.</p><p>Read about similar experiences from my colleagues:</p><ul><li><a href="https://medium.com/wix-engineering/building-a-super-easy-rate-limiter-with-zio-88f1ccb49776">Building a Super Easy Rate Limiter with ZIO</a> / <a href="https://medium.com/u/82980dc9e996">Dmitry Karlinsky</a></li><li><a href="https://medium.com/wix-engineering/easily-create-new-sync-types-with-zio-stm-9a4feeb2c427">Easily Create New Sync Types with ZIO STM</a> / <a href="https://medium.com/u/32e883029628">Natan Silnitsky</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4212ecf9198" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wix-engineering/creating-a-dead-simple-countdownlatch-with-zio-c4212ecf9198">Creating a dead simple CountDownLatch with ZIO</a> was originally published in <a href="https://medium.com/wix-engineering">Wix Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functional Testing with Tagless-Final]]></title>
            <link>https://medium.com/wix-engineering/functional-testing-with-tagless-final-50eeacf5df6?source=rss-7b83bce94554------2</link>
            <guid isPermaLink="false">https://medium.com/p/50eeacf5df6</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[final-tagless]]></category>
            <dc:creator><![CDATA[Amitay Horwitz]]></dc:creator>
            <pubDate>Sun, 10 Mar 2019 14:07:41 GMT</pubDate>
            <atom:updated>2019-04-08T08:58:41.551Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9aIvU8DO3WZGfCcPBELEcA.jpeg" /></figure><p>Although it was <a href="https://skillsmatter.com/skillscasts/13247-scala-matters">recently pronounced dead</a> by <a href="https://twitter.com/jdegoes">John De Goes</a>, <a href="https://www.youtube.com/watch?v=sxudIMiOo68">tagless-final</a> is still a popular technique for writing purely functional programs in Scala. It allows us to explicitly describe which effects are needed by each part of the program, giving us the ability to reason about what a function can, and more importantly, can’t do. Another benefit of using tagless-final is the fact we abstract over our effect type, which means we can relatively easily swap it later for a different implementation that might have different characteristics, for example, better performance or an async execution model (given we have all the type-class instances needed by our program).</p><p>This allows us to have our program run using Scala’s built-in Future and over time migrate to <a href="https://typelevel.org/cats-effect/">Cats-Effect IO</a>, or the shiny new <a href="https://scalaz.github.io/scalaz-zio/">Scalaz ZIO</a> — all without changing our code.</p><p>However, in practice we don’t often change the effect we use in our production environment. What I have found useful about using tagless-final in a real-world project, is the ability to test my application in a purely functional setting. The advantages of this approach are very compelling:</p><ul><li>I can easily write tests that exercise the entire system. I hardly write unit tests anymore, which means I don’t need to mock out anything — and I get very high level of confidence in my code and the integration between the different components.</li><li>Since my tests are more high-level, I get more refactoring opportunities. Having many, fragmented unit tests across the codebase usually couple our tests with implementation details making it harder to refactor.</li><li>These tests run entirely in memory, without any I/O or shared state. This means they run fast .VERY FAST. And because they’re totally independent, running tests in parallel is trivial.</li><li>Having an isolated environment for tests to run, in which you can control anything from time to randomness, means that the tests are very predictable. I can’t remember having a single flaky test since starting to use this pattern.</li></ul><p>I would like to share some of the lessons learned on how to do testing using tagless-final, which hopefully you’ll find useful as well.</p><p>Before we begin, we need some realistic, yet simplified example to work with. So let’s imagine we’re asked to create an endpoint in our web application, which given a user ID will fetch user profile information and a list of user’s orders. If some error occurs, we want to log it before failing. We will need three algebras to work with:</p><pre><strong>trait</strong> Users[F[_]] {<br>  def profileFor(userId: UserId): F[UserProfile]<br>}</pre><pre><strong>trait</strong> Orders[F[_]] {<br>  def ordersFor(userId: UserId): F[List[Order]]<br>}</pre><pre><strong>trait</strong> Logging[F[_]] {<br>  def error(e: Throwable): F[Unit]<br>}</pre><p>In production, Users and Orders could either use an internal database to fetch the information, or call out to a different micro-service. This makes no difference for our piece of business logic, and these implementation details are irrelevant for testing our code.</p><p>A straight-forward implementation might look something like this (using the <a href="https://typelevel.org/cats/">Cats</a> library for all the FP goodness):</p><pre><strong>import </strong>cats.implicits._</pre><pre><strong>type </strong>MonadThrowable[F[_]] = MonadError[F, Throwable]</pre><pre><strong>def </strong>fetchUserInformation<br>  [F[_]: MonadThrowable: Users: Orders: Logging]<br>  (userId: UserId): F[UserInformation] = {<br>  <strong>val </strong>result = <strong>for </strong>{<br>    profile &lt;- <em>Users</em>[F].profileFor(userId)<br>    orders &lt;- <em>Orders</em>[F].ordersFor(userId)<br>  } <strong>yield </strong>UserInformation.<em>from</em>(profile, orders)<br>  <br>  result.onError {<br>    <strong>case </strong>e =&gt; <em>Logging</em>[F].error(e)<br>  }<br>}</pre><h4>First attempt</h4><p>So how would we go about testing this function? We must first decide on what will be our effect type F[_]. To do that, we need to understand the capabilities we need from our effect. We see that we need to read some “external” data about the user profiles and orders. The obvious FP solution for this sort of problem is using the Reader monad. This might look something like this:</p><pre><strong>import </strong>cats.data._</pre><pre><strong>case class </strong>TestEnv(<br>  profiles: Map[UserId, UserProfile],<br>  orders: Map[UserId, List[Order]])</pre><pre><strong>type </strong>Test[A] = Reader[TestEnv, A]</pre><p>By doing so, we are now able to provide type-class instances for Users and Orders. However, we don’t only need to read from our environment, but also to write to it (with the Logging algebra).</p><h4>Adding some state</h4><p>FP offers an elegant solution for this problem as well, with the State monad. The State monad allows us to read information from some environment S, and also modify it before producing a value of type A. So, in essence, it’s a function S =&gt; (S, A). We’ll modify our TestEnv and Test effect to accommodate for these changes. We’ll also add a few helpers to TestEnv which will make our tests cleaner later on:</p><pre><strong>case class </strong>TestEnv(<br>  profiles: Map[UserId, UserProfile],<br>  orders: Map[UserId, List[Order]],<br>  loggedErrors: List[Throwable]) {</pre><pre>  <strong>def </strong>withProfile(profile: UserProfile): TestEnv =<br>    copy(profiles = profiles + (profile.userId -&gt; profile))</pre><pre>  <strong>def </strong>withOrder(order: Order): TestEnv = {<br>    <strong>val </strong>updatedUserOrders = order :: userOrders(order.userId)<br>    copy(orders = orders + (order.userId -&gt; updatedUserOrders))<br>  }<br>  <br>  <strong>def </strong>logError(e: Throwable): TestEnv =<br>    copy(loggedErrors = e :: loggedErrors)</pre><pre>  <strong>def </strong>userOrders(userId: UserId): List[Order] =<br>    orders.getOrElse(userId, <em>Nil</em>)</pre><pre>}</pre><pre><strong>object </strong>TestEnv {<br>  <strong>final val </strong><em>Empty </em>= <em>TestEnv</em>(<em>Map</em>.<em>empty</em>, <em>Map</em>.<em>empty</em>, <em>Nil</em>)<br>}</pre><pre><strong>type </strong>Test[A] = State[TestEnv, A]</pre><p>We are now able to provide type-class instances, or interpreters, for Users, Orders and Logging like so:</p><pre><strong>implicit val </strong><em>usersTest</em>: Users[Test] = <strong>new </strong>Users[Test] {<br>  <strong>override def </strong>profileFor(userId: UserId): Test[UserProfile] =<br>    State.inspect(_.profiles(userId))<br>}</pre><pre><strong>implicit val </strong><em>ordersTest</em>: Orders[Test] = <strong>new </strong>Orders[Test] {<br>  <strong>override def </strong>ordersFor(userId: UserId): Test[List[Order]] =<br>    State.inspect(_.userOrders(userId))<br>}</pre><pre><strong>implicit val </strong><em>loggingTest</em>: Logging[Test] = <strong>new </strong>Logging[Test] {<br>  <strong>override def </strong>error(e: Throwable): Test[Unit] =<br>    State.modify(_.logError(e))<br>}</pre><h4>Handling errors</h4><p>Cats library provides us with a Monad instance for State out of the box, however we are still missing the instance for MonadError[Test, Throwable]. Our chosen effect type is not suitable for dealing with errors, so we need to tweak it a bit. State[S, A] is actually a type alias for StateT[Eval, S, A]. The most basic wrapper for dealing with errors is Either. So we can redefine our test effect like so:</p><pre><strong>type </strong>EitherThrowableOr[A] = Either[Throwable, A]<br><strong>type </strong>Test[A] = StateT[EitherThrowableOr, TestEnv, A]</pre><pre>// Or with kind-projector compiler plugin:<br><strong>// type</strong> Test[A] = StateT[Either[Throwable, ?], TestEnv, A]</pre><p>This simple change gives us 2 important things:</p><ol><li>Cats can now derive a type-class instance for MonadError[Test, Throwable]</li><li>We are able refine our implementation of Users[Test] to handle the case where a user for requested ID is missing:</li></ol><pre><strong>case class </strong>UserNotFound(userId: UserId)<br>  <strong>extends </strong>RuntimeException(s&quot;User with ID <strong>$</strong>userId does not exist&quot;)</pre><pre><strong>implicit val </strong><em>usersTest</em>: Users[Test] = <strong>new </strong>Users[Test] {<br>  <strong>override def </strong>profileFor(userId: UserId): Test[UserProfile] =<br>    StateT.inspectF { env =&gt;<br>      env.profiles.get(userId) <strong>match </strong>{<br>        <strong>case </strong><em>Some</em>(profile) =&gt; <em>Right</em>(profile)<br>        <strong>case </strong>None =&gt; <em>Left</em>(<em>UserNotFound</em>(userId))<br>      }<br>    }<br>}</pre><p>Finally, we can write a test to check the fetchUserInformation function. These code examples will use the <a href="https://etorreborre.github.io/specs2/">Specs²</a> testing library, but can be written with other tools easily as well.</p><pre><strong>import </strong>org.specs2.mutable.Specification</pre><pre><strong>class </strong>UserInformationSpec <strong>extends </strong>Specification {</pre><pre>  &quot;fetch user name and orders by ID&quot; in {<br>    <strong>val </strong>userId = <em>UserId</em>(&quot;user-1234&quot;)<br>    <strong>val </strong>env = TestEnv.<em>Empty<br>      </em>.withProfile(<em>UserProfile</em>(userId, &quot;John Doe&quot;))<br>      .withOrder(<em>Order</em>(userId, <em>OrderId</em>(&quot;order-1&quot;)))<br>      .withOrder(<em>Order</em>(userId, <em>OrderId</em>(&quot;order-2&quot;)))</pre><pre>    <strong>val </strong>result = <em>fetchUserInformation</em>[Test](userId)</pre><pre>    result.runA(env) must beRight(<br>      <em>haveUserName</em>(&quot;John Doe&quot;) and<br>        <em>haveOrders</em>(<em>OrderId</em>(&quot;order-1&quot;), <em>OrderId</em>(&quot;order-2&quot;)))<br>  }</pre><pre>}</pre><p>That’s pretty cool — we can set up the exact conditions we want in our test, by modifying the environment we run in. However, we find ourselves in a problem when trying to test the error reporting we included in our function:</p><pre><strong>class </strong>UserInformationSpec <strong>extends </strong>Specification {</pre><pre>  &quot;fetch user name and orders by ID&quot; in { ... }</pre><pre>  &quot;log an error if user does not exists&quot; in {<br>    <strong>val </strong>userId = <em>UserId</em>(&quot;user-1234&quot;)<br>    <strong>val </strong>env = TestEnv.<em>Empty </em>// No users here</pre><pre>    <strong>val </strong>result = <em>fetchUserInformation</em>[Test](userId)</pre><pre>    result.run(env) must beLeft(<em>UserNotFound</em>(userId))<br>  }</pre><pre>}</pre><p>We are able to get out our error, wrapped in a Left, as expected. But we are unable to examine our TestEnv in case of failures — so we can’t check that our error was in fact logged correctly. We can understand why that’s the case when we expand the definition of our Test effect:</p><pre>Test[A]                                    --&gt;<br>StateT[Either[Throwable, ?], TestEnv, A]   --&gt;<br>TestEnv =&gt; Either[Throwable, (TestEnv, A)]</pre><p>As we can see, when we raise an error, we will get it back in the left part of the Either but we lose the resulting environment. We can only check what happened with our environment in the right part of the Either — namely, in the success case.</p><h4>Reordering our effects</h4><p>In order solve this, we must tweak our effect type once again. What we want to do, is replace TestEnv =&gt; Either[Throwable, (TestEnv, A)] with something like TestEnv =&gt; (TestEnv, Either[Throwable, A]). We still preserve our ability to raise errors — but are now able to examine the TestEnv in both success and failure cases. We can achieve this by turning our effects stack inside-out, using the EitherT monad transformer:</p><pre><strong>type </strong>Test[A] = EitherT[State[TestEnv, ?], Throwable, A]</pre><p>This will require us to re-implement our type-class instances once again to fit the new structure:</p><pre><strong>implicit val </strong><em>usersTest</em>: Users[Test] = <strong>new </strong>Users[Test] {<br>  <strong>override def </strong>profileFor(userId: UserId): Test[UserProfile] =<br>    EitherT {<br>      State.inspect { env =&gt;<br>        env.profiles.get(userId) <strong>match </strong>{<br>          <strong>case </strong><em>Some</em>(profile) =&gt; <em>Right</em>(profile)<br>          <strong>case </strong>None =&gt; <em>Left</em>(<em>UserNotFound</em>(userId))<br>        }<br>      }<br>    }<br>}<br><br><strong>implicit val </strong><em>ordersTest</em>: Orders[Test] = <strong>new </strong>Orders[Test] {<br>  <strong>override def </strong>ordersFor(userId: UserId): Test[List[Order]] =<br>    EitherT.<em>liftF</em>(State.inspect(_.userOrders(userId)))<br>}<br><br><strong>implicit val </strong><em>loggingTest</em>: Logging[Test] = <strong>new </strong>Logging[Test] {<br>  <strong>override def </strong>error(e: Throwable): Test[Unit] =<br>    EitherT(State(env =&gt; (env.logError(e), <em>Right</em>(()))))<br>}</pre><p>Luckily for us — Cats can still derive an instance for<br>MonadError[Test, Throwable] completely automagically!</p><p>After getting to this final form of effect type for testing, we can easily write tests that check both success and failure cases, and assert on what happened to our environment in both:</p><pre><strong>class </strong>UserInformationSpec <strong>extends </strong>Specification {<br><br>  &quot;fetch user name and orders by ID&quot; in { ... }<br><br>  &quot;log an error if user does not exists&quot; in {<br>    <strong>val </strong>userId = <em>UserId</em>(&quot;user-1234&quot;)<br>    <strong>val </strong>env = TestEnv.<em>Empty </em>// No users here<br><br>    <strong>val </strong>result = <em>fetchUserInformation</em>[Test](userId)<br><br>    result.value.runS(env).value must<br>      <em>containLoggedError</em>(<em>UserNotFound</em>(userId))<br>  }<br><br>}</pre><h3>Taking this idea further</h3><p>I have found this technique extremely useful for testing anything from simple functions to complex business flows in my applications. However, we sometimes require more from our effect types. If we revisit our implementation of fetchUserInformation we realise that fetching user’s profile and list of orders are actually independent operations, and sequencing them like this is wasteful. We can run these actions concurrently, combining the results afterwards, like so:</p><pre><strong>def </strong>fetchUserInformation<br>  [F[_]: Concurrent: Users: Orders: Logging]<br>  (userId: UserId): F[UserInformation] = {<br>  <strong>val </strong>result = <strong>for </strong>{<br>    profileFiber &lt;- <em>Users</em>[F].profileFor(userId).start<br>    ordersFiber &lt;- <em>Orders</em>[F].ordersFor(userId).start<br>    profile &lt;- profileFiber.join<br>    orders &lt;- ordersFiber.join<br>  } <strong>yield </strong>UserInformation.<em>from</em>(profile, orders)<br><br>  result.onError {<br>    <strong>case </strong>e =&gt; <em>Logging</em>[F].error(e)<br>  }<br>}</pre><p>This is where it gets tricky — we don’t get automatic type-class derivation for Concurrent for data types that are able to “write” to some shared environment (like WriterT or StateT). The reason for this is that operations can run concurrently, causing different states to be generated in an indeterministic order, unlike the sequential threading of state in the State monad. Because of that, we can’t necessarily know which output state is the “right” one to keep. Perhaps this could have been solved for <a href="https://en.wikipedia.org/wiki/Semilattice">Semilattice</a> states (that is — states that can be merged together in a way that is commutative, associative and idempotent). However, we can still find ways around this problem.</p><h4>Option #1 — custom instance for Concurrent</h4><p>We can give up on automatic derivation, and implement our own instance of Concurrent[Test], which is not actually concurrent. We can still ruse many of the auto-derived instances to delegate the actions from MonadError, such as flatMap, tailRecM or raiseError. But the fibers themselves will still run sequentially. This will allow us to keep all the code we wrote so far.</p><h4>Option #2 — a different approach</h4><p>Instead of using the State monad, we can go back to use a variation on the Reader monad. We redefine Test[A] as ReaderT[F, TestEnv, A], and as long we have a Concurrent instance for F we’ll get an auto-derived instance for Test like we need. The most obvious F we can choose is probably IO which ships with Cats-Effect and has all the type-class instances we need. This will require us to make some more modifications, since we can’t capture outputs like we did with StateT when using ReaderT — so instead of that, we’ll use a concurrent Ref from Cats-Effect. We’ll make this changes to our TestEnv:</p><pre><strong>case class </strong>TestEnv(<br>  profiles: Map[UserId, UserProfile],<br>  orders: Map[UserId, List[Order]],<br>  loggedErrors: Ref[IO, List[Throwable]]) {<br><br>  <strong>def </strong>withProfile(profile: UserProfile): TestEnv =<br>    copy(profiles = profiles + (profile.userId -&gt; profile))<br><br>  <strong>def </strong>withOrder(order: Order): TestEnv = {<br>    <strong>val </strong>updatedUserOrders = order :: userOrders(order.userId)<br>    copy(orders = orders + (order.userId -&gt; updatedUserOrders))<br>  }<br><br>  <strong>def </strong>logError(e: Throwable): IO[Unit] =<br>    loggedErrors.update(e :: _)<br><br>  <strong>def </strong>userOrders(userId: UserId): List[Order] =<br>    orders.getOrElse(userId, <em>Nil</em>)<br><br>}<br><br><strong>object </strong>TestEnv {<br>  <strong>final def </strong>empty: IO[TestEnv] =<br>    Ref.<em>of</em>[IO, List[Throwable]](<em>Nil</em>).map { loggedErrors =&gt;<br>      <em>TestEnv</em>(<em>Map</em>.<em>empty</em>, <em>Map</em>.<em>empty</em>, loggedErrors)<br>    }<br>}</pre><p>Reimplement our instances for the Users, Orders and Logging algebras:</p><pre><strong>type </strong>Test[A] = ReaderT[IO, TestEnv, A]<br><br><strong>implicit val </strong><em>usersTest</em>: Users[Test] = <strong>new </strong>Users[Test] {<br>  <strong>override def </strong>profileFor(userId: UserId): Test[UserProfile] =<br>    <em>ReaderT </em>{ env =&gt;<br>      env.profiles.get(userId) <strong>match </strong>{<br>        <strong>case </strong><em>Some</em>(profile) =&gt; IO.<em>pure</em>(profile)<br>        <strong>case </strong>None =&gt; IO.<em>raiseError</em>(<em>UserNotFound</em>(userId))<br>      }<br>    }<br>}<br><br><strong>implicit val </strong><em>ordersTest</em>: Orders[Test] = <strong>new </strong>Orders[Test] {<br>  <strong>override def </strong>ordersFor(userId: UserId): Test[List[Order]] =<br>    <em>ReaderT</em>(env =&gt; IO.<em>pure</em>(env.userOrders(userId)))<br>}<br><br><strong>implicit val </strong><em>loggingTest</em>: Logging[Test] = <strong>new </strong>Logging[Test] {<br>  <strong>override def </strong>error(e: Throwable): Test[Unit] =<br>    <em>ReaderT</em>(_.logError(e))<br>}</pre><p>And finally, apply the necessary changes in our tests:</p><pre><strong>class </strong>UserInformationSpec <strong>extends </strong>Specification {<br><br>  <strong>val </strong><em>userId </em>= <em>UserId</em>(&quot;user-1234&quot;)<br><br>  <strong>val </strong><em>userInformation </em>= <em>fetchUserInformation</em>[Test](<em>userId</em>)<br><br>  &quot;fetch user name and orders by ID&quot; in {<br>    <strong>val </strong>result = TestEnv.<em>empty</em>.flatMap { emptyEnv =&gt;<br>      <strong>val </strong>env = emptyEnv<br>        .withProfile(<em>UserProfile</em>(<em>userId</em>, &quot;John Doe&quot;))<br>        .withOrder(<em>Order</em>(<em>userId</em>, <em>OrderId</em>(&quot;order-1&quot;)))<br>        .withOrder(<em>Order</em>(<em>userId</em>, <em>OrderId</em>(&quot;order-2&quot;)))<br><br>      <em>userInformation</em>.run(env)<br>    }<br><br>    result.unsafeRunSync() must<br>      <em>haveUserName</em>(&quot;John Doe&quot;) and<br>      <em>haveOrders</em>(<em>OrderId</em>(&quot;order-1&quot;), <em>OrderId</em>(&quot;order-2&quot;))<br>  }<br><br>  &quot;log an error if user does not exists&quot; in {<br>    <strong>val </strong>result = <strong>for </strong>{<br>      env &lt;- TestEnv.<em>empty<br>      </em>// We need to use `attempt` here, so we won&#39;t fail-fast<br>      // and &quot;break out&quot; of the for comprehension<br>      _ &lt;- <em>userInformation</em>.run(env).attempt<br>      errors &lt;- env.loggedErrors.get<br>    } <strong>yield </strong>errors<br><br>    result.unsafeRunSync() must contain(UserNotFound(userId))<br>  }<br><br>}</pre><h3>Conclusion</h3><p>We’ve explored several methods of testing “real-world” applications that are written in the tagless-final style. We played with a contrived example, but these exact techniques are used in my day-to-day job, building real production systems. We evolved our definition of Test[A] given our needs and constraints:</p><ul><li>We started out with Reader[TestEnv, A]. This didn’t satisfy our needs of “writing” logged errors.</li><li>We moved to State[TestEnv, A] which let us capture state changes, like logged errors. However we could not raise errors in this context, which is an ability almost every real application needs.</li><li>We introduced error handling capabilities by switching to StateT[Either[Throwable, ?], TestEnv, A]. This looked very prominent, however, we could not inspect our environment in failure cases.</li><li>We re-ordered our effects stack with<br>EitherT[State[TestEnv, ?], Throwable, A] which satisfied all our needs except for testing concurrent code.</li><li>We explored 2 alternatives for handling with the need for the Concurrent type-class capabilities:<br>1) implement a pseudo-concurrent instance for testing purposes<br>2) use ReaderT[IO, TestEnv, A] which has the needed instances, but require our tests to run in the context of IO.</li></ul><p>I hope you found any of this useful / interesting, would love to get your comments on how you test your functional code.</p><ul><li>Update: the example code can be found on <a href="https://github.com/amitayh/functional-testing-tagless-final">https://github.com/amitayh/functional-testing-tagless-final</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50eeacf5df6" width="1" height="1" alt=""><hr><p><a href="https://medium.com/wix-engineering/functional-testing-with-tagless-final-50eeacf5df6">Functional Testing with Tagless-Final</a> was originally published in <a href="https://medium.com/wix-engineering">Wix Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Who needs Redux anyway?]]></title>
            <link>https://itnext.io/who-needs-redux-anyway-1d946e9ffa3b?source=rss-7b83bce94554------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d946e9ffa3b</guid>
            <category><![CDATA[elm]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Amitay Horwitz]]></dc:creator>
            <pubDate>Sun, 04 Mar 2018 21:30:32 GMT</pubDate>
            <atom:updated>2018-03-07T10:32:35.275Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-1xOJrwkohayBV41FOBOEw.jpeg" /><figcaption><strong>TL;DR</strong> — <a href="https://github.com/amitayh/no-redux">https://github.com/amitayh/no-redux</a></figcaption></figure><p><a href="https://www.linkedin.com/cws/share?url=https%3A%2F%2Fitnext.io%2Fwho-needs-redux-anyway-1d946e9ffa3b"><em>Click here to share this article on LinkedIn »</em></a></p><p>Redux is a very popular state-management library, mostly used along side React. People like it because it delivers a predictable data model, inspired by <a href="https://guide.elm-lang.org/architecture/">The Elm Architecture</a>™. However, there are many in the JS community who <a href="https://medium.com/@machnicki/why-redux-is-not-so-easy-some-alternatives-24816d5ad22d">don’t</a> <a href="https://games.greggman.com/game/react-and-redux-are-a-joke-right/">like</a> <a href="https://news.ycombinator.com/item?id=10940845">it</a> and would prefer to choose alternatives, such as <a href="https://github.com/mobxjs/mobx">MobX</a>.</p><p>To sum up the main pros/cons that usually come up:</p><h4>Redux’s strengths</h4><ul><li>Predictability and simplicity</li><li>Unidirectional data flow and immutability</li><li>Separation of data and presentation</li><li>Extensibility (via middlewares)</li><li>Popularity and community</li><li>Tooling support</li></ul><h4>Redux’s weaknesses</h4><ul><li>Boilerplate (views, action types, action creators, reducers, selectors, …)</li><li>No out-of-the-box solution for dealing with side-effects (available through middlewares such as <a href="https://github.com/gaearon/redux-thunk">redux-thunk</a> or <a href="https://github.com/redux-saga/redux-saga">redux-saga</a>)</li><li>Actions are disconnected from their effect (which is defined in the reducer)</li></ul><h3>A similar, yet different approach</h3><p>In a recent project I worked on I thought to myself “do I even need some third party library to manage my state”? React is a great library by itself, and I can control my state by having an immutable value with pure functions to transition between states. React will take care of the rest.</p><h4>Purity</h4><p>I wanted to keep most of the app pure, having used languages like Scala, Clojure and Haskell and seeing the benefits of functional programming and immutability.</p><p><strong>Pure view</strong> — this one’s easy. React gives us a programming model of</p><pre>view = f(state)</pre><p>So all we need to do is write that <em>f</em>. No need for connected components or internal component state. We can also benefit from React’s <a href="https://reactjs.org/docs/react-api.html#reactpurecomponent">pure components</a> which offer better performance over regular components.</p><p><strong>Pure state</strong> — again, no need for Redux here. We can simply use a single immutable value to represent our entire app’s state. Libraries like <a href="https://facebook.github.io/immutable-js/">Immutable.js</a> or <a href="http://ramdajs.com/">Ramda</a> can help us with managing immutable state.</p><p><strong>Pure updates</strong> — In both Redux and Elm, on of the key pieces of your application is a function to transition between states. In Elm, this is the <em>update</em> function, and in Redux it’s the <em>reducer</em>. They both share the same structure of</p><pre>(state, action) =&gt; new state</pre><p>Conventionally, they use some sort of pattern matching or switch statement over the action/message type, and return a new copy of the state with the needed updates. The thing that bothers me about this approach is that every update goes through the same function, which violates the <a href="https://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility principle</a> and might become hard to maintain, <a href="https://github.com/reactjs/redux/blob/master/examples/todomvc/src/reducers/todos.js">even for small applications</a>. I would rather use polymorphic dispatch, where the update function is a a part of the action.</p><h4>Actions</h4><p>Similarly to Redux, I wrote “action creators”. But unlike Redux, the actions are only containers to the update function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe9fd2eb4e66ec3d0b4d1d28b9df4c82/href">https://medium.com/media/fe9fd2eb4e66ec3d0b4d1d28b9df4c82/href</a></iframe><p>This is very self contained and decoupled. We just need a way to dispatch these actions. This is not too hard, we keep a single mutable variable at the root of our project to hold our state. Then we render the app, passing the dispatch function as a prop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b480b1d75162de832781ad29aa658fdd/href">https://medium.com/media/b480b1d75162de832781ad29aa658fdd/href</a></iframe><h4>Running (side) effects</h4><p>We want the ability to trigger side effects (for example, issuing AJAX requests) in a controlled and predictable way. The way this is achieved in Elm, is by allowing the <em>update</em> function to return a <a href="https://www.elm-tutorial.org/en/03-subs-cmds/02-commands.html">command</a> along side the updated model. The command itself doesn’t cause the side effect, but is merely a description of the effect to take place. We will use a similar approach, by allowing our actions to define a <em>runEffect</em> function. This will keep the actions themselves pure — since they don’t actually run the effects. This will be handled outside our program, in a single place at “the end of the world”</p><p>We will update our dispatch function to allow this behaviour:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/60ef588cb6ab4ebeee8eeca0145283e4/href">https://medium.com/media/60ef588cb6ab4ebeee8eeca0145283e4/href</a></iframe><p>Now, we can define actions that perform side-effects, keeping the rest of our application pure and safe:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/06f9ab8d7a975d29e0dcd0825f10eb34/href">https://medium.com/media/06f9ab8d7a975d29e0dcd0825f10eb34/href</a></iframe><p>This approach is very simplified, but it delivered the things I like about Redux and removed some of the complexity and boilerplate. It does have some issues I’d like to shortly address:</p><ul><li><strong>Tooling: </strong>since there’s no library here, and no community support — there are no awesome tools like <a href="https://github.com/gaearon/redux-devtools">Redux DevTools</a>. However, I found it quite easy to add a utility function for debugging that logs every action, with the old and new state.</li><li><strong>Serializability:</strong> Redux’s actions only hold the actions data and type. This allows you to easily save them, or transfer them over the wire. This is not possible with the action creators I described. However, the same benefits can easily be achieved by adding a <em>type</em> property on the action object, and having some sort of factory function that can recreate the action object from its payload.</li></ul><h3>Conclusion</h3><p>This has been an interesting project for me, and showed me that I can sometimes I bend familiar patterns a bit to achieve simpler codebases. This doesn’t mean Redux is a bad or useless library — but only that you might not always need everything it has to offer for your use case (this is true for every framework / library). So take the time to think about what you really need, because <a href="https://www.youtube.com/watch?v=rI8tNMsozo0">simplicity matters</a>.</p><p>Here’s the compulsory todo MVC implementation using this approach:</p><ul><li>Demo: <a href="https://amitayh.github.io/no-redux">https://amitayh.github.io/no-redux</a></li><li>Repo: <a href="https://github.com/amitayh/no-redux">https://github.com/amitayh/no-redux</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d946e9ffa3b" width="1" height="1" alt=""><hr><p><a href="https://itnext.io/who-needs-redux-anyway-1d946e9ffa3b">Who needs Redux anyway?</a> was originally published in <a href="https://itnext.io/">ITNEXT</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Implementing the Mano machine in Python]]></title>
            <link>https://medium.com/@amitayh/implementing-the-mano-machine-in-python-392df563118b?source=rss-7b83bce94554------2</link>
            <guid isPermaLink="false">https://medium.com/p/392df563118b</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[tech]]></category>
            <dc:creator><![CDATA[Amitay Horwitz]]></dc:creator>
            <pubDate>Sun, 12 Jun 2016 16:12:23 GMT</pubDate>
            <atom:updated>2016-06-12T16:12:23.499Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s_KhnqLaxptu7kt6sHt3aQ.jpeg" /></figure><p>When I was studying the “computer architecture” course, we used the <a href="https://en.wikipedia.org/wiki/Mano_machine">Mano machine</a> model describe a theoretical computer, which is simple enough to understand but powerful enough to be <a href="https://en.wikipedia.org/wiki/Turing_completeness">Turing complete</a> and run real programs (although the programs are written in a very minimalist assembly language, containing only 25 instructions)</p><p>I wanted to implement this model for two main reasons:</p><ol><li>To better understand the course material</li><li>Practice TDD with Python</li></ol><p>I will describe the main components of my implementation</p><h4>Register</h4><p>A simple class representing a register (constructed with the word size). The Mano machine contains a few registers, and having a unified class for them was helpful for the common register operations: shift left/right, increment, etc. The registers are used inside the <em>Computer</em> class, for example for address pointer (<em>ar</em>), program counter (<em>pc</em>), accumulator (<em>ac</em>), carry flag (<em>e</em>) and more.</p><h4>Memory</h4><p>This class represents the computer RAM. It is very simple (basically it’s an array), allowing only to read/write a word in an address</p><h4>Assembler</h4><p>This is not strictly needed for the computer to run, but it allows to write programs in an assembly language and load them into memory. You can see some examples of simple programs <a href="https://github.com/amitayh/mano-machine-emulator/blob/master/amitayh/test/test_computer.py#L53">in the tests</a></p><h4>Computer</h4><p>This is where it all comes together. While the computer runs (until the <em>HLT</em> instruction is seen), it will take an instruction from memory and execute it. Most of the work in this class is related to how to execute each of the supported instructions (that’s why it’s so long). Instructions can manipulate the memory or the registers, jump to different parts of the program (looping) and do I/O</p><p>This is basically it, you can check out the code here: <a href="https://github.com/amitayh/mano-machine-emulator">https://github.com/amitayh/mano-machine-emulator</a></p><p>I would also like to implement a more accurate simulation of registers / logic gates flow with something like <a href="https://en.wikipedia.org/wiki/Functional_reactive_programming">FRP</a>. Hope I’ll find the time :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=392df563118b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[TodoMVC on Steroids]]></title>
            <link>https://medium.com/@amitayh/todomvc-on-steroids-da5fc672f1bd?source=rss-7b83bce94554------2</link>
            <guid isPermaLink="false">https://medium.com/p/da5fc672f1bd</guid>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[immutablejs]]></category>
            <dc:creator><![CDATA[Amitay Horwitz]]></dc:creator>
            <pubDate>Fri, 10 Jun 2016 21:17:40 GMT</pubDate>
            <atom:updated>2016-06-10T21:19:43.367Z</atom:updated>
            <content:encoded><![CDATA[<h4>Experimenting with React/Redux/ImmutableJS</h4><p>Every frontend developer knows <a href="http://todomvc.com/">TodoMVC</a>. It showcases a common project (todo list), implemented in many different frameworks / languages / paradigms. The value preposition for this is to give a baseline for evaluating the different technologies.</p><p>Although this is good for getting first impression for different libraries, I always felt that a todo list is still too much of a “toy example”. In real projects I worked on, there was much more complexity and interaction between different components.</p><p>That’s why I usually use the TodoMVC example to “get the feel” of the technology, but I want to implement something more challenging myself. I usually choose to create a filters list selector. What is a filter list selector, you ask? It’s a UI component intended to select predicates on some list of fields. It’s borrowed from a real system I developed for BI reporting.</p><p>It may seem simple at first glance — similarly to the todo list example, it’s a dynamic list to which you add/remove items. However, these are the properties that I want to implement:</p><ul><li>In each filter you can choose the field by which you want to filter</li><li>The list of currently active fields is also dynamic, and may affect the filters list</li><li>Each field has some kind of type associated with it, which determines it’s available comparators and value widget (for example, for plain text you’ll have ‘contains’ / ‘does not contain’ comparators and a simple text field. For dates you’ll have ‘before’ / ‘after’ comparators and a date selector)</li></ul><p>This time, I chose to implement it using <a href="https://facebook.github.io/react/">React</a>, <a href="http://redux.js.org/">Redux</a> and <a href="https://facebook.github.io/immutable-js/">ImmutableJS</a>. I really enjoyed the functional nature of this stack — state management is so much simpler and predictable this way. Plus you get the cool <a href="https://github.com/gaearon/redux-devtools">DevTools</a>, hot reloading and more almost for free.</p><p>You can play around with the demo here: <a href="http://amitayh.github.io/react-redux-test/">http://amitayh.github.io/react-redux-test/</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/557/1*Bs6R8MXf7Si5111J4dd5ww.png" /><figcaption>Not a very pretty UI :)</figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=da5fc672f1bd" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>