Skip to content

Instantly share code, notes, and snippets.

View lbialy's full-sized avatar

Łukasz Biały lbialy

View GitHub Profile
@lbialy
lbialy / 5-example-js.scala
Created June 30, 2026 12:37
par — example JS — scala/toolkit#31
import scala.concurrent.*
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
@main def demo(): Unit =
val items = Vector.range(1, 9)
val result: Future[Vector[Int]] = items.parMap(parallelism = 4)(_ * 2)
result.foreach: xs =>
println(s"parMap result: $xs")
@lbialy
lbialy / 4-example-jvm-native.scala
Created June 30, 2026 12:37
par — example JVM/Native — scala/toolkit#31
@main def demo(): Unit =
def slow(i: Int): Int =
Thread.sleep(100)
i * i
val items = Vector.range(1, 33)
def time[A](label: String)(body: => A): A =
val t0 = System.nanoTime()
val r = body
@lbialy
lbialy / 3-platform-js.scala
Created June 30, 2026 12:37
par — platform JS (R[A]=Future[A]) — scala/toolkit#31
import scala.concurrent.*
object platform:
type R[A] = Future[A]
def parRun[A](parallelism: Int)(body: ExecutionContext => Future[A]): R[A] =
import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
body(summon[ExecutionContext])
@lbialy
lbialy / 2-platform-jvm-native.scala
Created June 30, 2026 12:37
par — platform JVM/Native (R[A]=A, blocking) — scala/toolkit#31
import scala.concurrent.*
import scala.concurrent.duration.*
import java.util.concurrent.Executors
object platform:
type R[A] = A
def parRun[A](parallelism: Int)(body: ExecutionContext => Future[A]): R[A] =
val pool = Executors.newFixedThreadPool(parallelism.max(1))
val ec = ExecutionContext.fromExecutorService(pool)
@lbialy
lbialy / 1-par.scala
Created June 30, 2026 12:37
par — common parMap/parForeach (cross-platform parallel collections PoC, scala/toolkit#31)
//> using scala 3.8.3
import scala.concurrent.*
import scala.collection.BuildFrom
import scala.collection.generic.IsIterable
import java.util.concurrent.atomic.AtomicInteger
extension [Repr](xs: Repr)(using it: IsIterable[Repr])
def parMap[B, To](parallelism: Int)(f: it.A => B)(using
@lbialy
lbialy / yield.scala
Created September 19, 2025 14:13
Different thread yielding techniques with Loom
//> using scala 3.7.3
//> using dep com.softwaremill.ox::core:1.0.0
import scala.concurrent.duration.*
import java.time.Instant
import ox.*
import scala.util.boundary
import java.util.ArrayList
import java.util.concurrent.locks.LockSupport
@lbialy
lbialy / mbp-pro-m1.txt
Last active August 3, 2025 20:46
CE 3.7.0-RC1: Queue parallel producer & consumer - Scala JVM vs Scala Native vs GraalVM
----------------------------------------
mode lto gc cap ms
----------------------------------------
jvm n/a n/a 1024 217
graalvm-ni n/a n/a 1024 342
release-full full commix 65534 517
release-fast full commix 65534 523
release-size full commix 65534 554
jvm n/a n/a 65534 555
release-full full immix 65534 562
@lbialy
lbialy / tuplexxl.scala
Created November 26, 2024 11:20
How to repack arbitrary tuple of Eithers into a case class
import java.util.UUID
import java.time.Instant
import scala.deriving.Mirror
case class MyCaseClass(
intField: Int,
longField: Long,
doubleField: Double,
stringField: String,
booleanField: Boolean,
@lbialy
lbialy / main.go
Last active November 22, 2024 11:51
comparing scala native perf and golang
// run `go build main.go`
package main
import (
"fmt"
"os"
"strconv"
)
func fibonacci(n int) int {
@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)