feat: replace eager map/where with lazy MapSequence and WhereSequence classes
Refactor Sequence.map and Sequence.where to return lazy MapSequence and WhereSequence wrappers instead of eagerly building a List. Add new MapSequence and WhereSequence classes that delegate iteration and apply the transformation or predicate on-the-fly. Update documentation to reflect lazy semantics and the need to call .list to materialize results. Adjust all existing tests to call .list on map/where results. Add new tests in test/core/sequence/ verifying lazy behavior with infinite Fibonacci iterators.
This commit is contained in:
@@ -81,14 +81,13 @@ Creates a [list](list.html) containing all the elements in the sequence.
|
||||
|
||||
### **map**(transformation)
|
||||
|
||||
Creates a new list by applying `transformation` to each element in the
|
||||
sequence.
|
||||
Creates a new sequence that applies the `transformation` to each element in the
|
||||
original sequence while it is iterated.
|
||||
|
||||
Iterates over the sequence, passing each element to the function
|
||||
`transformation`. Generates a new list from the result of each of those calls.
|
||||
The `list` method can be used to turn the resulting sequence into a list.
|
||||
|
||||
:::dart
|
||||
[1, 2, 3].map {|n| n * 2} // [2, 4, 6].
|
||||
[1, 2, 3].map {|n| n * 2}.list // [2, 4, 6].
|
||||
|
||||
### **reduce**(function)
|
||||
|
||||
@@ -102,10 +101,13 @@ Similar to above, but uses `seed` for the initial value of the accumulator. If t
|
||||
|
||||
### **where**(predicate)
|
||||
|
||||
Produces a new list containing only the elements in the sequence that pass the
|
||||
`predicate`.
|
||||
Creates a new sequence containing only the elements from the original sequence
|
||||
that pass the `predicate`.
|
||||
|
||||
Iterates over the sequence, passing each element to the function `predicate`.
|
||||
If it returns `true`, adds the element to the result list.
|
||||
During iteration, each element in the original sequence is passed to the
|
||||
function `predicate`. If it returns `false`, the element is skipped.
|
||||
|
||||
(1..10).where {|n| n % 2 == 1} // [1, 3, 5, 7, 9].
|
||||
The `list` method can be used to turn the resulting sequence into a list.
|
||||
|
||||
:::dart
|
||||
(1..10).where {|n| n % 2 == 1}.list // [1, 3, 5, 7, 9].
|
||||
|
||||
Reference in New Issue
Block a user