KT
Size: a a a
KT
АО
АО
АО
АО
KT
АО
function slice_when(f, a::Vector{T}) where {T <: AbstractFloat}
a1 = [-Inf; a; Inf]
idx = f.(a1[1:end - 1], a1[2:end])
b = (collect(1:length(a) + 1))[idx]
map(x -> a1[x[1] + 1:x[2]], zip(b[1:end-1], b[2:end]))
end
a = [100, 38, 1, 1.5, 2, 1, 6, 34, 35, 34.5, 10, 23, 11, 11.5, 12, 11.8, 11.4]
slice_when(a) do x, y
abs(x - y) > 2.0
end |> x -> filter(i -> length(i) > 1, x)АО
АО
struct SliceIter{T, F}
a::Vector{T}
f::F
end
slices(a, f) = SliceIter(a, f)
function Base.iterate(iter::SliceIter, state = 1)
state > length(iter.a) && nothing
while true
state1 = state
while state1 < length(iter.a)
state1 += 1
if iter.f(iter.a[state1 - 1], iter.a[state1])
if state1 - state > 1
return (iter.a[state:state1-1], state1)
else
state = state1
end
end
end
state1 - state > 1 ? (return (iter.a[state:state1 - 1], state1)) : return nothing
end
end
for arr in slices(a, (x, y) -> abs(x - y) > 2.0)
println(arr) # do whatever you like
endАО
RS
struct SliceIter{T, F}
a::Vector{T}
f::F
end
slices(a, f) = SliceIter(a, f)
function Base.iterate(iter::SliceIter, state = 1)
state > length(iter.a) && nothing
while true
state1 = state
while state1 < length(iter.a)
state1 += 1
if iter.f(iter.a[state1 - 1], iter.a[state1])
if state1 - state > 1
return (iter.a[state:state1-1], state1)
else
state = state1
end
end
end
state1 - state > 1 ? (return (iter.a[state:state1 - 1], state1)) : return nothing
end
end
for arr in slices(a, (x, y) -> abs(x - y) > 2.0)
println(arr) # do whatever you like
endslices(a, f) = SliceIter(a, f)
АО
АО
АО
KT
АО
KT