<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Senjin Hajrulahovic</title>
    <description>The latest articles on DEV Community by Senjin Hajrulahovic (@senjinhajrulahovic).</description>
    <link>https://dev.to/senjinhajrulahovic</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F573622%2F66ea6b11-19ab-4311-b290-406b2812badb.jpg</url>
      <title>DEV Community: Senjin Hajrulahovic</title>
      <link>https://dev.to/senjinhajrulahovic</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/senjinhajrulahovic"/>
    <language>en</language>
    <item>
      <title>Typeclasses explained in Java</title>
      <dc:creator>Senjin Hajrulahovic</dc:creator>
      <pubDate>Wed, 27 Jul 2022 12:28:43 +0000</pubDate>
      <link>https://dev.to/senjinhajrulahovic/typeclasses-explained-in-java-jb9</link>
      <guid>https://dev.to/senjinhajrulahovic/typeclasses-explained-in-java-jb9</guid>
      <description>&lt;p&gt;By &lt;a href="https://en.wikipedia.org/wiki/Type_class" rel="noopener noreferrer"&gt;definition&lt;/a&gt; type classes are a type system construct that supports ad hoc polymorphism.&lt;/p&gt;

&lt;p&gt;Let us elaborate this definition by explaining following aspects of type classes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Externalized not implemented inside the class in question&lt;/li&gt;
&lt;li&gt;Classes which we do not control&lt;/li&gt;
&lt;li&gt;Possibility to have multiple implementations&lt;/li&gt;
&lt;li&gt;Lawfullness&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An excellent example for showcasing the aforementioned aspects and the differences between the type class approach opposed to the usual inheritance approach are the java interfaces &lt;code&gt;Comparable&lt;/code&gt; and &lt;code&gt;Comparator&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  External vs internal implementation
&lt;/h2&gt;

&lt;p&gt;In order to use inheritance approach the class which instances we want compare has to extend &lt;code&gt;Comparable&lt;/code&gt; and add an implementation for the &lt;code&gt;compareTo&lt;/code&gt; method. Opposite to that if we take the type class approach we will implement a &lt;code&gt;Comparator&lt;/code&gt; instance.&lt;/p&gt;

&lt;h2&gt;
  
  
  Classes which we do not control
&lt;/h2&gt;

&lt;p&gt;It's not possible to use the inheritance approach in cases in which we do not have control over the class we want to compare. E.g. the class is pulled from an external dependency.&lt;br&gt;
There are way around that, e.g. defining a wrapper class which can sometimes be inconvenient. The type class approach does not have that restriction. We are free to implement &lt;code&gt;Comparator&lt;/code&gt; instance for every class we can our hands on.&lt;/p&gt;
&lt;h2&gt;
  
  
  Single vs multiple implementations
&lt;/h2&gt;

&lt;p&gt;Using &lt;code&gt;Comparable&lt;/code&gt; restricts us to a single Comparable implementation. There are ways around that too, e.g using subclasses and define different implementation for each subclass. Depending on the language we use we can define one or more type class implementations. While scala allows multiple type class implementations, expected they are not defined at the same resolution level. You can read more about it &lt;a href="https://scala-lang.org/files/archive/spec/2.13/07-implicits.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Haskell usually does not allow multiple type class instances. Although there are ways to avoid this restriction.&lt;/p&gt;
&lt;h2&gt;
  
  
  Lawfullness
&lt;/h2&gt;

&lt;p&gt;Every type class comes with a set of laws which every instance needs to abide to. In case of the &lt;code&gt;Comparator&lt;/code&gt; type class we have e.g.:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if x.equals(y) then x.compareTo(y) == 0

if x.compareTo(y) == 0 then y.compareTo(x) == 0

if x.compareTo(y) &amp;lt; 0 and y.compareTo(z) &amp;lt; 0
then x.comapreTo(z) &amp;lt; 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In general this is all there is to type classes. While this is just an example there is a plethora of type classes most of which come from category theory. But you definitely don't need to go into that topic to make use of them. In scala type classes are implemented using traits which are quite close to java interfaces.&lt;/p&gt;

&lt;p&gt;When it comes to scalas implementation of type classes the part that confuses people the most seems not to be the actual concept of type classes but the scala implicits magic behind the scenes. The magic is required because, different from haskell which defines type classes as first-class structure&lt;br&gt;
In scala they are not part of the language core, instead they are implemented using other language features such as implicit parameters and implicit conversions. Implicits can sometimes be confusing especially for newcomers, but don't let yourself get discouraged.&lt;/p&gt;

