# 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:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 22, 2020, 2:19pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/1 "2020-03-22T14:19:01Z")

</div>

Would it make sense to define the `^` operator for [iterated functions](https://en.wikipedia.org/wiki/Iterated_function)? Something like:

```
^(f::Function, n::Integer) = ∘(fill(f,n)...)

```

Then we can do things like

```
typeof(1.0) |> supertype^3

```

Instead of

```
typeof(1.0) |> supertype |> supertype |> supertype

```

A possible downside: people might expect `(sin^2)(x)` to mean `sin(x)^2` instead of `sin(sin(x))` (although they should not IMO 😉).

---

<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: [March 22, 2020, 2:41pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/2 "2020-03-22T14:41:04Z")

</div>

I think not. Whatever works for functions should work for all callables, and some types can be callables and at the same time define methods for `^` (eg as an operator on some algebra).

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 22, 2020, 3:54pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/3 "2020-03-22T15:54:18Z")

</div>

The basic issue is that `^` in Julia is for iteration of `*`. So it would be inconsistent (and poorly composable as @Tamas_Papp notes) to overload it for iteration of `∘`.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 22, 2020, 8:05pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/4 "2020-03-22T20:05:14Z")

</div>

Ah right it makes sense. Maybe at some point another operator will prove adequate…

---

<div class="post-metadata">

### Author: ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)
#### Post date: [March 22, 2020, 8:30pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/5 "2020-03-22T20:30:11Z")

</div>

In case you’re not aware, you _can_ do it for your own functions if you want!

```julia
julia> supertype(T) = Base.supertype(T)
supertype (generic function with 1 method)

julia> Base.:^(::typeof(supertype), n::Integer) = n == 1 ? supertype : supertype ∘ supertype^(n-1)

julia> (supertype^3)(Float64)
Number

```

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 22, 2020, 9:24pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/6 "2020-03-22T21:24:14Z")

</div>

Thanks, my original post actually included a working implementation, but it’s nice to see another one using recursion. It makes me realize that my version only works for n\>=2.

---

<div class="post-metadata">

### Author: ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)
#### Post date: [March 22, 2020, 9:55pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/7 "2020-03-22T21:55:49Z")

</div>

The difference is that your original version commits type piracy if it’s not in `Base` because it applies to all `Function`. That’s (one of) the reason(s) why you were suggesting its inclusion, I thought.

Mine applies only to the function that I own. That’s also the reason why I created a new function (with the same name) in the first place.

Does that make sense? I should have made the distinction clearer.

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [March 23, 2020, 3:41pm UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/8 "2020-03-23T15:41:51Z")

</div>

Ah sorry I read your message too fast and missed the point, it makes perfect sense.

---

<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).

---

<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: [April 6, 2020, 5:41am UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/10 "2020-04-06T05:41:26Z")

</div>

> [@Gyoshi](#):
>
> Convenient and intuitive iteration of any closed binary operator (e.g. the main post) without having to implement a new `^` method for custom types

I don’t think this is a very common use case for the typical Julia user.

It may be relevant algebra and similar, but it’s perfectly fine to have a package define some syntax for this (without type piracy, of course), perhaps using one of the [available operators](https://github.com/JuliaLang/julia/blob/c973ad84545e67ce528180c2d072b5e767ed73a3/src/julia-parser.scm#L35).

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [April 7, 2020, 10:58am UTC](https://discourse.julialang.org/t/exponentiation-operator-for-iterated-functions/36346/11 "2020-04-07T10:58:31Z")

</div>

> [@Gyoshi](#):
>
> ```julia
> julia> typeof(1.0) |> supertype^(∘,3)
> Number
> 
> julia> 3^(+,3) # Iterated addition -> Scalar multiplication
> 9
> 
> julia> 3^(^, 3) # Tetration 😄
> 19683
> 
> ```

Interesting generalization… It’s basically providing a shorter `reduce` syntax for the case where the collection is a repetition of the same element. Then I would rather provide nice syntax for the collection itself:

```julia
julia> ↑(el,n::Integer) = fill(el,n)

julia> reduce(*, 2↑10)
1024

julia> typeof(1.0) |> ∘(supertype↑3...)
Number

```

We could also define a reciprocal operator for the reduction:

```julia
↓(itr, op::Function) = reduce(op, itr)

julia> (2↑10)↓*
1024

julia> typeof(1.0) |> (supertype↑3)↓∘
Number

```

But the definition of `↓` should probably define the associativity of reduction (using `foldl` or `foldr`), and it’s not clear which it should be.

In any case I think @Tamas_Papp is right, it’s better to define this in a package using another operator.
