- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.7k
          Implement accumulate and friends for Tuple
          #34654
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| function accumulate(op, xs::Tuple; init = _InitialValue()) | ||
| rf = BottomRF(op) | ||
| ys, = foldl(xs; init = ((), init)) do (ys, acc), x | ||
| acc = rf(acc, x) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| (ys..., acc), acc | ||
| end | ||
| return ys | ||
| end | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this implementation lead to unrolled code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It const-folds, as shown in the OP. It is also unrolled when the tuple is not constant:
julia> @code_typed optimize=true cumsum(ntuple(identity, 10))
CodeInfo(
1 ── %1  = (getfield)(itr, 1)::Int64
│    %2  = (getfield)(itr, 2)::Int64
│    %3  = (getfield)(itr, 3)::Int64
│    %4  = (getfield)(itr, 4)::Int64
│    %5  = (getfield)(itr, 5)::Int64
│    %6  = (getfield)(itr, 6)::Int64
│    %7  = (getfield)(itr, 7)::Int64
│    %8  = (getfield)(itr, 8)::Int64
│    %9  = (getfield)(itr, 9)::Int64
│    %10 = (getfield)(itr, 10)::Int64
│    %11 = Base.add_int(%1, %2)::Int64
│    %12 = Base.add_int(%11, %3)::Int64
│    %13 = Base.add_int(%12, %4)::Int64
│    %14 = Base.add_int(%13, %5)::Int64
│    %15 = Base.add_int(%14, %6)::Int64
│    %16 = Base.add_int(%15, %7)::Int64
│    %17 = Base.add_int(%16, %8)::Int64
│    %18 = Base.add_int(%17, %9)::Int64
│    %19 = Base.add_int(%18, %10)::Int64
│    %20 = Core.tuple(%1, %11, %12, %13, %14, %15, %16, %17, %18, %19)::NTuple{10,Int64}
└───       goto #3
2 ──       $(Expr(:meta, :nkw, 1))
3 ┄─       goto #4
4 ──       goto #6
5 ──       $(Expr(:meta, :nkw, 1))
6 ┄─       goto #7
7 ──       goto #9
8 ──       $(Expr(:meta, :nkw, 1))
9 ┄─       goto #10
10 ─       return %20
) => NTuple{10,Int64}| Bump. Can this be merged? | 
close #26690
Demo: