Skip to content

Commit

Permalink
add warning to Iterators.filter about assumptions on predicate (#50497
Browse files Browse the repository at this point in the history
)

addresses #50440
  • Loading branch information
adienes authored Aug 7, 2023
1 parent 9f9e989 commit 8b5e3e9
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,15 @@ invocation of `filter`. Calls to `flt` will be made when iterating over the
returned iterable object. These calls are not cached and repeated calls will be
made when reiterating.
!!! warning
Subsequent *lazy* transformations on the iterator returned from `filter`, such
as those performed by `Iterators.reverse` or `cycle`, will also delay calls to `flt`
until collecting or iterating over the returned iterable object. If the filter
predicate is nondeterministic or its return values depend on the order of iteration
over the elements of `itr`, composition with lazy transformations may result in
surprising behavior. If this is undesirable, either ensure that `flt` is a pure
function or collect intermediate `filter` iterators before further transformations.
See [`Base.filter`](@ref) for an eager implementation of filtering for arrays.
# Examples
Expand Down

0 comments on commit 8b5e3e9

Please sign in to comment.