- Rename "Aufgabe 5"->Aufgabe5 and "Aufgabe 6"->Aufgabe6; spaces broke bazel run (Aspect URL-encoding, scala/JVM launcher self-path). - Per-directory BUILD.bazel: Aufgabe5 (swift_binary), Aufgabe6 (swift_binary). - Aufgabe6/Third: Scala app via rules_scala (scala_library + scala_binary). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
29 lines
534 B
Swift
29 lines
534 B
Swift
// 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 }
|
|
}
|