This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var shared = 0 | |
| val lock = new java.lang.Object | |
| def unsafe = | |
| val local = shared | |
| Thread.sleep(0) | |
| shared = local + 1 | |
| def safe = | |
| lock.synchronized: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 | |
| # |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Mathlib.Data.List.Basic | |
| section | |
| -- BEGIN | |
| -- https://lean-lang.org/blog/2024-1-11-recursive-definitions-in-lean/ | |
| -- TODO make tail-recursive? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [ | |
| { | |
| "breed": "Abyssinian", | |
| "country": "Ethiopia", | |
| "origin": "Natural\/Standard", | |
| "coat": "Short", | |
| "pattern": "Ticked" | |
| }, | |
| { | |
| "breed": "Aegean", |
NewerOlder