feat: add MapEntry class and make Map a Sequence for direct iteration

Add MapEntry class to expose key-value pairs during map iteration, and make Map extend Sequence so maps can be directly iterated with `for` loops. Rename the internal `iterate_` primitive to `iterate` and update error messages for invalid map iterators.
This commit is contained in:
Bob Nystrom
2016-07-06 14:17:07 +00:00
parent a05f50ee24
commit 4d1215697e
13 changed files with 129 additions and 9 deletions
+2
View File
@@ -0,0 +1,2 @@
System.print({}.isEmpty) // expect: true
System.print({1: 1}.isEmpty) // expect: false
+45
View File
@@ -0,0 +1,45 @@
var a = {"one": 1, "two": 2, "three": 3, "four": 4}
// The precise numeric values aren't defined since they are indexes into the
// entry table and the hashing process isn't specified. So we just validate
// what we can assume about them.
System.print(a.iterate(null) is Num) // expect: true
System.print(a.iterate(null) >= 0) // expect: true
System.print(a.iterate(0) is Num) // expect: true
System.print(a.iterate(0) > 0) // expect: true
System.print(a.iterate(1) is Num) // expect: true
System.print(a.iterate(1) > 0) // expect: true
System.print(a.iterate(2) is Num) // expect: true
System.print(a.iterate(2) > 0) // expect: true
System.print(a.iterate(3) is Num) // expect: true
System.print(a.iterate(3) > 0) // expect: true
var previous = -1
var iterator = a.iterate(null)
while (iterator) {
System.print(iterator > previous)
System.print(iterator is Num)
previous = iterator
iterator = a.iterate(iterator)
}
// First entry:
// expect: true
// expect: true
// Second entry:
// expect: true
// expect: true
// Third entry:
// expect: true
// expect: true
// Fourth entry:
// expect: true
// expect: true
// Out of bounds.
System.print(a.iterate(16)) // expect: false
System.print(a.iterate(-1)) // expect: false
// Nothing to iterate in an empty map.
System.print({}.iterate(null)) // expect: false
@@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.iterate(1.5) // expect runtime error: Iterator must be an integer.
@@ -0,0 +1,2 @@
var a = {1: 2, 3: 4}
a.iterate("2") // expect runtime error: Iterator must be a number.
+13
View File
@@ -0,0 +1,13 @@
var a = {1: "one"}
// The actual iterator values are implementation specific, so ask the map.
var iterator = a.iterate(null)
var value = a.iteratorValue(iterator)
System.print(value is MapEntry) // expect: true
System.print(value.key) // expect: 1
System.print(value.value) // expect: one
// The entry does not track the underlying map.
a[1] = "updated"
System.print(value.value) // expect: one
@@ -0,0 +1,2 @@
var a = {1: "one"}
a.iteratorValue(1.5) // expect runtime error: Iterator must be an integer.
@@ -0,0 +1,2 @@
var a = {1: "one"}
a.iteratorValue("2") // expect runtime error: Iterator must be a number.
@@ -0,0 +1,6 @@
var a = {1: "one"}
// The maximum value is based on the map's capacity, not its count, so use a
// sufficiently large enough value for the test to make not affected by growth
// strategy.
a.iteratorValue(9999) // expect runtime error: Iterator out of bounds.
@@ -0,0 +1,6 @@
var a = {1: "one"}
// The maximum value is based on the map's capacity, not its count, so use a
// sufficiently large enough value for the test to make not affected by growth
// strategy.
a.iteratorValue(-9999) // expect runtime error: Iterator out of bounds.