&lt;p&gt;As a result the notion of comparing instance of a certain class is externalized. Method definitions using the inheritance approach would look this this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Comparable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResultType&lt;/span&gt; &lt;span class="nf"&gt;methodWhichNeedsToCompareInstances&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// scala&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;methodWhichNeedsToCompareInstances&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="k"&gt;&amp;lt;:&lt;/span&gt; &lt;span class="kt"&gt;Comparable&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ResultType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While using the type class approach they would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// java&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;ResultType&lt;/span&gt; &lt;span class="nf"&gt;methodWhichNeedsToCompareInstances&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="nc"&gt;Comparator&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;comparator&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="c1"&gt;// scala&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;methodWhichNeedsToCompareInstances&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;comparator&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Comparator&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;ResultType&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I managed to gain you interest you can take a look at one of the following libraries like &lt;a href="https://github.com/typelevel/cats" rel="noopener noreferrer"&gt;cats&lt;/a&gt;, &lt;a href="https://github.com/scalaz/scalaz" rel="noopener noreferrer"&gt;scalaz&lt;/a&gt; for scala and &lt;a href="https://github.com/vavr-io/vavr" rel="noopener noreferrer"&gt;vavr&lt;/a&gt; for java which contain type class definitions and implementations for common types.&lt;/p&gt;

</description>
      <category>java</category>
      <category>scala</category>
      <category>functional</category>
    </item>
    <item>
      <title>Tackling huge pull requests</title>
      <dc:creator>Senjin Hajrulahovic</dc:creator>
      <pubDate>Wed, 11 May 2022 10:08:21 +0000</pubDate>
      <link>https://dev.to/senjinhajrulahovic/tackling-huge-pull-requests-moh</link>
      <guid>https://dev.to/senjinhajrulahovic/tackling-huge-pull-requests-moh</guid>
      <description>&lt;p&gt;Bigger tasks require a lot of code changes, sometimes resulting in huge pull requests which are nearly impossible to review properly.&lt;/p&gt;

&lt;p&gt;How can we mitigate that? The first thing which comes to mind is to not let it happen in the first place. How? By diving the task into smaller tasks and implement them step by step. Although preferable, this approach is not always possible.&lt;/p&gt;

&lt;p&gt;A prime example for that is a major upgrade of a framework or a library which used all over the place. In case that upgrade happens to introduce a lot of breaking changes, it renders the aforementioned approach useless.&lt;/p&gt;

&lt;p&gt;A solution for such a case is to structure your commits in a way that makes it easier to review. GitHub, for example, provides a feature which allows the reviewer to review the pull request commit by commit.&lt;/p&gt;

&lt;p&gt;At first it sounds cumbersome, because in addition to the implementing the actual task you also have to keep in mind the way in which you commit your changes. Instead of committing you code at random, or event worse - not committing at all until your finished, you have to make your commits thoughtfully.&lt;/p&gt;

&lt;p&gt;But this is actually an advantage if you look at it from the right angle. While this approach is definitely more demanding it has the advantage of forcing you to create a high level plan, to approach you task thoughtfully. This forces you to think of your implementation as a sequence of steps which you need to take to the solution. This prevents you to just start hacking away and finding yourself afterwards with a bunch of unrelated changed files, having a hard time coming up with a commit message.&lt;/p&gt;

&lt;p&gt;You can even go step a further. You will probably agree that formatting changes can obfuscate the real changes that were made in order to accomplish the task at hand. In order to mitigate that we can make ourselves commit changes in formatting in separate commit. If we mark these commits as "formatting changes only" they can safely be ignored by the reviewer, and we will further ease the reviewing process.&lt;/p&gt;

&lt;p&gt;Keep in mind that someone will eventually have to review the code you're writing and try to ease the work of the reviewer. The reviewer is you friend :)&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Utilizing Scala Cats to improve database access code</title>
      <dc:creator>Senjin Hajrulahovic</dc:creator>
      <pubDate>Wed, 23 Mar 2022 10:36:32 +0000</pubDate>
      <link>https://dev.to/senjinhajrulahovic/utilizing-scala-cats-to-improve-database-access-code-e4l</link>
      <guid>https://dev.to/senjinhajrulahovic/utilizing-scala-cats-to-improve-database-access-code-e4l</guid>
      <description>&lt;p&gt;Let start with something familiar. If you worked with scala for some time you most likely came across &lt;code&gt;scala.concurrent.Future#sequence&lt;/code&gt; and &lt;code&gt;scala.concurrent.Future#traverse&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here is a simplified method signature of these two methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;, &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;sequence&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Future&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;cats.Traverse&lt;/code&gt; is an abstraction which embodies the operation &lt;code&gt;scala.concurrent.Future#traverse&lt;/code&gt; but is more general. Since in addition to the function argument type &lt;code&gt;A&lt;/code&gt; and return type &lt;code&gt;B&lt;/code&gt;, the context type of both &lt;code&gt;Seq&lt;/code&gt; and &lt;code&gt;Future&lt;/code&gt; are further generalized:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;Traverse&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;F&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;traverse&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;G&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="k"&gt;_&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;, &lt;span class="kt"&gt;A&lt;/span&gt;, &lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;fa&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;F&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;])(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;G&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;])&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;G&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;F&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using a newer version of &lt;code&gt;slick&lt;/code&gt; for database access you're most likely familiar with &lt;code&gt;DBIO&lt;/code&gt;. There are often cases when you have a collection of some sort and want to execute a database action for each of the elements in the collection.&lt;br&gt;
