Creating ranges from findall

inds = findall(isodd, searching_specific)
ranges = @views (:).(inds[1:end-1], inds[2:end] .- 1)

This allocates, but it seems not to be a problem since you are using findall. If you want an allocation-free alternative, you may create a new iterable type like this:

struct FindRanges{F, T}
	f::F
	t::T
end
function Base.iterate(x::FindRanges, i1 = findfirst(x.f, x.t))
	i2 = findnext(x.f, x.t, nextind(x.t, i1))
	if i2 === nothing
		nothing
	else
		i1 : prevind(x.t, i2), i2
	end
end

ranges2 = FindRanges(isodd, searching_specific)

ranges2 is a iterable that yields the same as ranges, but without allocations.

1 Like