Skip to content

Instantly share code, notes, and snippets.

Avatar
😱
I might take a week to respond. Or a month.

Jakub Kozłowski kubukoz

😱
I might take a week to respond. Or a month.
View GitHub Profile
View valuesOf.scala
import scala.deriving.*
import scala.compiletime.*
inline def valuesOf[T <: Tuple]: List[String] = inline erasedValue[T] match {
case _: EmptyTuple => Nil
case _: (h *: t) =>
erasedValue[h] match {
case label: String =>
label :: valuesOf[t]
}
View Labels.scala
trait Labels[S] {
def label(v: S): String
}
object Labels {
import scala.deriving.*
import scala.compiletime.*
inline def derived[T](using s: Mirror.SumOf[T]): Labels[T] =
@kubukoz
kubukoz / ConsoleDsl.scala
Created May 15, 2021
New DSL ideas for Scala 3
View ConsoleDsl.scala
trait ConsoleDsl {
// callers will never know what this is, and that's a good thing
type A
def println(s: String): A
def readLn: A
def combine(a: A, b: A): A
}
@kubukoz
kubukoz / TypeNames.scala
Created May 10, 2021
Names of all subtypes of a Scala ADT
View TypeNames.scala
trait TypeNames[A] {
def names: List[String]
}
object TypeNames {
def of[A](implicit ev: TypeNames[A]) = ev.names
implicit def forGenericThing[A, Repr <: Coproduct, Types <: HList](
implicit gen: Generic.Aux[A, Repr],
typeables: coproduct.LiftAll.Aux[Typeable, Repr, Types],
@kubukoz
kubukoz / shell.nix
Created Apr 20, 2021
sbt with older java (nix)
View shell.nix
{ pkgs ? import <nixpkgs> {} }:
let
java8 = pkgs.openjdk8;
in
pkgs.mkShell {
buildInputs = with pkgs;[ (sbt.override { jre = java8; }) ];
}
@kubukoz
kubukoz / JsonHeaderDropbox.scala
Last active Apr 18, 2021
Download file from dropbox with http4s
View JsonHeaderDropbox.scala
import cats.effect.IO
import cats.effect.IOApp
import io.circe.Printer
import io.circe.literal._
import org.http4s.AuthScheme
import org.http4s.Credentials
import org.http4s.Header
import org.http4s.Request
@kubukoz
kubukoz / playshapeless.scala
Created Apr 12, 2021
Play JSON format syntax for HLists
View playshapeless.scala
import shapeless.::
object Extensions extends LowPriorityExtensions {
implicit class NotHListOWritesConcat[A](right: OWrites[A])(implicit notHList: shapeless.Refute[A <:< HList]) {
//noinspection ScalaStyle
def ~::[B](head: OWrites[B]): OWrites[B :: A :: HNil] = (head ~ right) (hlist => (hlist.head, hlist.tail.head))
}
@kubukoz
kubukoz / ProcessRunner.scala
Last active Apr 9, 2021
Run system process with fs2
View ProcessRunner.scala
trait ProcessRunner[F[_]] {
def run(program: List[String])(errorOut: Pipe[F, Byte, Nothing]): Pipe[F, Byte, Byte]
}
object ProcessRunner {
def apply[F[_]](implicit F: ProcessRunner[F]): ProcessRunner[F] = F
// This is a relatively simple implementation, for the real deal go use something like vigoo/prox
implicit def instance[F[_]: Async]: ProcessRunner[F] = new ProcessRunner[F] {
@kubukoz
kubukoz / ResourceFileGoldenCodecLawsCompat.scala
Created Apr 7, 2021
circe-golden bloop-compatible paths
View ResourceFileGoldenCodecLawsCompat.scala
import java.io.File
import java.nio.file.Paths
import io.circe.Decoder
import io.circe.Encoder
import io.circe.Printer
import io.circe.testing.golden.GoldenCodecLaws
import io.circe.testing.golden.ResourceFileGoldenCodecLaws
import io.circe.testing.golden.Resources
@kubukoz
kubukoz / http4s-onEmptyOrNonEmpty.scala
Last active Apr 2, 2021
Return NotFound if http4s response stream is empty, wrap it in a different status otherwise
View http4s-onEmptyOrNonEmpty.scala
/*
Copyright 2021 Jakub Kozłowski
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software