Since &lt;code&gt;DBIO&lt;/code&gt; is somewhat similar to &lt;code&gt;Future&lt;/code&gt; we could write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;DBIO&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="o"&gt;???&lt;/span&gt;

&lt;span class="k"&gt;val&lt;/span&gt; &lt;span class="nv"&gt;b&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;DBIO&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;B&lt;/span&gt;&lt;span class="o"&gt;]]&lt;/span&gt; &lt;span class="k"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;DBIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;sequence&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or if we lack documentation awareness we could event end up writing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nv"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;foldLeft&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;DBIO&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;successful&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Seq&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;acc&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;flatMap&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;f&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="py"&gt;map&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;acc&lt;/span&gt; &lt;span class="o"&gt;:+&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if we utilize slickcats we can simply write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="nv"&gt;items&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;traverse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you try to use this approach without the proper dependencies you'll see that the compilers complains about missing implicit instances for &lt;code&gt;DBIO&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In order to fix that you have to add following dependency:&lt;br&gt;
&lt;a href="https://github.com/RMSone/slick-cats" rel="noopener noreferrer"&gt;https://github.com/RMSone/slick-cats&lt;/a&gt;&lt;br&gt;
and add following import to your class:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;com.rms.miu.slickcats.DBIOInstances._&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on how far you are in you functional programming journey you'll know that &lt;code&gt;cats.Traverse&lt;/code&gt; is a special kind of functor which requires a type constructor with a single type argument.&lt;/p&gt;

&lt;p&gt;If the methods you want to use together with &lt;code&gt;slickcats&lt;/code&gt; return &lt;code&gt;DBIOAction[+R, +S &amp;lt;: NoStream, -E &amp;lt;: Effect]&lt;/code&gt; or some other related type which has more than one type parameter the compiler will complain. In these situation you'll have to help the compiler a bit by either changing the method signature to &lt;code&gt;DBIO&lt;/code&gt; or you'll have to cast it to &lt;code&gt;DBIO&lt;/code&gt; explicitly.&lt;/p&gt;

&lt;p&gt;This is just an example. &lt;code&gt;slickcats&lt;/code&gt; has typeclass instances which can help you to write more readable database access code.&lt;/p&gt;

</description>
      <category>functional</category>
      <category>scala</category>
      <category>database</category>
    </item>
    <item>
      <title>Tandem reading books - NoSQL</title>
      <dc:creator>Senjin Hajrulahovic</dc:creator>
      <pubDate>Sun, 31 Oct 2021 18:03:38 +0000</pubDate>
      <link>https://dev.to/senjinhajrulahovic/tandem-reading-books-nosql-22b0</link>
      <guid>https://dev.to/senjinhajrulahovic/tandem-reading-books-nosql-22b0</guid>
      <description>&lt;p&gt;Driven by the wish to explore the landscape of NoSql databases I decided to research the books available on the subject.&lt;/p&gt;

&lt;p&gt;After browsing reviews and top lists of books on the subject I narrowed it down to these two books:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.com/Seven-Databases-Weeks-Modern-Movement/dp/1934356921" rel="noopener noreferrer"&gt;Seven Databases in Seven Weeks&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.amazon.com/NoSQL-Mere-Mortals-Dan-Sullivan/dp/0134023218" rel="noopener noreferrer"&gt;NoSQL for Mere Mortals&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Both of them cover all the mainstream database families. Namely relational, key-value, columnar, graph and document databases.&lt;/p&gt;

&lt;p&gt;Unable to decide which one to pick I decided to read them both. Back to back... to back.&lt;/p&gt;

&lt;p&gt;Although I was initially put of by the title of the second one I still decided to read it despite not being a mere mortal.&lt;/p&gt;

&lt;p&gt;"Seven Databases in Seven Weeks" is basically a collection of quickstart guides for one NoSql database per database family. It goes less into concepts and focuses more on implementation. After each chapter you will have a functioning example of simple database interactions with the database in question. The book does not shy away from using databases which are exclusively available through cloud providers. After completing one of the chapters you'll have a working DynamoDB instance hosted on AWS.&lt;/p&gt;

&lt;p&gt;NoSQL for mere mortals, on the other hand, goes a lot deeper regarding motivation and limitations of each database family. It mostly stays database agnostic and focuses on guiding you through the concepts which are driving the design and behavior of the database families in question. Although it does not contain code it presents use cases for all the NoSql database families and provides conceptual solutions for them.&lt;/p&gt;

