Skip to content

Instantly share code, notes, and snippets.

View lbialy's full-sized avatar

Łukasz Biały lbialy

View GitHub Profile
@lbialy
lbialy / monad-laws.scala
Created April 11, 2022 15:57
Monadic laws in Scala 3
//> using scala "3.1.1"
// let's start with basic functor because all monads in scala have to be functors to work
// within a for-comprehension (because it desugars to flatMap + map chain)
// F[_] represents any concrete type parameterised with a single type (a type constructor), ie List, Option
trait Functor[F[_]]:
def map[A, B](fa: F[A])(f: A => B): F[B]
// define a correct interface for a Monad that extends that Functor, Monad is just
// two functions: pure (return in hs) and flatMap (bind in hs)
@lbialy
lbialy / loom.scala
Created October 3, 2022 08:36
An extremely looming Future for Scala, also parallel iterators are a thing now.
//> using scala "3.2"
import scala.concurrent.*, duration.*
import scala.collection.*
import java.util.concurrent.ExecutorService
import scala.language.implicitConversions
import java.util.concurrent.locks.ReentrantLock
object Looming:
sealed trait LoomingExecutionContext
@lbialy
lbialy / ai.scala
Last active January 3, 2025 01:43
I heard you like chatting with ChatGPT so I put ChatGPT inside of your Scala compilation so that you can chat with ChatGPT while you compile Scala
//> using lib "com.softwaremill.sttp.openai::core:0.0.6"
object ai:
import scala.quoted.*
import sttp.openai.OpenAISyncClient
import sttp.openai.requests.completions.chat.*
import sttp.openai.requests.completions.chat.ChatRequestBody.ChatBody
import sttp.openai.requests.completions.chat.ChatRequestBody.ChatCompletionModel
opaque type GPT[A <: String] <: String = String
//> using scala "3.3.0"
import scala.concurrent.*, ExecutionContext.Implicits.global, duration.*
import scala.util.boundary, boundary.*
def runsOnAnotherThread(msg: String): Future[Unit] =
Future:
println(s"This: $msg runs on ${Thread.currentThread.getName}")
def runTest(): Future[String] =
//> using scala "3.3.0"
import scala.concurrent.*, ExecutionContext.Implicits.global, duration.*
import scala.util.boundary, boundary.*
def runsOnAnotherThread(msg: String): Future[Unit] =
Future:
println(s"This: $msg runs on ${Thread.currentThread.getName}")
def testFatals(): Future[String] =
@lbialy
lbialy / local-mutability-under-spectacle.scala
Last active September 16, 2023 17:02
Early check whether Chimney can deal with mutable classes and whether it even makes sense to use localised mutability at all. Needs JMH.
//> using lib "io.github.martinhh::scalacheck-derived:0.4.1"
//> using lib "org.scalacheck::scalacheck:1.17.0"
//> using lib "io.scalaland::chimney:0.8.0-M1"
//> using lib "org.openjdk.jmh:jmh-core:1.37"
package local.mutability
import scala.collection.mutable
import io.scalaland.chimney.dsl.*
@lbialy
lbialy / release-repository.sh
Created September 24, 2023 08:05 — forked from romainbsl/release-repository.sh
Sonatype: close and release a staging repository
#!/usr/bin/env bash
username=
password=
stagedRepositoryId=
while [ "$1" != "" ]; do
case $1 in
-u | --username)
shift
@lbialy
lbialy / README.md
Created October 6, 2023 19:56
Scala Ox handling JDBC interruption
@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>
@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;