# |n\> as notation for function piping n times?

**URL:** <https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712>\
**Category:** Internals & Design\
**Created:** [January 9, 2023, 12:37pm UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712 "2023-01-09T12:37:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 9, 2023, 12:37pm UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712/1 "2023-01-09T12:37:04Z")

</div>

Suppose you wanted to repeatedly compose a certain function via piping. For sake of argument, say we want to do

```julia
3 |> sqrt |> sqrt |> sqrt |> sqrt

```

One can do this for an arbitrary number of times `n` (where `n = 4` above) using something relatively convoluted like

```julia
3 |> ∘(fill(sqrt,n)...)

```

I think it would be great to have the following notation in Julia for this:

```julia
3 |n> sqrt

```

Is this even implementable? Thoughts, either for or against?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [January 9, 2023, 12:46pm UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712/2 "2023-01-09T12:46:45Z")

</div>

Why not overload `^` for functions?  
At least that would not require changes to the parser.

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

julia> sqrt^3
sqrt ∘ sqrt ∘ sqrt

julia> (sqrt^3)(3)
1.147202690439877

julia> cbrt^3
cbrt ∘ cbrt ∘ cbrt

julia> (cbrt^3)(3)
1.041528498231435

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 9, 2023, 1:17pm UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712/3 "2023-01-09T13:17:18Z")

</div>

See [JuliaLang/julia#39042](https://github.com/JuliaLang/julia/pull/39042).

---

<div class="post-metadata">

**Author:** ![fph](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fph/32/17159_2.png) [@fph](https://discourse.julialang.org/u/fph)\
**Post date:** [January 9, 2023, 5:42pm UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712/4 "2023-01-09T17:42:02Z")

</div>

Isn’t this syntax ambiguous? One could parse it as `(3 | n) > sqrt`, where `|` is bitwise or and `>` is “greater than”. Currently this fails because `isless` is not defined for function types, but it isn’t hard to imagine an overload.

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [January 10, 2023, 10:16am UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712/5 "2023-01-10T10:16:19Z")

</div>

Thanks for all the responses. I’m still not clear that repeated piping is precisely the same as using function composition “exponentiation”.

Interestingly, the linked PR also links to issue [#41579,](https://github.com/JuliaLang/julia/issues/41579) where this way using the pipe repeatedly is also recorded:

```julia
foldl(|>, Iterators.repeated(foo, n))

```

and seems to be quite fast among the alternatives.

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [January 10, 2023, 11:02am UTC](https://discourse.julialang.org/t/n-as-notation-for-function-piping-n-times/92712/6 "2023-01-10T11:02:43Z")

</div>

Toying with it a bit…

```julia
julia> |>ⁿ(x, (f, n)) = foldl(|>, Iterators.repeated(f, n); init=x)
|>ⁿ (generic function with 1 method)

julia> 3 |>ⁿ (sqrt, 4)
1.0710754830729146

```