&lt;p&gt;Prior to reading these books I was highly biased towards relation databases. Although I worked with MongoDB and Redis, most of the time I work with relational databases like PostgreSQL and MySQL. The idea that you have to do additional work up front but reap the rewards later suited me. Benefits like enforced data integrity with schema and foreign keys, transactions, "easy" querying and so forth.&lt;/p&gt;

&lt;p&gt;Reading these books did not change my preconceived notion that much. NoSql database can be vary useful for specific tasks which fit the database model. It is of utmost importance to truly accept and embrace a particular NoSql database and its concepts. If you try to mimic the relational model in any of these database you'll have a bad time.&lt;/p&gt;

&lt;p&gt;But one has to admit that one of the things in which NoSql databases are far ahead of relational databases is distributability. A lot of NoSql databases are build from the ground up with distributability in mind.&lt;/p&gt;

</description>
      <category>database</category>
    </item>
    <item>
      <title>Mocking scala method calls containing type parameters with context bounds</title>
      <dc:creator>Senjin Hajrulahovic</dc:creator>
      <pubDate>Sat, 16 Oct 2021 10:33:03 +0000</pubDate>
      <link>https://dev.to/senjinhajrulahovic/mocking-method-calls-containing-type-parameters-with-context-bounds-5736</link>
      <guid>https://dev.to/senjinhajrulahovic/mocking-method-calls-containing-type-parameters-with-context-bounds-5736</guid>
      <description>&lt;p&gt;In our example we will be looking at a very simple auditing mechanism. Every time a resource changes we want to store its previous and its new state as json.&lt;/p&gt;

&lt;p&gt;While investigating the code base we come across a trait like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="nc"&gt;AuditLogHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;addAuditLog&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Writes&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We find out that &lt;code&gt;Writes&lt;/code&gt; is needed to create the json form of the object, and we notice that Session takes care of actually storing the values in the database.&lt;/p&gt;

&lt;p&gt;After creating a class which utilizes an instance of the trait above we start to write tests. We decide to use mocking.&lt;/p&gt;

&lt;p&gt;Our first attempt to mock the &lt;code&gt;addAuditLog&lt;/code&gt; method will most likely look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;auditLogHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;addAuditLog&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;&lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)(&lt;/span&gt;&lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;expects&lt;/span&gt;&lt;span class="o"&gt;(*,&lt;/span&gt; &lt;span class="o"&gt;*,&lt;/span&gt; &lt;span class="o"&gt;*).&lt;/span&gt;&lt;span class="py"&gt;anyNumberOfTimes&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="py"&gt;returning&lt;/span&gt;&lt;span class="o"&gt;(())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;which will result in the error message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unspecified value parameters: session: Session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We scratch our heads in disbelief. How can it be unspecified if it is right there?! We think that our IDE got confused, which happens from time to time. So we decide to recompile it with sbt which gives us the same error.&lt;/p&gt;

&lt;p&gt;After spending some time looking up the issue without results we analyze our dependencies. Their versions seem to be quite behind. Full of hope we update &lt;code&gt;scalatest&lt;/code&gt; and other related dependencies just to see the same outcome.&lt;/p&gt;

&lt;p&gt;Still:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Unspecified value parameters: session: Session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hopefully we eventually remember that &lt;code&gt;A: Writes&lt;/code&gt; is just syntactic sugar for an implicit parameter of type &lt;code&gt;Writes[A]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;addAuditLog&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A:&lt;/span&gt; &lt;span class="kt"&gt;Writes&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is actually:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;addAuditLog&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;](&lt;/span&gt;&lt;span class="n"&gt;previous&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
        &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;implicit&lt;/span&gt; &lt;span class="n"&gt;writes&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Writes&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="k"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After trying it out it proves to be the solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scala"&gt;&lt;code&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;auditLogHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;addAuditLog&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;Writes&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Assembly&lt;/span&gt;&lt;span class="o"&gt;],&lt;/span&gt; &lt;span class="k"&gt;_:&lt;/span&gt; &lt;span class="kt"&gt;Session&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt;
    &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="py"&gt;expects&lt;/span&gt;&lt;span class="o"&gt;(*,&lt;/span&gt; &lt;span class="o"&gt;*,&lt;/span&gt; &lt;span class="o"&gt;*).&lt;/span&gt;&lt;span class="py"&gt;anyNumberOfTimes&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="py"&gt;returning&lt;/span&gt;&lt;span class="o"&gt;(())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the solution we can't help but feel a bit irritated that it took us that much to figure out, while still being happy that we actually got it to work in the end.&lt;/p&gt;

</description>
      <category>scala</category>
      <category>testing</category>
    </item>
  </channel>
</rss>
