-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add immutable ordered finite 'map' and 'set' to the stdlib #561
Open
jiribenes
wants to merge
29
commits into
master
Choose a base branch
from
feature/stdlib/treemap-treeset
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
76080ef
Initial draft of (tree) maps & sets
jiribenes 418dc4f
Add a quick unit test
jiribenes 992e4c4
Use pattern guards in rotateL, rotateR
jiribenes 9236b29
Fix wrong right rotation
jiribenes a1995da
Add a few more regression tests
jiribenes 55f2a50
Update tests once again
jiribenes e94e51f
Add a 'counter' example
jiribenes c2ad2c0
Use '///' for doc comments
jiribenes bc6bf33
Move internal tools into their own namespace
jiribenes 5cb7ae8
Add internal namespace for set, fix missing brace
jiribenes 3265837
Re-add forgotten unit test for treesets
jiribenes dac0e5e
Ignore map&set stdlib tests on Chez and LLVM
jiribenes 678e4d6
Reformat treemap source
jiribenes 0f15df3
Rename speech in tests
jiribenes 7f6559c
Add intersection for treeset and treemap, incl. tests
jiribenes 209bfc8
Plug map&set into 'stream'
jiribenes 6bfdbb3
Fix set/unique test
jiribenes 537419e
Ignore added stream tests on Chez & LLVM
jiribenes 62560d1
Fix expected output in stream/map
jiribenes 47f5e30
Fix expected output in stream/map (again)
jiribenes f534faa
Add a few more useful functions
jiribenes daf398c
Add a few more tests for weird/complex operations
jiribenes cdd3e6b
Add map::filter & a caching test
jiribenes 127c982
Use an explicit comparison function
jiribenes eff01b5
Fix subtle, yet obvious bugs, duh
jiribenes ed9ca11
Attempt to fix tests
jiribenes e488a74
More bug fixing
jiribenes 12f7061
Try running 'map' and 'set' tests on Chez and LLVM
jiribenes fd5b9b3
Change 'internal::prettyPairs' to take show instances
jiribenes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
Get key1: value1 | ||
Get key2: value2 | ||
Get key1: newValue1 | ||
Get key2: value2 | ||
After cleaning: | ||
Get key1: newValue1 | ||
Get key2: Not found |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import map | ||
|
||
record CacheEntry[V](value: V, timestamp: Int) | ||
|
||
effect time(): Int | ||
|
||
def main() = { | ||
var currentTime = 0 | ||
try { | ||
var cache: Map[String, CacheEntry[String]] = map::empty(compareString) | ||
val maxAge = 8 | ||
|
||
def cachePut(key: String, value: String): Unit = | ||
cache = cache.put(key, CacheEntry(value, do time())) | ||
|
||
def cacheGet(key: String): Option[String] = | ||
cache.get(key) match { | ||
case Some(entry) and do time() - entry.timestamp < maxAge => Some(entry.value) | ||
case _ => None() | ||
} | ||
|
||
def cleanExpiredEntries(): Unit = { | ||
val currentTime = do time() | ||
cache = cache.filter { (_, entry) => | ||
currentTime - entry.timestamp < maxAge | ||
} | ||
} | ||
|
||
cachePut("key1", "value1") | ||
cachePut("key2", "value2") | ||
|
||
println("Get key1: " ++ cacheGet("key1").getOrElse { "Not found" }) | ||
println("Get key2: " ++ cacheGet("key2").getOrElse { "Not found" }) | ||
|
||
cachePut("key1", "newValue1") | ||
|
||
println("Get key1: " ++ cacheGet("key1").getOrElse { "Not found" }) | ||
println("Get key2: " ++ cacheGet("key2").getOrElse { "Not found" }) | ||
|
||
cleanExpiredEntries() | ||
|
||
println("After cleaning:") | ||
println("Get key1: " ++ cacheGet("key1").getOrElse { "Not found" }) | ||
println("Get key2: " ++ cacheGet("key2").getOrElse { "Not found" }) | ||
} with time { | ||
currentTime = currentTime + 1 | ||
resume(currentTime) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
ask: 3 | ||
can: 3 | ||
you: 3 | ||
see: 0 | ||
do: 4 | ||
Effekt: 2 | ||
[Effekt → 2, and → 1, ask → 3, but → 1, can → 3, do → 4, fellow → 2, for → 4, language → 2, man → 1, my → 2, not → 2, of → 2, programmers → 2, programs → 1, so → 1, the → 2, together → 1, we → 1, what → 4, will → 1, world → 1, you → 3, your → 2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import map | ||
|
||
def counter(words: List[String]): Map[String, Int] = { | ||
var m: Map[String, Int] = map::empty(compareString); | ||
|
||
list::foreach(words) { word => | ||
m = m.putWithKey(word, 1) { (_, n1, n2) => n1 + n2 } | ||
} | ||
|
||
m | ||
} | ||
|
||
def main() = { | ||
// John F. Kennedy's Inaugural Address, Jan 20, 1961; modified for Effekt | ||
val speech: List[String] = [ | ||
"and", "so", "my", "fellow", "Effekt", "programmers", | ||
"ask", "not", "what", "your", "language", "can", "do", "for", "you", | ||
"ask", "what", "you", "can", "do", "for", "your", "language", | ||
"my", "fellow", "programmers", "of", "the", "world", | ||
"ask", "not", "what", "Effekt", "will", "do", "for", "you", | ||
"but", "what", "together", "we", "can", "do", "for", "the", "programs", "of", "man" | ||
] | ||
|
||
val ctr: Map[String, Int] = counter(speech) | ||
|
||
def test(word: String) = { | ||
val count = ctr.getOrElse(word) { 0 } | ||
println(word ++ ": " ++ count.show) | ||
} | ||
|
||
test("ask") | ||
test("can") | ||
test("you") | ||
test("see") | ||
test("do") | ||
test("Effekt") | ||
|
||
println(map::internal::prettyPairs(ctr.toList) { s => show(s) } { n => show(n) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[0 → Hello, 1 → World, 2 → Woo!] | ||
[-1 → Hullo, 0 → Hello, 1 → World, 2 → Woo!] | ||
[-10 → EY, -1 → Hullo, 0 → Hello, 1 → World, 2 → Woo!] | ||
[0 → Hello, 2 → Woo!, 42 → Whole new world!, 100 → Big, 1000 → Bigger, 10000 → Biggest!] | ||
[-1 → Huh?!, 0 → Hello, 1 → Foo, 2 → Woo!, 42 → Whole new world!, 100 → Big, 1000 → Bigger, 10000 → Biggest!] | ||
[-1 → Huh?!, 0 → Hello, 1 → Foo, 2 → Woo!, 42 → Whole new world!, 100 → Big, 1000 → Bigger, 10000 → Biggest!] | ||
[0 → Hello, 1 → World, 2 → Woo!] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import map | ||
|
||
def main() = { | ||
val l = [(0, "Hello"), (1, "World"), (2, "Woo!")] | ||
|
||
val m = map::fromList(l, compareInt) | ||
println(map::internal::prettyPairs(m.toList) { n => show(n) } { s => show(s) }) | ||
|
||
val m2 = m.put(-1, "Hullo") | ||
println(map::internal::prettyPairs(m2.toList) { n => show(n) } { s => show(s) }) | ||
|
||
val m3 = m2.put(-10, "EY") | ||
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) { 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) { n => show(n) } { s => show(s) }) | ||
|
||
val m6: Map[Int, String] = m5.toList.fromList(compareInt) | ||
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) { n => show(n) } { s => show(s) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Min: 5 -> Five | ||
Max: 30 -> Thirty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import map | ||
|
||
def show(opt: Option[(Int, String)]): String = opt match { | ||
case Some((k, v)) => k.show ++ " -> " ++ v | ||
case None() => "X" | ||
} | ||
|
||
def main() = { | ||
val m = map::fromList([(10, "Ten"), (20, "Twenty"), (5, "Five"), (30, "Thirty")], compareInt) | ||
|
||
val min: Option[(Int, String)] = m.getMin | ||
val max: Option[(Int, String)] = m.getMax | ||
|
||
println("Min: " ++ min.show) | ||
println("Max: " ++ max.show) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Union (keeping first value): | ||
[1 → apple, 2 → banana, 3 → cherry, 4 → elderberry] | ||
|
||
Union (combining values): | ||
[1 → apple, 2 → banana/berry, 3 → cherry/date, 4 → elderberry] | ||
|
||
Intersection (combining keys): | ||
[2 → banana-berry, 3 → cherry-date] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import map | ||
|
||
def main() = { | ||
// Create multiple maps | ||
val map1 = map::fromList([(1, "apple"), (2, "banana"), (3, "cherry")], compareInt) | ||
val map2 = map::fromList([(2, "berry"), (3, "date"), (4, "elderberry")], compareInt) | ||
|
||
// Test union with different combine strategies | ||
println("Union (keeping first value):") | ||
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) { 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) { n => show(n) } { s => show(s) }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
ask: true | ||
can: true | ||
you: true | ||
see: false | ||
do: true | ||
Effekt: true | ||
|
||
Cons(Effekt, Cons(and, Cons(ask, Cons(but, Cons(can, Cons(do, Cons(fellow, Cons(for, Cons(language, Cons(man, Cons(my, Cons(not, Cons(of, Cons(programmers, Cons(programs, Cons(so, Cons(the, Cons(together, Cons(we, Cons(what, Cons(will, Cons(world, Cons(you, Cons(your, Nil())))))))))))))))))))))))) | ||
sorted | ||
Cons(Effekt, Cons(and, Cons(ask, Cons(but, Cons(can, Cons(do, Cons(fellow, Cons(for, Cons(language, Cons(man, Cons(my, Cons(not, Cons(of, Cons(programmers, Cons(programs, Cons(so, Cons(the, Cons(together, Cons(we, Cons(what, Cons(will, Cons(world, Cons(you, Cons(your, Nil())))))))))))))))))))))))) | ||
sorted | ||
|
||
Cons(after, Cons(around, Cons(better, Cons(do, Cons(ever, Cons(faster, Cons(harder, Cons(hour, Cons(is, Cons(it, Cons(make, Cons(makes, Cons(more, Cons(never, Cons(over, Cons(stronger, Cons(than, Cons(the, Cons(us, Cons(work, Cons(world, Nil()))))))))))))))))))))) | ||
sorted | ||
|
||
lyrics / speech: | ||
Cons(after, Cons(around, Cons(better, Cons(ever, Cons(faster, Cons(harder, Cons(hour, Cons(is, Cons(it, Cons(make, Cons(makes, Cons(more, Cons(never, Cons(over, Cons(stronger, Cons(than, Cons(us, Cons(work, Nil())))))))))))))))))) | ||
speech / lyrics: | ||
Cons(Effekt, Cons(and, Cons(ask, Cons(but, Cons(can, Cons(fellow, Cons(for, Cons(language, Cons(man, Cons(my, Cons(not, Cons(of, Cons(programmers, Cons(programs, Cons(so, Cons(together, Cons(we, Cons(what, Cons(will, Cons(you, Cons(your, Nil()))))))))))))))))))))) | ||
speech n lyrics: | ||
Cons(do, Cons(the, Cons(world, Nil()))) | ||
speech u lyrics: | ||
Cons(Effekt, Cons(after, Cons(and, Cons(around, Cons(ask, Cons(better, Cons(but, Cons(can, Cons(do, Cons(ever, Cons(faster, Cons(fellow, Cons(for, Cons(harder, Cons(hour, Cons(is, Cons(it, Cons(language, Cons(make, Cons(makes, Cons(man, Cons(more, Cons(my, Cons(never, Cons(not, Cons(of, Cons(over, Cons(programmers, Cons(programs, Cons(so, Cons(stronger, Cons(than, Cons(the, Cons(together, Cons(us, Cons(we, Cons(what, Cons(will, Cons(work, Cons(world, Cons(you, Cons(your, Nil())))))))))))))))))))))))))))))))))))))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import set | ||
|
||
def unique(words: List[String]): Set[String] = { | ||
var s: Set[String] = set::empty(compareString); | ||
|
||
list::foreach(words) { word => | ||
s = s.insert(word) | ||
} | ||
|
||
s | ||
} | ||
|
||
def main() = { | ||
// John F. Kennedy's Inaugural Address Jan 20 1961; modified for Effekt | ||
val speech: List[String] = [ | ||
"and", "so", "my", "fellow", "Effekt", "programmers", | ||
"ask", "not", "what", "your", "language", "can", "do", "for", "you", | ||
"ask", "what", "you", "can", "do", "for", "your", "language", | ||
"my", "fellow", "programmers", "of", "the", "world", | ||
"ask", "not", "what", "Effekt", "will", "do", "for", "you", | ||
"but", "what", "together", "we", "can", "do", "for", "the", "programs", "of", "man" | ||
] | ||
|
||
val uniqueSpeech: Set[String] = unique(speech) | ||
|
||
def test(word: String) = { | ||
val present = uniqueSpeech.contains(word) | ||
println(word ++ ": " ++ present.show) | ||
} | ||
|
||
test("ask") | ||
test("can") | ||
test("you") | ||
test("see") | ||
test("do") | ||
test("Effekt") | ||
|
||
// --- | ||
println("") | ||
|
||
def testSorted(s: Set[String]) = { | ||
val sorted = s.toList.isSortedBy { (x, y) => x <= y } | ||
if (sorted) { | ||
println("sorted") | ||
} else { | ||
println("unsorted") | ||
} | ||
} | ||
|
||
println(uniqueSpeech.toList) | ||
testSorted(uniqueSpeech) | ||
|
||
println(set::fromList(speech, compareString).toList) | ||
testSorted(set::fromList(speech, compareString)) | ||
|
||
// --- | ||
println("") | ||
|
||
// Around the World / Harder, Better, Faster, Stronger by Daft Punk (Alive 2007) | ||
val lyrics: List[String] = [ | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
|
||
"work", "it", "make", "it", "do", "it", "makes", "us", | ||
"harder", "better", "faster", "stronger", | ||
"more", "than", "hour", "hour", "never", | ||
"ever", "after", "work", "is", "over", | ||
|
||
"work", "it", "make", "it", "do", "it", "makes", "us", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world", | ||
|
||
"harder", "better", "faster", "stronger", | ||
"around", "the", "world", "around", "the", "world", | ||
"around", "the", "world", "around", "the", "world" | ||
] | ||
|
||
val uniqueLyrics = unique(lyrics) | ||
|
||
println(uniqueLyrics.toList) | ||
testSorted(uniqueLyrics) | ||
|
||
// --- | ||
println("") | ||
|
||
println("lyrics / speech:") | ||
println(uniqueLyrics.difference(uniqueSpeech).toList) | ||
|
||
println("speech / lyrics:") | ||
println(uniqueSpeech.difference(uniqueLyrics).toList) | ||
|
||
println("speech n lyrics:") | ||
println(uniqueLyrics.intersection(uniqueSpeech).toList) | ||
|
||
println("speech u lyrics:") | ||
println(uniqueLyrics.union(uniqueSpeech).toList) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
1: H (72) | ||
2: e (101) | ||
3: l (108) | ||
4: l (108) | ||
5: o (111) | ||
[1 → 72, 2 → 101, 3 → 108, 4 → 108, 5 → 111] | ||
Hello |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import stream | ||
import map | ||
|
||
def main() = { | ||
val m = map::fromList([(1, 'H'), (2, 'e'), (3, 'l'), (4, 'l'), (5, 'o')], compareInt) | ||
|
||
for[(Int, Char)] { each(m) } { case (k, v) => | ||
println(show(k) ++ ": " ++ show(v) ++ " (" ++ show(v.toInt) ++ ")") | ||
} | ||
|
||
val newMap = collectMap[Int, Char](compareInt) { each(m) } | ||
println(map::internal::prettyPairs(newMap.toList) { n => show(n) } { c => show(c) }) | ||
|
||
val hello: String = collectString { eachValue(m) } | ||
println(hello) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work on LLVM, hence #748. Moreover, it's probably worth it to have a few of these
compare
functions prepared for more types than justInt
s andString
s, but that's future work :)