Skip to content

Commit

Permalink
Change 'internal::prettyPairs' to take show instances
Browse files Browse the repository at this point in the history
  • Loading branch information
jiribenes committed Dec 13, 2024
1 parent 12f7061 commit fd5b9b3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/stdlib/map/counter.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ def main() = {
test("do")
test("Effekt")

println(map::internal::prettyPairs(ctr.toList))
println(map::internal::prettyPairs(ctr.toList) { s => show(s) } { n => show(n) })
}
14 changes: 7 additions & 7 deletions examples/stdlib/map/map.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ def main() = {
val l = [(0, "Hello"), (1, "World"), (2, "Woo!")]

val m = map::fromList(l, compareInt)
println(map::internal::prettyPairs(m.toList))
println(map::internal::prettyPairs(m.toList) { n => show(n) } { s => show(s) })

val m2 = m.put(-1, "Hullo")
println(map::internal::prettyPairs(m2.toList))
println(map::internal::prettyPairs(m2.toList) { n => show(n) } { s => show(s) })

val m3 = m2.put(-10, "EY")
println(map::internal::prettyPairs(m3.toList))
println(map::internal::prettyPairs(m3.toList) { n => show(n) } { s => show(s) })

// ...

val m4 = m.delete(1).put(42, "Whole new world!").put(100, "Big").put(1000, "Bigger").put(10000, "Biggest!")
println(map::internal::prettyPairs(m4.toList))
println(map::internal::prettyPairs(m4.toList) { n => show(n) } { s => show(s) })

val m5 = map::fromList(Cons((1, "Foo"), Cons((-1, "Huh?!"), m4.toList.reverse)), compareInt)
println(map::internal::prettyPairs(m5.toList))
println(map::internal::prettyPairs(m5.toList) { n => show(n) } { s => show(s) })

val m6: Map[Int, String] = m5.toList.fromList(compareInt)
println(map::internal::prettyPairs(m6.toList))
println(map::internal::prettyPairs(m6.toList) { n => show(n) } { s => show(s) })

val nuMap = map::fromList(l.reverse, compareInt)
println(map::internal::prettyPairs(nuMap.toList))
println(map::internal::prettyPairs(nuMap.toList) { n => show(n) } { s => show(s) })
}
6 changes: 3 additions & 3 deletions examples/stdlib/map/setops.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ def main() = {

// Test union with different combine strategies
println("Union (keeping first value):")
println(map::internal::prettyPairs(map1.union(map2).toList))
println(map::internal::prettyPairs(map1.union(map2).toList) { n => show(n) } { s => show(s) })

println("\nUnion (combining values):")
val combinedMap = map1.union(map2, compareInt) { (k, v1, v2) => v1 ++ "/" ++ v2 }
println(map::internal::prettyPairs(combinedMap.toList))
println(map::internal::prettyPairs(combinedMap.toList) { n => show(n) } { s => show(s) })

// Test intersection
println("\nIntersection (combining keys):")
val intersectedMap = map1.intersection(map2, compareInt) { (k, v1, v2) => v1 ++ "-" ++ v2 }
println(map::internal::prettyPairs(intersectedMap.toList))
println(map::internal::prettyPairs(intersectedMap.toList) { n => show(n) } { s => show(s) })
}
2 changes: 1 addition & 1 deletion examples/stdlib/stream/map.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def main() = {
}

val newMap = collectMap[Int, Char](compareInt) { each(m) }
println(map::internal::prettyPairs(newMap.toList))
println(map::internal::prettyPairs(newMap.toList) { n => show(n) } { c => show(c) })

val hello: String = collectString { eachValue(m) }
println(hello)
Expand Down
5 changes: 3 additions & 2 deletions libraries/common/map.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -960,6 +960,7 @@ namespace internal {

// Section: prettyprinting for trees:

/// Works only on the backends that support `genericShow` (JavaScript)!
def prettyTree[K, V](m: Tree[K, V]): String = {
// Helper function to recursively build the string representation of the tree
def go(t: Tree[K, V], prefix: String, isTail: Bool): String = {
Expand All @@ -981,9 +982,9 @@ namespace internal {
go(m, "", true)
}

def prettyPairs[K, V](list: List[(K, V)]): String = {
def prettyPairs[K, V](list: List[(K, V)]) { showLeft: K => String } { showRight: V => String }: String = {
val res: String =
list.map { case (k, v) => k.genericShow ++ " → " ++ v.genericShow }
list.map { case (k, v) => showLeft(k) ++ " → " ++ showRight(v) }
.join(", ")

"[" ++ res ++ "]"
Expand Down

0 comments on commit fd5b9b3

Please sign in to comment.