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:
@@ -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
|
||||
Reference in New Issue
Block a user