How to join 2 scala maps using cats

1 minute read Published: 2025-10-10

The following code snippet performs a full outer join between 2 scala Maps.

If using scala 2.12, you will need the -Ypartial-unification compiler flag.

import cats.Semigroup
import cats.data.Ior
import cats.syntax.all.*

def join[K, V1, V2](m1: Map[K, V1], m2: Map[K, V2]): Map[K, Ior[V1, V2]] = {
  def rightWins[A]: Semigroup[A] = Semigroup.instance { case (_, right) => right }
  implicit val semigroupV1 = rightWins[V1]
  implicit val semigroupV2 = rightWins[V2]

  m1.fmap(_.leftIor[V2]) |+| m2.fmap(_.rightIor[V1])
}

val m1 = Map("a" -> 1, "b" -> 2)
val m2 = Map("a" -> "foo", "c" -> "bar")

join(m1, m2)
// Map(a -> Ior.Both(1,foo), c -> Ior.Right(bar), b -> Ior.Left(2))

Creating a nix overlay to patch a python package

1 minute read Published: 2024-08-24

I recently ran into an error while updating my nixos configuration: the python package wxpython pulled as a dependency of another package was failing to build with the following error.

error: wxpython-4.2.1 not supported for interpreter python3.12

A quick search and I was able to find a patch to get wxpython to build. Rather than waiting for the patch to make its way into the nixpkgs repository, I decided to make an overlay for it. In nix, python package derivations are defined once but are built for multiple python versions so creating an overlay requires using pythonPackagesExtensions as follow.

self: super:

{
  pythonPackagesExtensions = super.pythonPackagesExtensions ++ [
    (pyfinal: pyprev: {
      wxpython = pyprev.wxpython.overrideAttrs (old: {
        disabled = false;
        postPatch =
          let
            waf_2_0_25 = super.fetchurl {
              url = "https://waf.io/waf-2.0.25";
              hash = "sha256-IRmc0iDM9gQ0Ez4f0quMjlIXw3mRmcgnIlQ5cNyOONU=";
            };
          in
          ''
            cp ${waf_2_0_25} bin/waf-2.0.25
            chmod +x bin/waf-2.0.25
            substituteInPlace build.py \
              --replace-fail "wafCurrentVersion = '2.0.24'" "wafCurrentVersion = '2.0.25'" \
              --replace-fail "wafMD5 = '698f382cca34a08323670f34830325c4'" "wafMD5 = 'a4b1c34a03d594e5744f9e42f80d969d'" \
              --replace-fail "distutils.dep_util" "setuptools.modified"
          '';
      });
    })
  ];
}