BreakAt iterator suggestion



While trying to write a quick function to address a question in StackOverflow, an iterator decorator which takes another iterator and returns its values until some condition is met felt like a useful thing to have. So, here is a coded up version, and a little example use.

breakat.jl code.

Usage example:

using IterTools
using Base.Test

# `inds(s)` returns a vector of indices of valid characters in the UTF8 encoded string `s`
inds(s) = breakat(x->x>sizeof(s),iterate(x->nextind(s,x),1))
# `ranges(s)` returns a vector of ranges of indices spanned by each codepoint in UTF8 encoded string `s`
ranges(s) = breakat(x->first(x)>endof(s), iterate(x->last(x)+1:nextind(s,last(x)+1)-1,1:nextind(s,1)-1))

s = "ďaľšý"
@test collect(inds(s)) == [1,3,4,6,8]
@test collect(ranges(s)) == [1:2,3:3,4:5,6:7,8:9]

using Primes
# find all composite numbers from following the prime 1009 (1013 turns out to be the next one)
@test collect(breakat(isprime, Iterators.countfrom(1010))) == [1010,1011,1012]

Should something like this be PRed into Base.Iterators or IterTools ?