Skip to content

Instantly share code, notes, and snippets.

Avatar

Adam Warski adamw

View GitHub Profile
View update_signature.md
.libv1> builtins.merge
test1: Nat -> Nat
test1 n = n + n
View x.scala
package sttp.tapir.examples
import cats.effect.{ExitCode, IO, IOApp}
import org.http4s.HttpRoutes
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.Router
import sttp.model.{Part, StatusCode}
import sttp.model.headers.WWWAuthenticateChallenge
import sttp.tapir._
import sttp.tapir.generic.auto._
View HelloWorldAkkaServer.scala
package sttp.tapir.examples
import akka.actor.ActorSystem
import akka.http.scaladsl.Http
import akka.http.scaladsl.server.Route
import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
import scala.io.StdIn
@adamw
adamw / gen.scala
Created September 6, 2022 07:15
How to generate an exhaustive match for a sealed trait in a Scala 3 macro?
View gen.scala
// TestMacro.scala
import scala.quoted.*
object TestMacro {
inline def name[E](e: E): String = ${ nameImpl[E]('e) }
def nameImpl[E: Type](e: Expr[E])(using Quotes): Expr[String] = {
import quotes.reflect.*
View test.scala
object UsingIO:
trait Connection
trait User
case class IO[-R, +A]():
def *>[R1 <: R, B](that: => IO[R1, B]): IO[R1, B] = IO()
def foo(): IO[Connection, Unit] = IO()
def bar(): IO[User, Unit] = IO()
@adamw
adamw / windowing.scala
Created August 5, 2016 13:30
Windowing data in Akka
View windowing.scala
package com.softwaremill.akka
import java.time._
import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source
import scala.collection.mutable
import scala.concurrent.Await
@adamw
adamw / x.scala
Last active March 31, 2022 14:20
View x.scala
opaque type NonEmptyString = String
object NonEmptyString:
def apply(s: String): Option[NonEmptyString] =
if s.isEmpty then None else Some(s)
@adamw
adamw / log.scala
Last active May 10, 2021 09:33
Logging request duration, path, status code using Akka HTTP
View log.scala
val rejectionHandler = RejectionHandler.default
def logDuration(inner: Route): Route = { ctx =>
val start = System.currentTimeMillis()
// handling rejections here so that we get proper status codes
val innerRejectionsHandled = handleRejections(rejectionHandler)(inner)
mapResponse { resp =>
val d = System.currentTimeMillis() - start
logger.info(s"[${resp.status.intValue()}] ${ctx.request.method.name} ${ctx.request.uri} took: ${d}ms")
resp
}(innerRejectionsHandled)(ctx)
View gist:d06722ab6826aad108beca979065a90b
"pool-48-thread-5" #225 prio=5 os_prio=31 cpu=172535.04ms elapsed=180.55s tid=0x00007fd765e3d800 nid=0xdd0b runnable [0x0000700019eb4000]
java.lang.Thread.State: RUNNABLE
at dotty.tools.dotc.core.Types$Type.classSymbol(Types.scala:488)
at dotty.tools.dotc.transform.patmat.SpaceEngine.canDecompose(Space.scala:649)
at dotty.tools.dotc.transform.patmat.SpaceLogic.simplify(Space.scala:147)
at dotty.tools.dotc.transform.patmat.SpaceLogic.simplify$(Space.scala:78)
at dotty.tools.dotc.transform.patmat.SpaceEngine.simplify(Space.scala:323)
at dotty.tools.dotc.transform.patmat.SpaceLogic.isSubspace(Space.scala:176)
at dotty.tools.dotc.transform.patmat.SpaceLogic.isSubspace$(Space.scala:78)
at dotty.tools.dotc.transform.patmat.SpaceEngine.isSubspace(Space.scala:323)
View test.scala
package sttp.tapir.examples
import cats.effect._
import cats.syntax.all._
import org.http4s.HttpRoutes
import org.http4s.server.Router
import org.http4s.server.blaze.BlazeServerBuilder
import org.http4s.syntax.kleisli._
import sttp.client3._
import sttp.tapir._