# Exponentiation operator for iterated functions?

**URL:** <https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346>\
**Category:** Internals & Design\
**Tags:** proposal, syntax\
**Created:** [March 22, 2020, 2:19pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346 "2020-03-22T14:19:01Z")\
**Posts on this page:** 1\
**Showing post:** 9

<div class="post-metadata">

**Author:** ![Gyoshi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gyoshi/32/7119_2.png) [@Gyoshi](https://discourse.julialang.org/u/Gyoshi)\
**Post date:** [April 6, 2020, 12:59am UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/9 "2020-04-06T00:59:57Z")

</div>

What about something like this?

```julia
function Base.:^(x::Any, exponent::Tuple{Function,Integer})
    op, p = exponent
    p == 1 ? x : reduce(op, fill(x, p))
end

```

```julia
julia> typeof(1.0) |> supertype^(∘,3)
Number

julia> 3^(+,3) # Iterated addition -> Scalar multiplication
9

julia> 3^(^, 3) # Tetration 😄
19683

```

Implementation and syntax is obviously up for debate: For example, closure could work too e.g. `3 ^(+) 3`, but I couldn’t figure out how to do it; I don’t know how to return an anonymous function that behaves like an operator.

I feel like a more general exponentiation operator has several benefits:

- Convenient and intuitive iteration of any closed binary operator (e.g. the main post) without having to implement a new `^` method for custom types
- When you have more than one product-like operation defined, you can eliminate ambiguity as to which operation your exponentiation is referring to.
- If you want to be more strict, instead of overloading `^` for custom types to mean iteration of some non-`*` operator, you can instead specify exactly what you mean without much loss in convenience.
- Code golf 😛

As for how useful it would be in practice…I’m not the person to ask.

* * *

My dream is for regular exponentiation `x^p` to just be a special case via

```julia
(^)(x::Any, p::Integer) = x^(*, p)

```

👻⠀

See related [this thread I made](https://discourse.julialang.org/t/built-in-exponentiation-for-any-type/37083).

---

_[View the full topic](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346)._
