Skip to content

Instantly share code, notes, and snippets.

View soujiro32167's full-sized avatar
👾
Software is eating the world

Eli Kasik soujiro32167

👾
Software is eating the world
  • Traverse Labs
  • Montreal, QC
View GitHub Profile
@soujiro32167
soujiro32167 / avro_discriminator.scala
Created February 28, 2023 16:37
When your avro needs a discriminator in the payload
import vulcan.{AvroError, Codec}
import vulcan.generic.*
import zio.{ZIO, ZIOAppDefault}
sealed trait Foo
object Foo {
case class A(a: Int, `type`: "A" = "A") extends Foo
case class B(b: String, `type`: "B" = "B") extends Foo
@soujiro32167
soujiro32167 / pgnotify.scala
Created January 13, 2023 20:19
Doobie postgres listen + notify CDC
import cats.effect.{IO, IOApp, Resource}
import doobie.postgres.PHC
import doobie.{ConnectionIO, HC, LogHandler, Transactor}
import fs2.Pipe
import org.postgresql.PGNotification
import scala.concurrent.duration.*
import cats.syntax.all.*
import doobie.implicits.*
import fs2.Stream
@soujiro32167
soujiro32167 / inline.scala
Created December 4, 2022 19:41
Scala 3: Matching on member of a union types
inline def f2[A, B](x: A | B): String =
x match
case a: A => "a"
case b: B => "b"
// doesn't work with subtyping
trait T[A] {
def foo(a: A): String
}
@soujiro32167
soujiro32167 / producer.scala
Created December 2, 2022 21:21
Multi-topic sealed trait kafka producer
import _root_.vulcan.Codec
import _root_.vulcan.generic.*
import foo.Events.{E1, E2}
import fs2.kafka.*
import fs2.kafka.vulcan.{AvroSettings, SchemaRegistryClientSettings, avroSerializer}
import zio.interop.catz.*
import zio.interop.catz.implicits.rts
import zio.{Scope, Task, ZIO, ZIOAppDefault}
object foo extends ZIOAppDefault {
@soujiro32167
soujiro32167 / newtypes.amm
Created October 3, 2022 14:02
Deriving typeclasses for zio-prelude newtypes outside of the object scope (scala2 only)
import $ivy.`dev.zio::zio-prelude:1.0.0-RC15`
import zio.prelude.Newtype
object newtypes {
def derive[A, STA <: Newtype[A], F[_]](implicit ev: STA <:< Newtype[A], typeClass: F[A]): F[STA#Type] =
typeClass.asInstanceOf[F[STA#Type]]
trait Auto {
implicit def auto[A, STA <: Newtype[A], F[_]](implicit ev: STA <:< Newtype[A], typeClass: F[A]): F[STA#Type] =
@soujiro32167
soujiro32167 / unioncompose.scala
Created October 3, 2022 13:33
Composing effects with union types
trait F[+A]{
def ++[B >: A](that: F[B]): F[B]
def flatMap[B >: A](f: A => F[B]): F[B]
def map[B](f: A => B): F[B]
}
trait A
trait B
trait C
@soujiro32167
soujiro32167 / flateither.scala
Created October 3, 2022 13:31
Flattening an Either tree in Scala 3
import scala.annotation.tailrec
type FlatEither[A] = A match
case Either[l, r] => FlatEither[l] | FlatEither[r]
case _ => A
@tailrec
def flatEither[A](a: A): FlatEither[A] = a match
case e: Either[?, ?] => e match
case Right(r) => flatEither(r)
@soujiro32167
soujiro32167 / bug.scala
Created October 30, 2021 19:41
Tapir ZIO Http composition bug
import sttp.client3.asynchttpclient.zio.{AsyncHttpClientZioBackend, SttpClient}
import sttp.tapir.server.ziohttp.ZioHttpInterpreter
import zhttp.http.RHttpApp
import zhttp.service.{EventLoopGroup, Server}
import zhttp.service.server.ServerChannelFactory
import zio.{Runtime, ZIO}
object tapir {
import sttp.tapir.ztapir._
@soujiro32167
soujiro32167 / logging.scala
Created March 22, 2021 17:34
Conditional logging in http4s
package com.byond.infinity.sda
import cats.data.{ Kleisli, OptionT }
import cats.effect.{ Resource, Sync }
import org.http4s.{ HttpRoutes, Response }
import cats.syntax.flatMap._
import org.http4s.client.Client
object logging {
val log = org.log4s.getLogger
@soujiro32167
soujiro32167 / aoc16.scala
Created December 20, 2020 01:35
AoC Day 16
package advent
import cats.syntax.all._
object Sixteen extends Inputs:
type Rule = Int => Boolean
type Ticket = List[Int]
type TicketPosition = List[Int -> Int]
def parseRule: String => String \/ Rule = { s =>