Skip to content

Instantly share code, notes, and snippets.

View klaeufer's full-sized avatar
:octocat:

Konstantin Läufer klaeufer

:octocat:
View GitHub Profile
import scala.util.Random
import java.util.concurrent.locks.ReentrantLock
def dinner(number: Int, delayInMs: Int = 10) =
val forks = Array.fill(number)(new ReentrantLock)
// warning: running each philosopher in their own explicit Java thread
def phil(i: Int) = new Thread:
override def run(): Unit =
@klaeufer
klaeufer / ThreadSafety.scala
Last active March 13, 2026 21:39
Unsafe and safe concurrent threads for experimenting in the Scala REPL
var shared = 0
val lock = new java.lang.Object
def unsafe =
val local = shared
Thread.sleep(0)
shared = local + 1
def safe =
lock.synchronized:
@klaeufer
klaeufer / IOboundTasks.scala
Last active March 13, 2026 21:30
I/O-bound tasks for experimenting in the Scala REPL
def p(s: String) =
for i <- 1 to 10 do
Thread.sleep(500)
println(s"$i : $s")
// running two or more tasks sequentially
// about 20 seconds by design
val t0 = System.currentTimeMillis
p("a")
@klaeufer
klaeufer / CPUboundTasks.scala
Last active March 13, 2026 21:25
CPU-bound tasks for experimenting in the Scala REPL
// a CPU-intensive task
def isPrime(i: Long): Boolean =
if i == 2 then return true
if i < 2 || i % 2 == 0 then return false
var k = 3L
val half = i / 2
while k <= half do
if i % k == 0 then return false
k += 2
true
@klaeufer
klaeufer / KLSigPipeFun.scala
Last active March 13, 2026 21:15 — forked from gkthiruvathukal/KLSigPipeFun.scala
Tinkering with Konstantin's example
object KLSigPipeFun:
def main(args: Array[String]) =
val lines = scala.io.Source.stdin.getLines()
val lineNumbers = Iterator.from(1)
val numberedLines = lineNumbers.zip(lines)
val okLines = numberedLines takeWhile: _ =>
!scala.sys.process.stdout.checkError()
val outLines = okLines map:
case (number, line) => println(s"$number: line")
println(s"${outLines.length} lines counted")
@klaeufer
klaeufer / tlatex.sh
Last active May 14, 2025 21:38
Converts a TLA+ source to a LaTeX snippet containing only the resulting tlatex environment
#!/bin/bash
# Copyright 2024 Konstantin Läufer <laufer AT cs.luc.edu>
#
# 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
#
@klaeufer
klaeufer / SimpleList.lean
Last active December 31, 2024 21:41
Lean 4: simple experiments with List
import Mathlib.Data.List.Basic
section
-- BEGIN
-- https://lean-lang.org/blog/2024-1-11-recursive-definitions-in-lean/
-- TODO make tail-recursive?
@klaeufer
klaeufer / SimpleNat.lean
Last active December 30, 2024 20:52
Lean 4: simple experiments with Nat
import Mathlib.Data.Nat.Factorization.Basic
import Mathlib.Data.Nat.Prime.Basic
open Nat
open Prime
section
-- BEGIN
def claim := ∀ p : Prop, ∀ q : Prop, p → (q → p)
// see also https://www.implicitdef.com/2015/11/19/comparing-scala-http-client-libraries.html
// task: use the prime checker webservice
// https://github.com/LoyolaChicagoCode/primenumbers-spray-scala
// to count the number of primes within a given range
/* instructions: create a build.sbt file in your project root folder containing the following lines:
scalaVersion := "3.3.3"
@klaeufer
klaeufer / catfacts.json
Last active April 18, 2024 22:43
LUC COMP 141 Materials
[
{
"breed": "Abyssinian",
"country": "Ethiopia",
"origin": "Natural\/Standard",
"coat": "Short",
"pattern": "Ticked"
},
{
"breed": "Aegean",