# Mapfoldr and inference on Tuple

**URL:** <https://discourse.julialang.org/t/mapfoldr-and-inference-on-tuple/28657>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 11, 2019, 5:42pm UTC](https://discourse.julialang.org/t/mapfoldr-and-inference-on-tuple/28657 "2019-09-11T17:42:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 11, 2019, 5:42pm UTC](https://discourse.julialang.org/t/mapfoldr-and-inference-on-tuple/28657/1 "2019-09-11T17:42:44Z")

</div>

I would like `mapfoldr` to infer on a `Tuple`. MWE:

```julia
using Test
prefix_combinations(x, y) = vec(((x, y) -> (x, y...)).(x, permutedims(y)))
@inferred prefix_combinations([1, 2], [(3, ), (4, )]) # OK
f1(D) = mapfoldr(Base.OneTo, prefix_combinations, D; init = [()])
@inferred f1((2, 3)) # FAILS

```

I can of course just write a recursive version that works:

```julia
mapfoldr_recursive(acc, f, op) = acc

function mapfoldr_recursive(acc, f, op, first, rest...)
    mapfoldr_recursive(op(f(first), acc), f, op, rest...)
end

f2(D) = mapfoldr_recursive([()], Base.OneTo, prefix_combinations, reverse(D)...)

@inferred f2((2, 3)) # OK

```

but I am wondering

1. if there is a way to do this with `mapfoldr` somehow out of the box, or in a simpler way,

2. if not, whether contributing this to `Base` would make sense as a special case, or if there is a package that does this already.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 11, 2019, 9:57pm UTC](https://discourse.julialang.org/t/mapfoldr-and-inference-on-tuple/28657/2 "2019-09-11T21:57:18Z")

</div>

> [@Tamas\_Papp](#):
>
> if not, whether contributing this to `Base` would make sense as a special case

It looks like your example can be inferred with just

```julia
Iterators.reverse(xs::Tuple) = Base.reverse(xs)

```

I guess it makes sense because there are already other special cases:

> <https://github.com/JuliaLang/julia/blob/9a8b2fd72b675bb8a5bf0322943ee9451787b86a/base/iterators.jl#L96-L100>

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [September 12, 2019, 8:45am UTC](https://discourse.julialang.org/t/mapfoldr-and-inference-on-tuple/28657/3 "2019-09-12T08:45:58Z")

</div>

Thanks, made a PR:  
[https://github.com/JuliaLang/julia/pull/33235](https://github.com/JuliaLang/julia/pull/33235)
