Skip to content

Commit

Permalink
Move internal tools into their own namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
jiribenes committed Oct 2, 2024
1 parent 7eb4aa1 commit ef7d4a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 42 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(ctr.toList.prettyPairs)
println(map::internal::prettyPairs(ctr.toList))
}
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)
println(m.toList.prettyPairs)
println(map::internal::prettyPairs(m.toList))

val m2 = m.put(-1, "Hullo")
println(m2.toList.prettyPairs)
println(map::internal::prettyPairs(m2.toList))

val m3 = m2.put(-10, "EY")
println(m3.toList.prettyPairs)
println(map::internal::prettyPairs(m3.toList))

// ...

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

val m5 = map::fromList(Cons((1, "Foo"), Cons((-1, "Huh?!"), m4.toList.reverse)))
println(m5.toList.prettyPairs)
println(map::internal::prettyPairs(m5.toList))

val m6: Map[Int, String] = m5.toList.fromList
println(m6.toList.prettyPairs)
println(map::internal::prettyPairs(m6.toList))

val nuMap = map::fromList(l.reverse)
println(nuMap.toList.prettyPairs)
println(map::internal::prettyPairs(nuMap.toList))
}
70 changes: 36 additions & 34 deletions libraries/common/map.effekt
Original file line number Diff line number Diff line change
Expand Up @@ -668,47 +668,49 @@ def putMax[K, V](m: Map[K, V], k: K, v: V): Map[K, V] = {
}
}

// Section: internal utilities for tests:
/// Section: internal utilities
namespace internal {
// Section: for tests and invariants:

/// Check if a map `m` is balanced.
def isBalanced[K, V](m: Map[K, V]): Bool = {
m match {
case Tip() => true
case Bin(_, _, _, l, r) =>
val bothSmall = l.size() + r.size() <= 1
val leftSmallEnough = l.size() <= delta * r.size()
val rightSmallEnough = r.size() <= delta * l.size()
(bothSmall || (leftSmallEnough && rightSmallEnough)) && isBalanced(l) && isBalanced(r)
/// Check if a map `m` is balanced.
def isBalanced[K, V](m: Map[K, V]): Bool = {
m match {
case Tip() => true
case Bin(_, _, _, l, r) =>
val bothSmall = l.size() + r.size() <= 1
val leftSmallEnough = l.size() <= delta * r.size()
val rightSmallEnough = r.size() <= delta * l.size()
(bothSmall || (leftSmallEnough && rightSmallEnough)) && isBalanced(l) && isBalanced(r)
}
}
}

// Section: prettyprinting for tree maps and list maps
// Section: prettyprinting for tree maps and list maps:

def prettyMap[K, V](m: Map[K, V]): String = {
// Helper function to recursively build the string representation of the tree
def go(t: Map[K, V], prefix: String, isTail: Bool): String = {
t match {
case Tip() => ""
case Bin(_, k, v, l, r) =>
val pair = k.genericShow ++ " → " ++ v.genericShow
val currentLine = prefix ++ (if (isTail) "└── " else "├── ") ++ pair ++ "\n"
def prettyMap[K, V](m: Map[K, V]): String = {
// Helper function to recursively build the string representation of the tree
def go(t: Map[K, V], prefix: String, isTail: Bool): String = {
t match {
case Tip() => ""
case Bin(_, k, v, l, r) =>
val pair = k.genericShow ++ " → " ++ v.genericShow
val currentLine = prefix ++ (if (isTail) "└── " else "├── ") ++ pair ++ "\n"

val newPrefix = prefix ++ (if (isTail) " " else "│ ")
val leftStr = go(l, newPrefix, false)
val rightStr = go(r, newPrefix, true)
val newPrefix = prefix ++ (if (isTail) " " else "│ ")
val leftStr = go(l, newPrefix, false)
val rightStr = go(r, newPrefix, true)

currentLine ++ leftStr ++ rightStr
currentLine ++ leftStr ++ rightStr
}
}
}

// Start the recursion with the initial map, an empty prefix, and true for the root being the tail
go(m, "", true)
}
// Start the recursion with the initial map, an empty prefix, and true for the root being the tail
go(m, "", true)
}

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

"[" ++ res ++ "]"
}
"[" ++ res ++ "]"
}

0 comments on commit ef7d4a2

Please sign in to comment.