feat: add lazy Sequence.skip and Sequence.take methods with SkipSequence and TakeSequence classes
Implement SkipSequence and TakeSequence as lazy iterator-producing wrappers in the core Sequence class. SkipSequence advances past the first count elements on initial iteration, while TakeSequence tracks taken count via _taken field and returns null when exceeded. Include documentation in sequence.markdown, update wren_core.wren.inc, and add test files for both methods covering edge cases (zero, negative, overflow counts).
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
class TestSequence is Sequence {
|
||||
construct new() {}
|
||||
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return 1
|
||||
if (iterator == 3) return false
|
||||
return iterator + 1
|
||||
}
|
||||
|
||||
iteratorValue(iterator) { iterator }
|
||||
}
|
||||
|
||||
var test = TestSequence.new().skip(0)
|
||||
|
||||
System.print(test is Sequence) // expect: true
|
||||
System.print(test) // expect: instance of SkipSequence
|
||||
|
||||
// Skipping 0 changes nothing
|
||||
System.print(test.toList) // expect: [1, 2, 3]
|
||||
|
||||
// Skipping 1 works
|
||||
System.print(test.skip(1).toList) // expect: [2, 3]
|
||||
|
||||
// Skipping more than length of sequence produces empty list
|
||||
System.print(test.skip(4).isEmpty) // expect: true
|
||||
|
||||
// Skipping less than 0 changes nothing
|
||||
System.print(test.skip(-10).toList) // expect: [1, 2, 3]
|
||||
@@ -0,0 +1,28 @@
|
||||
class TestSequence is Sequence {
|
||||
construct new() {}
|
||||
|
||||
iterate(iterator) {
|
||||
if (iterator == null) return 1
|
||||
if (iterator == 3) return false
|
||||
return iterator + 1
|
||||
}
|
||||
|
||||
iteratorValue(iterator) { iterator }
|
||||
}
|
||||
|
||||
var test = TestSequence.new().take(3)
|
||||
|
||||
System.print(test is Sequence) // expect: true
|
||||
System.print(test) // expect: instance of TakeSequence
|
||||
|
||||
// Taking 0 produces empty list
|
||||
System.print(test.take(0).isEmpty) // expect: true
|
||||
|
||||
// Taking 1 works
|
||||
System.print(test.take(1).toList) // expect: [1]
|
||||
|
||||
// Taking more than length of sequence produces whole sequence
|
||||
System.print(test.take(4).toList) // expect: [1, 2, 3]
|
||||
|
||||
// Taking less than 0 produces empty list
|
||||
System.print(test.take(-10).isEmpty) // expect: true
|
||||
Reference in New Issue
Block a user