From 0bc8137078091a9c0c598940db89a56a63fce79e Mon Sep 17 00:00:00 2001 From: cediackermann Date: Mon, 15 Jun 2026 13:11:54 +0200 Subject: [PATCH] refactor(m323): per-directory BUILDs, drop dir spaces, add Scala app - 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 --- Sources/Aufgabe 5/main.swift | 29 ------------- Sources/{Aufgabe 5 => Aufgabe5}/BUILD.bazel | 2 +- Sources/Aufgabe5/main.swift | 29 +++++++++++++ Sources/Aufgabe6/BUILD.bazel | 7 ++++ Sources/Aufgabe6/Third/BUILD.bazel | 15 +++++++ Sources/Aufgabe6/Third/Runner.scala | 6 +++ Sources/Aufgabe6/first.swift | 46 +++++++++++++++++++++ Sources/Aufgabe6/main.swift | 7 ++++ Sources/Aufgabe6/second.swift | 29 +++++++++++++ 9 files changed, 140 insertions(+), 30 deletions(-) delete mode 100644 Sources/Aufgabe 5/main.swift rename Sources/{Aufgabe 5 => Aufgabe5}/BUILD.bazel (77%) create mode 100644 Sources/Aufgabe5/main.swift create mode 100644 Sources/Aufgabe6/BUILD.bazel create mode 100644 Sources/Aufgabe6/Third/BUILD.bazel create mode 100644 Sources/Aufgabe6/Third/Runner.scala create mode 100644 Sources/Aufgabe6/first.swift create mode 100644 Sources/Aufgabe6/main.swift create mode 100644 Sources/Aufgabe6/second.swift diff --git a/Sources/Aufgabe 5/main.swift b/Sources/Aufgabe 5/main.swift deleted file mode 100644 index b8610a5..0000000 --- a/Sources/Aufgabe 5/main.swift +++ /dev/null @@ -1,29 +0,0 @@ -import Foundation - -let weatherData: [(city: String, description: String, temperature: Double)] = [ - (city: "Zurich", description: "cloudy", temperature: 18.5), - (city: "Bern", description: "sunny", temperature: 22.1), - (city: "Geneva", description: "rainy", temperature: 15.3), - (city: "Basel", description: "sunny", temperature: 24.7), - (city: "Lucerne", description: "foggy", temperature: 11.0), - (city: "Lugano", description: "sunny", temperature: 31.2), - (city: "Lausanne", description: "cloudy", temperature: 19.8), - (city: "Chur", description: "snowing", temperature: -2.5), - (city: "Davos", description: "snowing", temperature: -5.1), - (city: "Locarno", description: "thunderstorm", temperature: 27.4), -] - -func weatherStation(city: String, description: String, temperature: Double) -> ( - String, String, Double -)? { - if temperature >= 20 { - let dateTimeString = Date.now.formatted(date: .abbreviated, time: .shortened) - return (description, dateTimeString, temperature) - } else { - return nil - } -} - -for weather in weatherData { - weatherStation(city: weather.0, description: weather.1, temperature: weather.2) -} diff --git a/Sources/Aufgabe 5/BUILD.bazel b/Sources/Aufgabe5/BUILD.bazel similarity index 77% rename from Sources/Aufgabe 5/BUILD.bazel rename to Sources/Aufgabe5/BUILD.bazel index 31fc7f8..2ddaeb0 100644 --- a/Sources/Aufgabe 5/BUILD.bazel +++ b/Sources/Aufgabe5/BUILD.bazel @@ -1,7 +1,7 @@ load("@rules_swift//swift:swift.bzl", "swift_binary") # Per-directory BUILD: define targets where their sources live, then reference -# them by label (e.g. //Sources/Aufgabe 5:Aufgabe5) from elsewhere to keep the +# them by label (e.g. //m323/Sources/Aufgabe5:Aufgabe5) from elsewhere to keep the # top-level BUILD.bazel uncluttered. swift_binary( name = "Aufgabe5", diff --git a/Sources/Aufgabe5/main.swift b/Sources/Aufgabe5/main.swift new file mode 100644 index 0000000..8a52745 --- /dev/null +++ b/Sources/Aufgabe5/main.swift @@ -0,0 +1,29 @@ +import Foundation + +let weatherData: [(city: String, description: String, temperature: Double)] = [ + (city: "Zurich", description: "cloudy", temperature: 18.5), + (city: "Bern", description: "sunny", temperature: 22.1), + (city: "Geneva", description: "rainy", temperature: 15.3), + (city: "Basel", description: "sunny", temperature: 24.7), + (city: "Lucerne", description: "foggy", temperature: 11.0), + (city: "Lugano", description: "sunny", temperature: 31.2), + (city: "Lausanne", description: "cloudy", temperature: 19.8), + (city: "Chur", description: "snowing", temperature: -2.5), + (city: "Davos", description: "snowing", temperature: -5.1), + (city: "Locarno", description: "thunderstorm", temperature: 27.4), +] + +func weatherStation(city: String, description: String, temperature: Double) -> ( + String, String, Double +)? { + if temperature >= 20 { + let dateTimeString = Date.now.formatted(date: .abbreviated, time: .shortened) + return (description, dateTimeString, temperature) + } else { + return nil + } +} + +for weather in weatherData { + weatherStation(city: weather.0, description: weather.1, temperature: weather.2) +} diff --git a/Sources/Aufgabe6/BUILD.bazel b/Sources/Aufgabe6/BUILD.bazel new file mode 100644 index 0000000..228d7f8 --- /dev/null +++ b/Sources/Aufgabe6/BUILD.bazel @@ -0,0 +1,7 @@ +load("@rules_swift//swift:swift.bzl", "swift_binary") + +swift_binary( + name = "Aufgabe6", + srcs = ["main.swift","first.swift", "second.swift"], + visibility = ["//visibility:public"], +) diff --git a/Sources/Aufgabe6/Third/BUILD.bazel b/Sources/Aufgabe6/Third/BUILD.bazel new file mode 100644 index 0000000..40d483b --- /dev/null +++ b/Sources/Aufgabe6/Third/BUILD.bazel @@ -0,0 +1,15 @@ +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"], +) diff --git a/Sources/Aufgabe6/Third/Runner.scala b/Sources/Aufgabe6/Third/Runner.scala new file mode 100644 index 0000000..d28c2ea --- /dev/null +++ b/Sources/Aufgabe6/Third/Runner.scala @@ -0,0 +1,6 @@ +object Runner { + def main(args: Array[String]) = { + val naturals = LazyList.from(1) + println(naturals.take(8).toList) + } +} diff --git a/Sources/Aufgabe6/first.swift b/Sources/Aufgabe6/first.swift new file mode 100644 index 0000000..b928447 --- /dev/null +++ b/Sources/Aufgabe6/first.swift @@ -0,0 +1,46 @@ +import Foundation + +class Course { + let name: String + let students: [String] + + init(name: String, students: [String]) { + self.name = name + self.students = students + } +} + +let courses = [ + Course(name: "M323", students: ["Peter", "Petra", "Paul", "Paula"]), + Course(name: "M183", students: ["Paula", "Franz", "Franziska"]), + Course(name: "M117", students: ["Paul", "Paula"]), + Course(name: "M114", students: ["Petra", "Paul", "Paula"]), +] + +// print( +// courses.map { +// return $0.students +// }.filter { +// $0.contains("Peter") +// }.count +// ) + +// print( +// courses.map { +// return $0.students +// }.filter { +// $0.contains("Petra") +// }.count +// ) + +func studentAttendsCourse(studentName: String, courses: [Course]) -> String { + let visitedCourses = courses.map { + return ($0.name, $0.students) + }.filter { + $0.1.contains(studentName) + }.map { + return $0.0 + } + return + "\(studentName) visits the following courses: \(visitedCourses.joined(separator: ", "))" +} diff --git a/Sources/Aufgabe6/main.swift b/Sources/Aufgabe6/main.swift new file mode 100644 index 0000000..97d908a --- /dev/null +++ b/Sources/Aufgabe6/main.swift @@ -0,0 +1,7 @@ +import Foundation + +print(studentAttendsCourse(studentName: "Petra", courses: courses)) +print(studentAttendsCourse(studentName: "Peter", courses: courses)) + +let result = allowedToLeaveHome().unsafeRun() +print("Allowed to leave home: \(result)") diff --git a/Sources/Aufgabe6/second.swift b/Sources/Aufgabe6/second.swift new file mode 100644 index 0000000..2c320b0 --- /dev/null +++ b/Sources/Aufgabe6/second.swift @@ -0,0 +1,29 @@ +// IO monad: wraps a thunk (deferred computation) +struct IO { + private let effect: () -> A + + init(_ effect: @escaping () -> A) { + self.effect = effect + } + + // Transform the result without executing + func map(_ f: @escaping (A) -> B) -> IO { + IO { f(self.effect()) } + } + + func unsafeRun() -> A { + effect() + } +} + +func rollDiceImpure() -> Int { + return Int.random(in: 1...6) +} + +func rollDice() -> IO { + IO { rollDiceImpure() } +} + +func allowedToLeaveHome() -> IO { + rollDice().map { $0 == 6 } +}