Skip to content

Commit 7c5bf41

Browse files
committed
Piggyback unrelated tests
1 parent f70ac4a commit 7c5bf41

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import util.boundary
2+
import boundary.{break, Label}
3+
4+
def run(f: Int => String, x: Int): String =
5+
boundary:
6+
def recur(x: Int): String =
7+
if x < 0 then break("negative")
8+
else if x == 0 then ""
9+
else f(x) ++ " " ++ recur(x - 1)
10+
recur(x)
11+
12+
def runDyn(f: Int => Label[String] ?=> String, x: Int): String =
13+
boundary:
14+
def recur(x: Int): String =
15+
if x < 0 then break("negative")
16+
else if x == 0 then ""
17+
else f(x) ++ " " ++ recur(x - 1)
18+
recur(x)
19+
20+
def toStr(x: Int)(using lbl: Label[String]): String = x.toString
21+
22+
def middleDyn(x: Int)(using lbl: Label[String]) =
23+
if x == 1 then break("middleman attack")(using lbl)
24+
x.toString
25+
26+
case class Break(s: String) extends Exception
27+
28+
def runExc(f: Int => String, x: Int): String =
29+
try
30+
def recur(x: Int): String =
31+
if x < 0 then throw Break("negative")
32+
else if x == 0 then ""
33+
else f(x) ++ " " ++ recur(x - 1)
34+
recur(x)
35+
catch case Break(s) => s
36+
37+
def middleExc(x: Int) =
38+
if x == 1 then throw Break("middleman attack")
39+
x.toString
40+
41+
@main def Test =
42+
println(run(_.toString, 2))
43+
println(run(_.toString, -2))
44+
45+
println(runDyn(toStr, 2))
46+
println(runDyn(toStr, -2))
47+
println(runDyn(middleDyn, 2))
48+
49+
println(runExc(_.toString, 2))
50+
println(runExc(_.toString, -2))
51+
println(runExc(middleExc, 2))
52+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
(kind = Dog, age = 7, weight = 6.5)
2+
(kind = Dog, age = 7, weight = 6.5, growth = 0.9285714285714286)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import NamedTuple.*
2+
import compiletime.{constValue, erasedValue}
3+
4+
// The generic buildColumn function
5+
transparent inline def buildColumn[N <: Tuple, V <: Tuple, T](
6+
r: NamedTuple[N, V], name: String, f: NamedTuple[N, V] => T)
7+
(using Tuple.Disjoint[N, Tuple1[name.type]] =:= true)
8+
: Concat[NamedTuple[N, V], NamedTuple[Tuple1[name.type], Tuple1[T]]]
9+
= r ++ Tuple1(f(r)).withNames[Tuple1[name.type]]
10+
11+
extension [N <: Tuple, V <: Tuple](r: NamedTuple[N, V])
12+
inline def mkString: String =
13+
r.toSeqMap
14+
.map((n, v) => s"$n = $v")
15+
.mkString("(", ", ", ")")
16+
17+
// Concrete Pet example
18+
type Pet = (kind: String, age: Int, weight: Double)
19+
20+
val pet = (kind = "Dog", age = 7, weight = 6.5)
21+
22+
@main def Test =
23+
val pet2 = buildColumn(pet, "growth", pet => pet.weight / pet.age)
24+
// Test the inferred type:
25+
val _: (kind: String, age: Int, weight: Double, growth: Double) = pet2
26+
// Show the results
27+
println(pet.mkString)
28+
println(pet2.mkString)
29+
30+

0 commit comments

Comments
 (0)