Skip to content

Commit

Permalink
Make __foldl__ for vov type-stable
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Dec 31, 2018
1 parent de81777 commit 5504bcf
Showing 1 changed file with 46 additions and 1 deletion.
47 changes: 46 additions & 1 deletion examples/transducible_processes.jl
Expand Up @@ -77,4 +77,49 @@ end

Base.length(vov::VecOfVec) = sum(length, vov.vectors)

collect(vov)
@show collect(vov)

xf = PartitionBy(isequal(1)) |> Cat()
@assert mapfoldl(xf, +, vov, init=1) == 11

using Test

let err
try
@inferred mapfoldl(xf, +, vov, init=1)
catch err
end

print("Got: ")
showerror(stdout, err)
println()
end

#-

@inline function Transducers.__foldl__(rf, val, vov::VecOfVec, complete)
if isempty(vov.vectors) || all(isempty, vov.vectors)
return complete(rf, val)
end

n = findfirst(!isempty, vov.vectors)
v1 = @inbounds vov.vectors[n]
x = @inbounds v1[1]
val = next(rf, val, x)
@return_if_reduced complete(rf, val)

for i in 2:length(v1)
val = next(rf, val, @inbounds v1[i])
@return_if_reduced complete(rf, val)
end

for vector in @inbounds @view vov.vectors[n + 1:end]
for x in vector
val = next(rf, val, x)
@return_if_reduced complete(rf, val)
end
end
return complete(rf, val)
end

@assert (@inferred mapfoldl(xf, +, vov, init=1)) == 11

0 comments on commit 5504bcf

Please sign in to comment.