# More idiomatic way of phrasing \`reduce(\\circ, repeated(f, x))(y)\`?

**URL:** <https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647>\
**Category:** Performance\
**Tags:** performance, functional\
**Created:** [August 6, 2026, 3:01pm UTC](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647 "2026-08-06T15:01:49Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [August 7, 2026, 1:33pm UTC](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647/21 "2026-08-07T13:33:10Z")

</div>

> [@rokke](#):
>
> I usually use `reduce(∘, repeated(func, x))(a)` when I need to apply `func` to `a` `x` times, but apparently it spends most of the time creating the single composed function; […] is there a way to tell julia to just apply the functions directly instead of first collecting them into a composed function?

A bit late to the party, but the closest thing to what you are doing is to use `foldl` with `|>` instead of creating the composite function. For instance

```julia
val = 1.2
n = 5
foldl(|>, Iterators.repeated(exp, n), init = val)

```

I haven’t benchmarked it, but I’ve found `foldl(|>, fs, init = val)` to be a pretty useful pattern to compose a list of functions, but of course the other approach with `(y, _) -> exp(y)` also works if all the functions are equal.

---

<div class="post-metadata">

**Author:** ![rokke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rokke/32/222691_2.png) [@rokke](https://discourse.julialang.org/u/rokke)\
**Post date:** [August 7, 2026, 1:44pm UTC](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647/22 "2026-08-07T13:44:46Z")

</div>

nice, looks like that’s basically the same as the other best ones and is much easier to read:

master:

```julia-auto
julia> f(a) = foldl(|>, repeated(exp, 1000); init=a)
f (generic function with 3 methods)

julia> @benchmark f(10.)
BenchmarkTools.Trial: 10000 samples with 999 evaluations per sample.
 Range (min … max): 8.245 ns … 184.536 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 9.232 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 9.830 ns ± 3.589 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▃ ▄▇█▇▃ ▅▃ ▂▁ ▂
  █▆▄▄██████▇▆▆▄▄▄▆▄███▇▇▆██▇▆▆▆▆▆▆▆▆▆▆▅▆▇▆▇▇▆▆▆▅▅▅▆▆▆▄▅▄▄▅▄▅ █
  8.25 ns Histogram: log(frequency) by time 16.7 ns <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark f($10.)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 5.883 μs … 112.796 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 7.540 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 7.786 μs ± 2.355 μs ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▂ ▅▅ █▂ ▁▁ ▂▁ ▁
  █▅▂██▇▆▇██▇███████▇▇▇▆▆▆▆▄▅▅▅▆▆▅▆██▆▅▅▅▅▃▄▅▅▆▄▃▂▃▂▅▃▃▃▂▂▃▃▅ █
  5.88 μs Histogram: log(frequency) by time 17.1 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [August 8, 2026, 8:03am UTC](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647/23 "2026-08-08T08:03:34Z")

</div>

As DNF suggested, here is a simple loop implementation:

```julia-auto
julia> iterated_fnx(f,n,x) = ( while (n -= 1) ≥ 0; x = f(x); end; return x)
iterated_fnx (generic function with 1 method)

julia> iterated_fnx(exp, 3, 1.0)
3.814279104760214e6

```

It’s compacted a bit, but still as legible as the alternatives IMHO.  
It benchmarks with no allocations on Julia 1.11.

---

<div class="post-metadata">

**Author:** ![rokke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rokke/32/222691_2.png) [@rokke](https://discourse.julialang.org/u/rokke)\
**Post date:** [August 8, 2026, 8:12am UTC](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647/24 "2026-08-08T08:12:11Z")

</div>

while loop appears to be a bit slower, maybe since you’ve got an extra `n-=1` operation and `>` check each time:

```julia-auto
julia> f1(a) = foldl(|>, repeated(exp, 1000); init=a)
f1 (generic function with 1 method)

julia> f2(a) = (for _=1:1000 a=exp(a) end; a)
f2 (generic function with 1 method)

julia> f3(a) = (n=1000; while (n-=1)≥0 a=exp(a) end; a)
f3 (generic function with 1 method)

julia> @benchmark f1($10.)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 6.554 μs … 25.477 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 6.665 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 6.824 μs ± 864.537 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▆█▄▁ ▁
  █████▇▇▆▆▆▇▇██▇▇▆▆▆▆▆▆▅▅▆▆▆▆▆▆▆▆▅▅▆▅▅▅▅▅▄▅▅▅▄▄▄▄▄▅▄▂▂▃▄▄▃▃▃ █
  6.55 μs Histogram: log(frequency) by time 10.6 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark f2($10.)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 6.564 μs … 25.035 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 6.642 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 6.868 μs ± 967.479 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ██▂▁ ▁
  █████▇▇▆▆▆▆▆▆▅▄▅▅▆▇▇▇▆▇▆▆▆▆▆▅▆▅▅▆▆▅▇▆▆▅▆▆▆▆▄▅▆▆▆▆▆▅▆▅▅▄▅▅▅▄ █
  6.56 μs Histogram: log(frequency) by time 11 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark f3($10.)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 7.518 μs … 29.437 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 7.546 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 7.719 μs ± 912.501 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  █▄▂▂▁▁ ▁
  ██████████▇█▇▆▆▇▆▇▆▆▆▆▆▆▆▅▅▆▆▆▆▆▆▅▅▅▅▆▅▆▅▅▆▅▅▅▅▅▅▄▅▄▄▃▃▅▅▄▅ █
  7.52 μs Histogram: log(frequency) by time 10.1 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

if you specifically define all three as variables, it becomes equivalent though:

```julia-auto
julia> @benchmark f1(exp, $10., 1000)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 5.942 μs … 33.609 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 6.646 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 6.762 μs ± 646.787 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

           ▅▇█▄▁ ▁▁▁ ▂
  ▅▁▁▁▁▃▁▁▁███████▇▇█▇██████▆▆▆▇▇▇▆▇▇▆▆▅▆▆▆▅▅▅▅▅▆▆▅▅▅▅▅▅▄▄▅▅▅ █
  5.94 μs Histogram: log(frequency) by time 9.67 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark f2(exp, $10., 1000)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 5.962 μs … 24.994 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 6.680 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 6.927 μs ± 966.049 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

         ▇▇█▂ ▁▁▁ ▁▂▁ ▃ ▂
  ▃▁▃▁▁▁▁█████▇▇▆█████▇▆▇▇▇███▆▇▆▆▆█▇█▆▆▇▇▆▅▆▆▅▆▆█▅▅▅▄▄▅▄▅▄▅▅ █
  5.96 μs Histogram: log(frequency) by time 10.6 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark f3(exp, $10., 1000)
BenchmarkTools.Trial: 10000 samples with 5 evaluations per sample.
 Range (min … max): 5.973 μs … 36.853 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 6.675 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 6.804 μs ± 796.739 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

           ▇██▃▁ ▁▁ ▂
  ▃▁▁▁▁▁▁▃▁██████▇█▇▇██████▇▆▇▆▇▅▆▆▆▆▆▆▆▇▅▆▆▅▆▅▅▆▅▅▅▅▆▆▄▅▅▅▅█ █
  5.97 μs Histogram: log(frequency) by time 9.74 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

---

<div class="post-metadata">

**Author:** ![wheeheee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wheeheee/32/36663_2.png) [@wheeheee](https://discourse.julialang.org/u/wheeheee)\
**Post date:** [August 15, 2026, 2:49pm UTC](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647/25 "2026-08-15T14:49:08Z")

</div>

You can frequently get a free speed boost by sprinkling `@inline` there, if it’s a rather heavy function that doesn’t automatically get inlined.

```julia
f2(f, a, n) = (for _=1:n a=f(a) end; a)
f3(f, a, n) = (for _=1:n a=@inline(f(a)) end; a)

```

```julia-repl
julia> @benchmark f2(exp, x, 1000) setup=(x=rand())
BenchmarkTools.Trial: 10000 samples with 9 evaluations per sample.
 Range (min … max): 2.456 μs … 9.911 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 2.478 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 2.605 μs ± 269.830 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  █▇ ▁▇▆ ▁▂ ▅ ▁▁ ▂
  ██▄▁▅▁▃▁▁▃▄▇███▅██▇▆▅▆▆▆▆▇▅▅█▅██▆▅▁▇█▇▆▆▇▅▇▆▆▅▅▆▆▇▃▅▄▅▃▃▄▁▆ █
  2.46 μs Histogram: log(frequency) by time 3.39 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

julia> @benchmark f3(exp, x, 1000) setup=(x=rand())
BenchmarkTools.Trial: 10000 samples with 9 evaluations per sample.
 Range (min … max): 2.022 μs … 8.378 μs ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 2.033 μs ┊ GC (median): 0.00%
 Time (mean ± σ): 2.063 μs ± 170.374 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

  ▅█▄ ▂▁ ▁
  ███▄▅██▃▃▅▃▁▁▁▁▃▅▇▆▃▆██▁▇▆▆▅▆▆▆▅▅▆▅▁▅▃▅▃▅▃▃▄▁▁▁▁▃▁▁▁▁▄██▆▆▅ █
  2.02 μs Histogram: log(frequency) by time 2.62 μs <

 Memory estimate: 0 bytes, allocs estimate: 0.

```

[Previous page](https://discourse.julialang.org/t/more-idiomatic-way-of-phrasing-reduce-circ-repeated-f-x-y/138647.md?page=1)
