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 <noreply@anthropic.com>
This commit is contained in:
parent
36f6abeea4
commit
0bc8137078
9 changed files with 140 additions and 30 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
@ -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",
|
||||
29
Sources/Aufgabe5/main.swift
Normal file
29
Sources/Aufgabe5/main.swift
Normal file
|
|
@ -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)
|
||||
}
|
||||
7
Sources/Aufgabe6/BUILD.bazel
Normal file
7
Sources/Aufgabe6/BUILD.bazel
Normal file
|
|
@ -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"],
|
||||
)
|
||||
15
Sources/Aufgabe6/Third/BUILD.bazel
Normal file
15
Sources/Aufgabe6/Third/BUILD.bazel
Normal file
|
|
@ -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"],
|
||||
)
|
||||
6
Sources/Aufgabe6/Third/Runner.scala
Normal file
6
Sources/Aufgabe6/Third/Runner.scala
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
object Runner {
|
||||
def main(args: Array[String]) = {
|
||||
val naturals = LazyList.from(1)
|
||||
println(naturals.take(8).toList)
|
||||
}
|
||||
}
|
||||
46
Sources/Aufgabe6/first.swift
Normal file
46
Sources/Aufgabe6/first.swift
Normal file
|
|
@ -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: ", "))"
|
||||
}
|
||||
7
Sources/Aufgabe6/main.swift
Normal file
7
Sources/Aufgabe6/main.swift
Normal file
|
|
@ -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)")
|
||||
29
Sources/Aufgabe6/second.swift
Normal file
29
Sources/Aufgabe6/second.swift
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
// 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