Skip to content

Instantly share code, notes, and snippets.

View lbialy's full-sized avatar

Łukasz Biały lbialy

View GitHub Profile
@lbialy
lbialy / no-illegal-states.scala
Created November 2, 2024 10:45
Making illegal states unrepresentable the Scala way
//> using scala 3.5.2
//> using dep "com.softwaremill.ox::core:0.5.2"
object types:
opaque type Title = String
object Title:
def apply(title: String): Either[Exception, Title] =
if title.isBlank then Left(Exception("Title must not be blank"))
else Right(title)
@lbialy
lbialy / kyo-discipline.scala
Created October 19, 2024 19:37
Kyo passes monadic laws via Discipline
//> using scala 3.5.1
//> using test.dep org.typelevel::cats-laws:2.12.0
//> using test.dep org.typelevel::discipline-munit:2.0.0
//> using test.dep io.getkyo::kyo-core:0.13.1
//> using test.dep io.getkyo::kyo-cats:0.13.1
//> using test.dep org.scalameta::munit:1.0.2
@lbialy
lbialy / graalvm-in-docker.txt
Created March 28, 2024 21:30
Have fun Anton
$ sbt new http4s/http4s.g8 --branch 1.0-scala3
// use all the defaults, just set sbt to 1.9.9
$ cd quickstart
$ sbt assembly
$ cat Dockerfile # create this file with these contents:
FROM ghcr.io/graalvm/jdk-community:22
RUN mkdir /opt/app
//> using scala 3.4.0
import language.experimental.captureChecking
class DbCon:
def runUpdate(): Unit = println(s"runUpdate on ${Thread.currentThread()}")
def close(): Unit = println("closing DbCon")
def transact[A](f: DbCon^ => A): A =
val dbCon = DbCon()
@lbialy
lbialy / backpressure.scala
Created March 2, 2024 22:10
Demonstration of backpressure with Loom
//> using scala 3.3.3
//> using jvm graalvm-java21:21.0.2
//> using dep com.softwaremill.ox::core:0.0.21
import ox.*
import java.util.concurrent.*
@main def main = supervised {
val queue = ArrayBlockingQueue[Int](5)
@lbialy
lbialy / Helpers.scala
Last active February 27, 2024 17:19 — forked from prolativ/Helpers.scala
import scala.quoted.*
extension (using quotes: Quotes)(repr: quotes.reflect.TypeRepr)
def flatMapResultType(f: quotes.reflect.TypeRepr => Option[quotes.reflect.TypeRepr]): Option[quotes.reflect.TypeRepr] =
import quotes.reflect.*
repr match
case MethodType(paramNames, paramTypes, resultType) =>
resultType.flatMapResultType(f).map(tp => MethodType(paramNames)(_ => paramTypes, _ => tp))
// case PolyType => // should not appear here
case tp =>
@lbialy
lbialy / caliban-demo.sc
Last active December 30, 2023 13:04
Run with `scala-cli run caliban-demo.sc` or `scala-cli run https://gist.github.com/lbialy/c9aa46136f3ee7897600c697de273567`
//> using dep com.github.ghostdogpr::caliban-quick:2.5.0
enum Origin {
case EARTH, MARS, BELT
}
case class Character(
name: String,
nicknames: List[String],
origin: Origin
@lbialy
lbialy / main.scala
Last active December 29, 2023 21:09
comparing word counting: std vs fs2
//> using scala "3.3.1"
//> using lib "co.fs2::fs2-io:3.9.3"
import scala.util.*
import scala.io.Source
def safePrintln(s: Any): Either[Throwable, Unit] =
try Right(println(s))
catch case err: Throwable => Left(err)
@lbialy
lbialy / app.decompiled.java
Created October 27, 2023 12:24
Decompiled Kotlin coroutine rendezvous from SML Blogpost
// AppKt.java
package hello;
import kotlin.Metadata;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function2;
import kotlin.jvm.internal.Intrinsics;
import kotlin.jvm.internal.SourceDebugExtension;
@lbialy
lbialy / pom.xml
Created October 27, 2023 11:10
Pom for kotlin coroutines rendezvous snippet
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>org.jetbrains.kotlin.examples</groupId>
<artifactId>hello-world</artifactId>
<version>1.0-SNAPSHOT</version>