Skip to content

Instantly share code, notes, and snippets.


Fabio Labella SystemFw

View GitHub Profile
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 }
SystemFw / example.scala
Last active Jan 3, 2020
Running fs2 streams in parallel and collect their result in sequence, with queues
View example.scala
object Example {
import cats._, implicits._
import cats.effect._
import fs2._
import scala.concurrent.ExecutionContext
// start N streams concurrently, and stream their results in order
// e.g. download a file from a server in N parts concurrently, and stream it
abstract class Channel[F[_], A] {
SystemFw / Test.scala
Last active Jun 7, 2019
Shapeless: Convert between any two compatible case classes, selecting a subset of the fields
View Test.scala
object Test {
case class User(name: String, age: Int)
case class UserDTO(name: Option[String], age: Option[Int])
import conversions._
def a = User("John", 24).convertTo[UserDTO](Set("name"))
// res0: Test.UserDTO = UserDTO(Some(John),None)
case class WrongFieldNames(surname: Option[String], age: Option[Int])
You can’t perform that action at this time.