Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add methods of first and last accepting a predicate
On zulip, there was a thread about the fact that there's not a good way of finding the first item matching a predicate in a non-indexable iterable without manually writing out the for-loop. One possibility is using the existing first function composed with Iterators.filter: first(Iterators.filter(>(5), 1:10)) The problem with this approach is that it requires wrapping in a try block if the predicate not being satisfied is a possibility (Iterators.filter returns an empty collection). There should be a way of expressing this without using exception-handling as control flow or writing the for-loop manually. Ideally, I think first and last should just return Union{Some{T},Nothing} always, but that would be a breaking change. So I see two obvious alternatives: 1. This commit adds an optional predicate argument to first and last which is mapped over the collection and the first/last value satisfying predicate is returned. 2. Add a Iterators.first/Iterators.last which are identical to the versions in Base except that when successful they return Some{T} and when unsuccesful return Nothing
- Loading branch information