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
+8
View File
@@ -1,4 +1,12 @@
class Sequence {
count {
var result = 0
for (element in this) {
result = result + 1
}
return result
}
map(f) {
var result = new List
for (element in this) {