<?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 Ihor Vovk on Medium]]></title>
        <description><![CDATA[Stories by Ihor Vovk on Medium]]></description>
        <link>https://medium.com/@ivovk?source=rss-15def56f5f8a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xz63yeiTa3L3MhRNyZymjA.jpeg</url>
            <title>Stories by Ihor Vovk on Medium</title>
            <link>https://medium.com/@ivovk?source=rss-15def56f5f8a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 07:04:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ivovk/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[Remapping “§±” key to “`~” (tilde) key on Mac computers]]></title>
            <link>https://medium.com/@ivovk/how-to-remap-key-on-iso-keyboards-to-key-on-mac-d6ea479b4f0b?source=rss-15def56f5f8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/d6ea479b4f0b</guid>
            <category><![CDATA[keyboard]]></category>
            <category><![CDATA[macos]]></category>
            <dc:creator><![CDATA[Ihor Vovk]]></dc:creator>
            <pubDate>Sat, 13 Sep 2025 08:20:09 GMT</pubDate>
            <atom:updated>2025-09-13T08:24:20.561Z</atom:updated>
            <content:encoded><![CDATA[<p>ISO layout has one annoyance to me, which is the <em>§ </em>key, located to the left from numeric <em>1</em> key:</p><figure><img alt="Image from Reddit" src="https://cdn-images-1.medium.com/max/1024/1*zmLqPZ3Q4cRFoIRLLOVCcA.jpeg" /><figcaption>Image from Reddit</figcaption></figure><p>Anyone, who had worked with ANSI keyboards, had used to see the ~ key instead. ~ key is used in a lot of macOS shortcuts, such as CMD+~ to switch between the windows of the same program, ` symbol used to quote code in markdown and various messengers such as Slack, so that having it near left Shift on ISO keyboards is very impractical.</p><p>Googling had shown that there is a software, called Karabiner Elements, capable of remapping the key, and this software was the first solution of the author. But Karabiner Elements is a third-party software, that needs to have a permissions to grab all user keystrokes, consumes RAM and place in the system tray, which kind of not very good. Turns out that this problem can be solved with a built-in utility called hidutil, which accepts the configuration for remapping the keys from source to destination. Format of the call is:</p><pre>hidutil property \<br>  --set&#39;{&quot;UserKeyMapping&quot;:[{&quot;HIDKeyboardModifierMappingSrc&quot;: src,&quot;HIDKeyboardModifierMappingDst&quot;: dst}]}&#39;</pre><p>Where src and dst are the key codes, that can obtained on the old <a href="https://developer.apple.com/library/archive/technotes/tn2450/_index.html">Apple Developer page</a>, but it did not list all the codes. With some experimentation I found that the code of the §± key is 0x700000064, so that remapping script looks like:</p><pre>hidutil property \<br>  --set &#39;{&quot;UserKeyMapping&quot;:[{&quot;HIDKeyboardModifierMappingSrc&quot;: 0x700000064,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000035}]}&#39;</pre><p><em>Bonus: I also like to remap the Caps Lock key to the Backspace (Delete). You would be surprised but this has a massive productivity boost, since the hand doesn’t need to move in the top right corner each time something needs to be deleted, now it’s just a small movement of a pinky finger.</em></p><p>So the full script looks like:</p><pre># https://developer.apple.com/library/archive/technotes/tn2450/_index.html<br># 0x700000039 – Caps Lock<br># 0x70000002A – Backspace <br># 0x700000064 – Key to the left of 1 on ISO keyboards (§±)<br># 0x700000035 – Tilde (`~)<br><br>hidutil property \<br>  --set &#39;{&quot;UserKeyMapping&quot;:[{&quot;HIDKeyboardModifierMappingSrc&quot;:0x700000039,&quot;HIDKeyboardModifierMappingDst&quot;:0x70000002A},{&quot;HIDKeyboardModifierMappingSrc&quot;: 0x700000064,&quot;HIDKeyboardModifierMappingDst&quot;:0x700000035}]}&#39;</pre><h4>Making the script automatically loadable upon the system boot</h4><p>You can execute this script in the Terminal and it will change the behavior of the keys. There are multiple ways to load something during the boot, I’ve chosen the Automator utility, and saved the script in the iCloud, since I need it on multiple computers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qUk9VegLNv6ek59zWy7a5Q.png" /></figure><ul><li>Run an Automator utility, choose the folder to store the script.</li><li>Add “Run Shell Script” action and paste the script into it</li><li>You can test it by running it already</li><li>To make it bootable, open “Login Items &amp; Extensions” system settings page and drag the file into it:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*y38CvOkwEiKFAJPFscKAWA.png" /><figcaption>“Remap Keys” is an Automator script</figcaption></figure><p>That’s it! Now upon reboot remapping configuration will be applied automatically.</p><p>I hope you found this information helpful.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d6ea479b4f0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Spatial joins with DuckDB]]></title>
            <link>https://medium.com/@ivovk/spatial-joins-with-duckdb-4f7c958ad9e6?source=rss-15def56f5f8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4f7c958ad9e6</guid>
            <category><![CDATA[geospatial]]></category>
            <category><![CDATA[sql]]></category>
            <category><![CDATA[duckdb]]></category>
            <dc:creator><![CDATA[Ihor Vovk]]></dc:creator>
            <pubDate>Fri, 15 Aug 2025 20:57:44 GMT</pubDate>
            <atom:updated>2025-08-15T21:04:10.305Z</atom:updated>
            <content:encoded><![CDATA[<p>One of the interesting tasks I’ve had recently, was to assign an <a href="https://en.wikipedia.org/wiki/IATA_airport_code">IATA</a> location identifiers of the closest airports to the cities on the <a href="https://dosh.at/">dosh.at</a> website.</p><p>I’ve already had a table with cities, which was based on the database taken from <a href="https://simplemaps.com/data/world-cities">https://simplemaps.com/data/world-cities</a>. For the sake of this post we will download a database from their website:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jVFf4OSn5Hbye9z8GjZl9A.png" /></figure><p>Same story with airports, we can download the database from <a href="https://ourairports.com/data/">https://ourairports.com/data/</a> website (<a href="https://davidmegginson.github.io/ourairports-data/airports.csv">airports.csv</a>).</p><p>Now the first nice thing about the DuckDB, is that it supports loading CSV files, so we can easily create tables from those files by running:</p><pre>CREATE TABLE cities AS <br>  FROM read_csv(&#39;~/Downloads/worldcities.csv&#39;);<br>CREATE TABLE airports AS <br>  FROM read_csv(&#39;~/Downloads/airports.csv&#39;);</pre><p>Running those queries resulted in two tables, airports and cities:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bHfLLpOx_Yc-l_D9PP9syw.png" /></figure><p>Both tables contain geographical coordinates that we can use to perform geospatial joins. We will need to compute the distance between each city and airport to find the closest combinations. To compute a distance between 2 points, we can either compute an Euclidean distance manually, or even better option would be to use the spatial extension, provided by <a href="https://duckdb.org/docs/stable/core_extensions/spatial/overview">DuckDB</a>:</p><pre>INSTALL spatial;<br>LOAD spatial;</pre><p>Spatial extension provides ST_Distance function, allowing computing the distance between 2 points. We can then multiply it by (roughly) 111 to convert degrees to kilometers.</p><p>Here is the SQL-query:</p><pre>SELECT<br>  c.city,<br>  c.country,<br>  c.lat as city_lat,<br>  c.lng as city_lng,<br>  a.name as airport_name,<br>  a.latitude_deg as airport_lat,<br>  a.longitude_deg as airport_lng,<br>  a.iata_code,<br>  ST_Distance (ST_Point (city_lng, city_lat), ST_Point (airport_lng, airport_lat)) * 111 as distance_km<br>FROM<br>  cities c<br>  CROSS JOIN airports a<br>WHERE<br>  c.capital = &#39;primary&#39;<br>  AND a.iata_code IS NOT NULL<br>QUALIFY<br>  ROW_NUMBER() OVER (<br>    PARTITION BY<br>      city<br>    ORDER BY<br>      ST_Distance (ST_Point (city_lng, city_lat), ST_Point (airport_lng, airport_lat))<br>  ) = 1<br>ORDER BY city;</pre><p>What happens under the hood:</p><ul><li>ST_Distance is used to compute the distances between 2 points</li><li>We use the technique called CROSS JOIN — it joins each row of the source table with the each row in the destination table, producing m*n combinations.</li><li>Then <a href="https://duckdb.org/docs/stable/sql/query_syntax/qualify">QUALIFY</a> is used to reduce those combinations. We use city as a partitioning key, and distance between cities and airports as ORDER BY clause, taking the first row from each partition.</li></ul><p>Here is the results of the query:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rl1WfJ7Db3y_yBUK-lKbPA.png" /></figure><p>Do you like how simple this complex task turned out to be?</p><p>That is for me a sign of a good technology, to make seemingly hard tasks simple. Which DuckDB definitely is. Thank you for reading, please write in the comments what improvements you’d also make in the flow.</p><p>P.S.: Installing and running DuckDB is as easy as executing:</p><pre>&gt; brew install duckdb<br>&gt; duckdb -ui</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4f7c958ad9e6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A case for a side-project: weather comparison website — dosh.at]]></title>
            <link>https://medium.com/@ivovk/a-case-for-a-side-project-weather-comparison-website-dosh-at-3fe499c90325?source=rss-15def56f5f8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/3fe499c90325</guid>
            <category><![CDATA[pet-project]]></category>
            <category><![CDATA[announcements]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[weather]]></category>
            <dc:creator><![CDATA[Ihor Vovk]]></dc:creator>
            <pubDate>Sun, 13 Oct 2024 13:42:08 GMT</pubDate>
            <atom:updated>2024-10-13T13:49:28.568Z</atom:updated>
            <content:encoded><![CDATA[<h3>Announcing a project: weather comparison website — dosh.at</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nv2FmYxHAeyHLne68X887g.png" /></figure><p>I want to share a story, how I came up to creating the <a href="https://dosh.at/">dosh.at</a> — website to track and compare weather across multiple places.</p><p>It all started from the realization that I, while being on different roles in software engineering for the last 10 years, doesn’t have anything that was “mine” and that I’m basically still exchanging my time for the money. Moreover, I realized that the stuff I enjoy — making stuff, releasing it to the users, observing the effect – is usually not very connected with the typical software engineering process on the backend track.</p><p>The idea of such website came after I moved to Netherlands, and wanted to compare the weather in Amsterdam with other big tech hubs, like Berlin or London. I found that existing websites have many limitations, like inability to compare more than 2 cities, or the interface that came out from 95s, or the interface that looked like it was made for scientists, or technically-unadvanced users. So I thought, I can try and enter this field with the website that will be fast, responsive and simple, will be built with modern technologies and thus receive high ranks in search engines.</p><p>When I bonded with the idea, I started thinking about few topics: for whom I will be creating it, what approach to the website organization should I take, what strategy should I choose to eventually make it profitable.</p><h3>Auditory</h3><p>This was the easiest one. The auditory of this website as I imagined:</p><ul><li>people who want to travel, and so they prepare and want to choose the place with a good weather in a particular period of the year, comparing typical touristic destinations</li><li>people who are preparing to move for a long stand in a different city / country, so they want to compare yearly cycles of the weather in different cities</li><li>people who live in few different geozones: need to track and/or travel multiple times a year by their work needs</li><li>comparison of the weather is a side case of just outputting the weather in single location, so I decided not to discard it and support cases of outputting weather for a single location and thus to include much wider auditory of people who has interest in general weather</li></ul><h3>Search Engine-optimized website or dynamic web-app</h3><p>Next question I wanted to clear for myself, is what kind of website I will be building:</p><ul><li>Initial idea was to create a Gmail single-page-style web-app, that will support user authentication, and will allow users to create lists and include weather. I disliked this idea in favor of:</li><li>Search engine-optimized traditional website-like design, that will be optimized for crawlers and gathering traffic.</li></ul><p>Favor of second one was made after I realized, that until I will have someone who <em>needs </em>specific functionality in weather comparison, it’s much easier to find users using search engines as a source of traffic, having more than 150,000 of indexable webpages with combinations of different cities.</p><h3>Profitability</h3><p>This is still needs to be cleared out, and so far I see 3 ways of getting profit:</p><ul><li>Google Ads. Not something that I like: first of all, ads tend to crowd the website and I think they will decrease conversion of new users into permanent audience. And almost everyone who I know already disables them in some way. Also ads are only good when you don’t know what to sell to the users, giving this to Google, and in my case, I think I know what to sell to the users</li><li>Disable some features by default and give them to subscribed users. Again, this could work (even in conjunction with the ads, where payment would be one of the options to disable them), but I don’t yet know how useful the website is, and also it will require development of some kind of authentication mechanism, so I’m deciding to stick with:</li><li>Affiliate programs with websites in the area of tourism, selling tickets and accommodation: this choice seems to be the most natural for me. I will be able to control how to show this content in a way which doesn’t harm user experience, and I tend to think it will have the most value for the imagined audience of the website.</li></ul><p>So at the moment I decided to stick with affiliate programs (this is yet to be developed at the moment of writing the article and yet to confirm the theory).</p><h3>Technologies used</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F1IgxMWL-HCj20hwFd3teQ.png" /><figcaption>Architectural diagram of the Dosh</figcaption></figure><p>There are 2 micro-services written in Scala:</p><ul><li><strong>Weather aggregator</strong>, which owns everything weather data-related, including forecasts and historical data by day, performing data aggregations.</li><li><strong>location resolver</strong>, holding the data about the locations.</li></ul><p>, and a frontend-microservice written in Typescript with Next.js.</p><p>The most interesting thing for me was choosing the Next.js framework, which allows to write the code both for the frontend and backend using React framework and hides most of the complexity of frontend-server communication. While I was away from the frontend for a good few years, it’s amazing how easy is to build websites with it that are both dynamic and indexable by search engines at the same time.</p><p>The transport layer framework that was chosen is GRPC, which allowed not to spend any time on writing REST-clients, be multi-language and provide great performance. Protobuf schemas were also used for storing data in Redis, as they are faster / take less space compared to JSON.</p><p>Database of choice is a PostgreSQL (<a href="https://www.timescale.com/">Timescale</a> to be concrete), which has nice support for storing time-series data, including ability to archive old data (not really needed now, but useful for the future, also I just had it installed for other projects so it was natural for me to use it).</p><p>Cloud provider that I’m using and can recommend is <a href="https://hetzner.cloud/?ref=iAnthJAtoQ8d">Hetzner</a> with Cloudflare on top.</p><h3>Next steps</h3><p>Next features I’m planning to add:</p><ul><li>(obvious one) adding more data on a site, like air quality comparisons, etc</li><li>Adding user authentication and giving ability for users to store their lists with locations / changing and persisting settings of the output.</li><li>Adding a blog and publishing some articles, like interesting places to travel, etc</li><li>Textual descriptions of the weather differences, generated by ChatGPT. From one hand, they should increase the SEO of the website. Also I have a theory that charts are sometimes hard to read for some users, so it seems natural to add textual summaries.</li></ul><h3>Interesting links</h3><ul><li>Visually interesting to see how opposite is the weather in cities of Northern and Southern hemisphere: <a href="https://dosh.at/weather/Rome%2CIT%3BSydney%2CAU">Rome and Sydney</a> in this case</li><li>Last summer in Kyiv (city I’m from) was way hotter than in <a href="https://dosh.at/weather/Kyiv,UA;Barcelona,ES">Barcelona</a></li><li>While many people complain about the weather in Berlin, it actually has nicer weather compared with other tech hubs, like <a href="https://dosh.at/weather/Berlin,DE;London,GB;Amsterdam,NL">London or Amsterdam</a> (more sun and higher temperatures except last 2 months of the year)</li></ul><p>Thank you! I hope you will enjoy the website <a href="https://dosh.at/">dosh.at</a>!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fe499c90325" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IntelliJ IDEA is slow when working with Scala 3? Here is an option that fixes it]]></title>
            <link>https://medium.com/@ivovk/intellij-idea-is-slow-when-working-with-scala-3-here-is-the-option-that-fixes-it-e01640180eae?source=rss-15def56f5f8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/e01640180eae</guid>
            <category><![CDATA[intellij-idea]]></category>
            <category><![CDATA[scala]]></category>
            <dc:creator><![CDATA[Ihor Vovk]]></dc:creator>
            <pubDate>Sat, 08 Jul 2023 10:07:21 GMT</pubDate>
            <atom:updated>2023-07-27T06:46:26.436Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Update: </em><a href="https://blog.jetbrains.com/scala/2023/07/26/intellij-scala-plugin-2023-2-is-out/"><em>latest version of Scala plugin</em></a><em>, released on Jul 26, 2023 fixes this problem by setting Zinc compiler as default.</em></p><p>After the transition from Scala 2.13 to Scala 3 there were a few things that did bother me. One of them was significantly degraded performance of the IntelliJ IDEA. It felt like my MacBook Pro with M1 Pro processor was again an Intel-based laptop, everything was sluggish, IntelliJ was marking some valid code as if it was containing errors, and I was often forced to recompile project, after which errors would normally disappear. Recompilation by itself was taking more time than I used to using Scala 2.13. It really felt like the tooling is not on par with what I used to. And it turned out there was a single option, pretty much a hidden gem in the settings, that changed that experience to much better: backend for incremental compilation of Scala code.</p><p>IntelliJ IDEA uses incremental compiler to highlight the code and check it‘s validity. And by default, IntelliJ uses the old internal Incremental Compiler, which is not optimized for Scala 3. Another option that is available is <a href="https://github.com/sbt/zinc">Zinc compiler</a>, which is waaay faster and is used behind SBT and other tools. To set Zinc as default, you need to go into <em>Settings -&gt; Build, Execution, Deployment -&gt; Compiler -&gt; Scala Compiler </em>and set <em>Incrementality type </em>option to “<em>Zinc</em>”:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e7QlGKGnQki55fBgYNNKAw.png" /></figure><p>After doing that, full rebuild of the project is needed. This change did a massive upgrade of performance for me: full compilation of relatively small project (~100 Scala files) was taking 28 seconds with IDEA compiler, and is now taking only 16 seconds with Zinc. Incremental compilation now takes around 1,5 seconds, while it was taking ~2.5–5 seconds before. Apart from that, there is less “false errors” — highlights, that do disappear after recompilation, so overall the coding experience is much better now.</p><p>You can ask why it’s not a default option? Actually, there is a <a href="https://youtrack.jetbrains.com/issue/SCL-21064">ticket</a> from 9th of March in IntelliJ IDEA issue tracker to set it as default, so hopefully <em>Zinc</em> will become a default in near future, but you can speed up things by already doing it now!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e01640180eae" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[FS2 Streams: detecting stream idleness]]></title>
            <link>https://medium.com/@ivovk/fs2-streams-detecting-stream-idleness-aec12af06936?source=rss-15def56f5f8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/aec12af06936</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[fs2]]></category>
            <category><![CDATA[reactive-streams]]></category>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[stream-processing]]></category>
            <dc:creator><![CDATA[Ihor Vovk]]></dc:creator>
            <pubDate>Tue, 27 Jun 2023 13:51:39 GMT</pubDate>
            <atom:updated>2023-06-27T13:59:49.162Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This article provides a solution for “looked-5-minute-in-the-beginning” problem, for which I couldn’t find anything in the Internet. I hope it will be useful for someone.</em></p><p>Problem: one day I needed to shut down an FS2 stream in case when there was no new elements for certain amount of time. I had a stream with financial data coming through the websocket connection, and underlying library wasn’t properly closing it on any network problems, so from time-to-time my stream was becoming unoperational, while staying open.</p><p>To solve this problem I had to introduce “idleness” detection: because I knew that the data is coming in predefined intervals, having no data over a certain amount of time was clearly a sign of some problems, so the stream needed to be closed and then connection attempt performed.</p><p>While searching for solutions, I stumbled on fs2.Stream.timeout(timeout: FiniteDuration) combinator first, but turned out it was not doing what I expected, it just fails a stream if it wasn’t completed until timeout. I’d say that the documentation for FS2 library doesn’t cover all the functionality it has, so sometimes you need to spend time with this library, which in turn I did, and turned out that FS2 has solution for this problem, just it’s somehow “hidden”, because to implement it one needs to go a level down and work with the Pull API.</p><p>Basically Stream and Pull APIs are compatible between each other, one can call .pull method on a Stream to start working with it, and then go back by invoking a .stream method. Pull API is very powerful and I’d say is not covered well in the documentation, there is this <a href="https://fs2.io/#/guide?id=transforming-streams-using-pulls">section</a> in the docs which shortly describes it, and in <a href="https://fs2.io/#/faq?id=what-is-the-difference-between-stream-and-pull">FAQ</a> I found the following picture which describes the difference between 2 APIs:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AETUP191kGrfF-SFcBTTIA.png" /></figure><p>So Pull API actually has something to solve this problem: Pull.Timed class, which allows setting up a timer which then fires when no elements are pulled for certain amount of time, exactly the case I needed. The full code looks like this:</p><pre>import cats.effect.Async<br>import fs2.{Pipe, Pull}<br><br>import scala.concurrent.duration.FiniteDuration<br><br>object StreamUtils {<br><br>  case class TimeoutException(duration: FiniteDuration) extends RuntimeException(s&quot;No new messages received for $duration&quot;)<br><br>  def timeoutOnIdle[F[_] : Async, A](duration: FiniteDuration): Pipe[F, A, A] = { stream =&gt;<br>    stream<br>      .pull<br>      .timed { timedPull =&gt;<br>        def go(timedPull: Pull.Timed[F, A]): Pull[F, A, Unit] =<br>          timedPull.timeout(duration) &gt;&gt;<br>            timedPull.uncons.flatMap {<br>              case Some((Right(elems), next)) =&gt; Pull.output(elems) &gt;&gt; go(next)<br>              case Some((Left(_), _)) =&gt; Pull.raiseError(TimeoutException(duration))<br>              case None =&gt; Pull.done<br>            }<br><br>        go(timedPull)<br>      }<br>      .stream<br>  }<br><br>}</pre><p>, it can be used by utilizing .through(StreamUtils.timeoutOnIdle(5.seconds)) method on a stream. I hope this story will solve some precious time for someone.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aec12af06936" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Dependency Injection with Cats-effect Resource Monad]]></title>
            <link>https://medium.com/@ivovk/dependency-injection-with-cats-effect-resource-monad-ad7cd47b977?source=rss-15def56f5f8a------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad7cd47b977</guid>
            <category><![CDATA[scala]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[cats-effect]]></category>
            <dc:creator><![CDATA[Ihor Vovk]]></dc:creator>
            <pubDate>Fri, 16 Jun 2023 10:17:06 GMT</pubDate>
            <atom:updated>2025-06-04T15:07:59.787Z</atom:updated>
            <content:encoded><![CDATA[<p><em>Upd: published the library on Github </em><a href="https://github.com/igor-vovk/cedi?tab=readme-ov-file"><em>https://github.com/igor-vovk/cedi</em></a></p><p>Cats-effect <a href="https://typelevel.org/cats-effect/docs/std/resource">Resource</a> Monad provides a great monadic abstraction over <a href="https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html">try-with-resource</a> pattern, e.g. it allows to manage dependency lifecycle including resource closing/finalization when it is not needed anymore (closing connection to a database, freeing cache on shutdown). Together with composability of monads it became a very popular approach for dependencies management, to the extent that Scala libraries like <a href="https://http4s.org/v1/docs/service.html">http4s</a> provide their dependencies wrapped in Resource Monad.</p><p><em>Off-topic: one of the big advantages of Resource Monad is that it allows to free resources in the opposite way to how they were instantiated, feature which even monsters like Google Guice doesn’t have</em></p><p>Now every Scala application developer, when creating their application, performs dependency management in some way. There are libraries that help with dependency management, like <a href="https://github.com/adamw/macwire">macwire</a>, but usually using pure Resource monad is enough to perform dependency injection, like in the following simple example of http service:</p><pre>import cats.effect.*<br> ​<br> // repository, two services, and api:<br> class Repository(conn: ConnectionFactory) {}<br> ​<br> class ServiceA(repo: Repository) {}<br> class ServiceB(repo: Repository) {}<br> ​<br> class HttpServerTask(serviceA: ServiceA, serviceB: ServiceB) {<br>   def run: IO[Unit] = ???<br> }<br> ​<br> // Dependency Injection part:<br> object Dependencies {<br>   private val conn: Resource[IO, ConnectionFactory] = ???<br>   <br>   private val repo: Resource[IO, Repository] = for {<br>     conn &lt;- this.conn<br>  } yield new Repository(conn)<br>    <br>   private val serviceA: Resource[IO, ServiceA] = for {<br>     repo &lt;- this.repo<br>  } yield new ServiceA(repo)<br>   <br>   private val serviceB: Resource[IO, ServiceB] = for {<br>     repo &lt;- this.repo<br>  } yield new ServiceB(repo)<br>   <br>   val server: Resource[IO, HttpServerTask] = for {<br>     serviceA &lt;- this.serviceA<br>     serviceB &lt;- this.serviceB<br>  } yield new HttpServerTask(serviceA, serviceB)<br> }<br> ​<br> // Application entry point<br> object Main extends IOApp.Simple {<br>   def run = Dependencies.server.use(_.run())<br> }</pre><p>Now let’s name things. We can say that our Depencies object is a <em>Dependency Injection container</em>, and server is an <em>exit dependency</em> – e.g. dependency that will be used outside of the container. All other dependencies are then an <em>internal dependencies</em>.</p><p>Now, this code has a major problem: because of <a href="https://typelevel.org/cats-effect/docs/2.x/datatypes/io#on-referential-transparency-and-lazy-evaluation">lazy nature</a> of cats-effect, while it is temping to think that conn is a val, in reality it will be executed as many times as it is referenced, twice in this example – which we don&#39;t want to, cause then we have 2 connection pools to the same database in a single application. The usual pattern of Dependency Injection is to have only one instance of every dependency, if not specified otherwise for specific cases.</p><p>So usually this problem is solved by thinking in a functional way, and first approach that comes into head is to convert all the wiring to a function with big for-comprehension which returns <em>exit dependency</em>, like here:</p><pre>...<br> ​<br> // Here we specify our exit dependencies, wrapping in a case class to add new exit dependendies in the future<br> case class Dependencies(server: HttpServerTask)<br> ​<br> object Dependencies {<br>   val conn: Resource[IO, ConnectionFactory] = ???<br> ​<br>   def apply: Resource[IO, Dependencies] = for {<br>     conn &lt;- this.conn<br>     repo &lt;- new Repository(conn)<br>     serviceA &lt;- new ServiceA(repo)<br>     serviceB &lt;- new ServiceB(repo)<br>     server &lt;- new HttpServerTask(serviceA, serviceB)<br>  } yield Dependencies(server)<br>   <br> }</pre><p>This approach is widely used for small applications (<a href="https://github.com/pauljamescleary/scala-pet-store/blob/master/src/main/scala/io/github/pauljamescleary/petstore/Server.scala">example</a>) because of it’s simplicity. It also have some downsides:</p><ul><li>having one big for-comprehension is not extensible and starts to look badly when it grows with new dependencies included;</li><li>adding more exit dependencies requires to instantiate all of them, including their graph of dependencies. Suppose we have 2 APIs and 2 entry points (that will be deployed as 2 separate services), using this approach we either need to write 2 separate for-comprehensions or instantiate all of them together and use only 1, which is not optimal to say at least;</li><li>suppose we want to have a freedom of <em>each</em> dependency to be an <em>exit dependency</em>, e.g. to be able to use each dependency outside of a DI container, something that java libraries like Google Guice easily allow one to do, than this approach doesn’t work <em>at all</em></li></ul><p>So another approach is to go one step back to having separate dependencies, and just try to initialize them but keep track of initialized dependencies shutdown functions, to be able to gracefully shutdown them on the stop of the application.</p><p>To accomplish this, we need some helper class called Allocator, that will be responsible for dependency initialization, keeping track over initialized dependencies, and finalizing them:</p><pre>import cats.effect.*<br>import cats.effect.unsafe.IORuntime<br>​<br>class Allocator(implicit runtime: IORuntime) {<br>  // Ref that will keep track of finalizers<br>  private val shutdown: Ref[IO, IO[Unit]] = Ref.unsafe(IO.unit)<br><br>  // Method to allocate dependencies<br>  def allocate[A](resource: Resource[IO, A]): A =<br>    resource.allocated.flatMap { case (a, release) =&gt;<br>      // Shutdown this resource, and after shutdown all previous<br>      shutdown.update(release *&gt; _).map(_ =&gt; a)<br>  }.unsafeRunSync()<br>​<br>  // Shutdown dependencies<br>  def shutdownAll: IO[Unit] = {<br>    shutdown.getAndSet(IO.unit).flatten<br>  }<br>​<br>}</pre><p>What this class does is it allows us to perform some non-idiomatic unwrapping of the Resources and keep track of finalizers to shutdown them in the right order. Non-idiomacity will be fixed later, now check how the code looks like after using this simple class:</p><pre>class Dependencies(val allocator: Allocator) {<br>  lazy val conn: ConnectionFactory = allocator.allocate {<br>     ???<br>  }<br>   <br>  lazy val repo = new Repository(conn)<br>    <br>  lazy val serviceA = new ServiceA(repo)<br>   <br>  lazy val serviceB = new ServiceB(repo)<br>   <br>  lazy val server = HttpServerTask(serviceA, serviceB)<br>}</pre><p>As you can see, only one dependency needed Allocator as it had something to finalize (ConnectionFactory). The code is much simplified and allows us to achieve:</p><ul><li>The dependency instation is still lazy, but now lazyness is achieved by lazy val Scala mechanism instead of Resource laziness.</li><li>Now all dependencies can be exposed as exit dependencies. Each dependency can be freely used outside.</li><li>Again the code is simple and extensible. We can group similar dependencies in separate Dependencies classes, only reuse single Instantantiator between them.</li></ul><p>Now let’s track the problem that we had broken idiomacy of Resource pattern inside of the DI container. If you take a look on the Allocator class, it is by itself has shutdownAll method, which allows us now to wrap our entire DI container in a single Resource monad:</p><pre>object Dependencies {<br>  // Safe method to create dependencies:<br>  def apply(runtime: IORuntime): Resource[IO, Dependencies] =<br>    Resource.make {<br>      IO(unsafeCreate(runtime))<br>   } {<br>      _.allocator.shutdownAll<br>   }<br> ​<br>  // Unsafe method, use it carefully as no shutdown is executed:<br>  def unsafeCreate(runtime: IORuntime): Dependencies =<br>    new Dependencies(new Allocator()(runtime))<br>}<br>​<br>class Dependencies(val allocator: Allocator) {<br>  ...<br>}<br>​<br>// Runner class<br>object Main extends IOApp.Simple {<br>  // Now we can use any dependency and not just `server` as all of them are exposed:<br>  private val dependencies = Dependencies(IORuntime.global)<br>  <br>  override def run = dependencies.use(_.server.run)<br>}</pre><p>That’s it! This method is my chosed method of doing Dependency Injection in Scala with Cats-Effect right now. While breaking Resource idiomatic boundary inside of the container, it allows to achieve reusing of the dependencies in a simple and extensible way, and we still go back to the Resource pattern for external users who will call DI container&#39;s dependencies.</p><p>Check how this pattern also allows breaking dependencies into multiple containers easily, like so:</p><pre>class AwsDependencies(val allocator: Allocator, config: Config) {<br>  val s3: AwsS3Client[IO] = allocator.allocate {<br>    Resource.fromAutoCloseable(IO.blocking {<br>      S3AsyncClient.builder()<br>        .region(config.region)<br>        .build()<br>    }).map(new AwsS3Client(_))<br>  }<br>}<br> ​<br>class MainDependencies(val allocator: Allocator) {<br>  <br>  lazy val config = ???<br>  <br>  lazy val aws: AwsDependencies = new AwsDependencies(allocator, config.as[Config](&quot;aws&quot;))<br>  <br>  lazy val httpRoutes: Routes = new Routes(aws.s3)<br>  <br>}</pre><p>Here is a full example of the final code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1392b6623d0fc0138178ae285d5b1e9e/href">https://medium.com/media/1392b6623d0fc0138178ae285d5b1e9e/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad7cd47b977" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>