# BreakAt iterator suggestion

**URL:** https://discourse.julialang.org/t/breakat-iterator-suggestion/6771
**Category:** Internals & Design
**Tags:** question, proposal
**Created:** [October 30, 2017, 1:06pm UTC](https://discourse.julialang.org/t/breakat-iterator-suggestion/6771 "2017-10-30T13:06:43Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)
#### Post date: [October 30, 2017, 1:06pm UTC](https://discourse.julialang.org/t/breakat-iterator-suggestion/6771/1 "2017-10-30T13:06:43Z")

</div>

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](https://gist.github.com/getzdan/fb4e91e19b0ee7cfa07e3387f1857651) code.

Usage example:

```julia
include("breakat.jl")
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 ?
