Skip to content

Instantly share code, notes, and snippets.

SystemFw / Ex.scala
Last active Dec 15, 2020
Update fields of a case class with a Diff generically, with support for nesting
View Ex.scala
object Ex {
import Lib._
case class Person(name: String, age: Int)
case class PersonOpt(name: Option[String], age: Option[Int])
def mergePerson(p: Person, po: PersonOpt) =
Person(, po.age.getOrElse(p.age))
case class Contact(person: Person, phone: String)
SystemFw / Example.scala
Last active Oct 29, 2020
Using shapeless to check exhaustiveness of Golden tests
View Example.scala
// stemming from
// The idea is
// read the JSON files in a directory
// try to decode each of them as Event which could be either One or Two, we don't know this at compile time
// serialize the values we got, again could be either One or Two and compare it with the content of the JSON file
// verify that there was at least a JSON file for One and Two (exhaustivity check)
import shapeless._
SystemFw / Ex.scala
Last active Nov 3, 2018
Encoding existentials via abstract types (1) and higher-ranks (2)
View Ex.scala
// This is the traditional encoding, using abstract type members
// to encode existentials
object AbstractExistentials {
trait E {
type X
val x: X
def f(p: X): String
SystemFw / Ex.scala
Created Oct 26, 2018
Get instances of a typeclass `TC[_]` for all cases of a Coproduct [shapeless]
View Ex.scala
import shapeless._
@annotation.implicitNotFound("Make sure all cases of ${C} have an instance of ${TC}")
trait CopTCs[C <: Coproduct, TC[_]] {
type Out <: HList
def result: Out
object CopTCs {
type Aux[C <: Coproduct, TC[_], O <: HList] = CopTCs[C, TC] { type Out = O }
def apply[C <: Coproduct, TC[_]](implicit ev: CopTCs[C, TC]) = ev
SystemFw / groupBy.scala
Created Jul 9, 2018
fs2 `groupBy/partitions`
View groupBy.scala
// Grows with the number of distinct `K`
def partitions[F[_], A, K](selector: A => F[K])(implicit F: Effect[F], ec: ExecutionContext) : Pipe[F, A, (K, Stream[F, A])] = in =>
Stream.eval(async.refOf[F, Map[K, Queue[F, Option[A]]]](Map.empty)).flatMap { st =>
val cleanup = {
import alleycats.std.all._
(in ++ Stream.eval_(cleanup)).evalMap { el =>
(selector(el), st.get).mapN { (key, queues) =>
SystemFw / RankN
Last active Jun 1, 2019
Cats-effect, blocking, RankN-types.
View RankN


The cats-effect project defines a purely functional effect type (IO[A]), and associated typeclasses defining its behaviour. The ones we care about for this example are:

trait Sync[F[_]] extends MonadError[F, Throwable] {
   def delay[A](a: => A): F[A]
SystemFw / Test.scala
Created Jan 21, 2018
Recursively increment every Int in a case class by a given runtime quantity
View Test.scala
object Test {
import inc._
case class Foo(i: Int, s: String)
case class Bar(f: Int, a: Foo, n: Int)
val res = Bar(0, Foo(1, "hello"), 2).incBy(2)
//res0: Test.Bar = Bar(2,Foo(3,hello),4)
SystemFw / Test.scala
Last active Jul 9, 2018
Parallel foldMonoid with fs2
View Test.scala
object Test {
import cats.implicits._
import cats.kernel.CommutativeMonoid
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// same as parallelFoldMonoid, but with some logging to show the parallelism
def loggedParallelFoldMonoid[F[_], A: CommutativeMonoid](n: Int)(
input: Stream[F, A])(implicit F: Effect[F], ec: ExecutionContext) =
SystemFw / Lib.scala
Last active Jun 7, 2019
Shapeless: derive JDBC Results for arbitrary case classes
View Lib.scala
import shapeless._ // requires.shapeless
import cats._, implicits._, data.Kleisli // requires.cats
import cats.sequence._ //requires kittens
import cats.effect.IO //requires cats-effect
// ofc, uses "-Ypartial-unification" and kind-projector
case class Result() // replace with the JDBC equivalent
case class DB(val r: Result) {
def nextInt: IO[Int] = ??? //IO(g.nextInt)
SystemFw / Q.scala
Created Oct 9, 2017
Shapeless: create an HList of functions from case class T to each of its fields
View Q.scala
object Q {
import shapeless._, labelled._, ops.record.Selector
trait Accessors[T, I] {
type Out
def value: Out
object Accessors {
type Aux[T, I, O] = Accessors[T, I] { type Out = O }