feat: add count method to Sequence class with iteration-based element tally

Implement a `count` method on the `Sequence` class that iterates over all elements and returns the total number. The method uses a simple for-loop to increment a counter for each element, providing a generic way to determine sequence length without requiring a separate length property. Includes a test sequence that yields values 1 through 10 to verify the count returns 10.
This commit is contained in:
Bob Nystrom
2015-03-06 15:01:02 +00:00
parent 69f6f2e71c
commit b712bb1929
3 changed files with 27 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
class TestSequence is Sequence {
iterate(iterator) {
if (iterator == null) return 1
if (iterator == 10) return false
return iterator + 1
}
iteratorValue(iterator) { iterator }
}
IO.print((new TestSequence).count) // expect: 10