Aufgabe6: add extra exercises, remove Third runner
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
parent
7903970f08
commit
52525c4045
6 changed files with 126 additions and 53 deletions
|
|
@ -1,15 +0,0 @@
|
|||
load("@rules_scala//scala:scala.bzl", "scala_binary", "scala_library")
|
||||
|
||||
# Reusable library; reference it by label from other BUILD files.
|
||||
scala_library(
|
||||
name = "runner",
|
||||
srcs = ["Runner.scala"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
# Runnable: bazel run //m323/Sources/Aufgabe6/Third:app
|
||||
scala_binary(
|
||||
name = "app",
|
||||
main_class = "Runner",
|
||||
deps = [":runner"],
|
||||
)
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
object Runner {
|
||||
def main(args: Array[String]) = {
|
||||
val naturals = LazyList.from(1)
|
||||
println(naturals.take(8).toList)
|
||||
}
|
||||
}
|
||||
31
Sources/Aufgabe6/extra.swift
Normal file
31
Sources/Aufgabe6/extra.swift
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
import Foundation
|
||||
|
||||
actor Warehouse {
|
||||
var stock: Int = 10
|
||||
func takeFromStock(amount: Int, thread: String) {
|
||||
let localStock = stock
|
||||
stock -= amount
|
||||
print(thread)
|
||||
}
|
||||
}
|
||||
|
||||
@main
|
||||
struct App {
|
||||
static func main() async {
|
||||
let warehouse = Warehouse()
|
||||
|
||||
let threadB = Task.detached {
|
||||
await warehouse.takeFromStock(amount: 2, thread: "B")
|
||||
}
|
||||
|
||||
let threadA = Task.detached {
|
||||
await warehouse.takeFromStock(amount: 1, thread: "A")
|
||||
}
|
||||
|
||||
_ = await threadB.result
|
||||
_ = await threadA.result
|
||||
|
||||
let finalStock = await warehouse.stock
|
||||
print("Endgültiger Lagerbestand: \(finalStock) Stück")
|
||||
}
|
||||
}
|
||||
95
Sources/Aufgabe6/extraUnsafe.swift
Normal file
95
Sources/Aufgabe6/extraUnsafe.swift
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
import Foundation
|
||||
|
||||
@main
|
||||
|
||||
struct App {
|
||||
|
||||
static func main() {
|
||||
|
||||
class Warehouse {
|
||||
|
||||
var stock: Int = 10
|
||||
|
||||
}
|
||||
|
||||
let warehouse = Warehouse()
|
||||
|
||||
let semStartB = DispatchSemaphore(value: 0)
|
||||
|
||||
let semBFinishedRead = DispatchSemaphore(value: 0)
|
||||
|
||||
let semAFinishedCart = DispatchSemaphore(value: 0)
|
||||
|
||||
let semBFinishedUpdate = DispatchSemaphore(value: 0)
|
||||
|
||||
let semDone = DispatchSemaphore(value: 0)
|
||||
|
||||
Thread.detachNewThread {
|
||||
|
||||
let localStockA = warehouse.stock
|
||||
|
||||
print("Thread A liest den Lagerbestand (\(localStockA) Stück)")
|
||||
|
||||
semStartB.signal()
|
||||
|
||||
semBFinishedRead.wait()
|
||||
|
||||
print("Thread A legt Smartphone in den Warenkorb")
|
||||
|
||||
let newStockA = localStockA - 1
|
||||
|
||||
semAFinishedCart.signal()
|
||||
|
||||
semBFinishedUpdate.wait()
|
||||
|
||||
warehouse.stock = newStockA
|
||||
|
||||
print(
|
||||
|
||||
"Thread A aktualisiert den Lagerbestand auf \(warehouse.stock) Stück (\(localStockA) Stück - 1 Stück)"
|
||||
|
||||
)
|
||||
|
||||
semDone.signal()
|
||||
|
||||
}
|
||||
|
||||
Thread.detachNewThread {
|
||||
|
||||
semStartB.wait()
|
||||
|
||||
let localStockB = warehouse.stock
|
||||
|
||||
print("Thread B liest den Lagerbestand (\(localStockB) Stück)")
|
||||
|
||||
print("Thread B legt 2 Smartphones in den Warenkorb")
|
||||
|
||||
let newStockB = localStockB - 2
|
||||
|
||||
semBFinishedRead.signal()
|
||||
|
||||
semAFinishedCart.wait()
|
||||
|
||||
warehouse.stock = newStockB
|
||||
|
||||
print(
|
||||
|
||||
"Thread B aktualisiert den Lagerbestand auf \(warehouse.stock) Stück (\(localStockB) Stück - 2 Stück)"
|
||||
|
||||
)
|
||||
|
||||
semBFinishedUpdate.signal()
|
||||
|
||||
semDone.signal()
|
||||
|
||||
}
|
||||
|
||||
semDone.wait()
|
||||
|
||||
semDone.wait()
|
||||
|
||||
print("Endgültiger Lagerbestand im System: \(warehouse.stock) Stück")
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -2,6 +2,3 @@ import Foundation
|
|||
|
||||
print(studentAttendsCourse(studentName: "Petra", courses: courses))
|
||||
print(studentAttendsCourse(studentName: "Peter", courses: courses))
|
||||
|
||||
let result = allowedToLeaveHome().unsafeRun()
|
||||
print("Allowed to leave home: \(result)")
|
||||
|
|
|
|||
|
|
@ -1,29 +0,0 @@
|
|||
// IO monad: wraps a thunk (deferred computation)
|
||||
struct IO<A> {
|
||||
private let effect: () -> A
|
||||
|
||||
init(_ effect: @escaping () -> A) {
|
||||
self.effect = effect
|
||||
}
|
||||
|
||||
// Transform the result without executing
|
||||
func map<B>(_ f: @escaping (A) -> B) -> IO<B> {
|
||||
IO<B> { f(self.effect()) }
|
||||
}
|
||||
|
||||
func unsafeRun() -> A {
|
||||
effect()
|
||||
}
|
||||
}
|
||||
|
||||
func rollDiceImpure() -> Int {
|
||||
return Int.random(in: 1...6)
|
||||
}
|
||||
|
||||
func rollDice() -> IO<Int> {
|
||||
IO { rollDiceImpure() }
|
||||
}
|
||||
|
||||
func allowedToLeaveHome() -> IO<Bool> {
|
||||
rollDice().map { $0 == 6 }
|
||||
}
|
||||
Loading…
Reference in a new issue