Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: verb to apply a function N times #41579

Closed
BeastyBlacksmith opened this issue Jul 14, 2021 · 1 comment
Closed

FR: verb to apply a function N times #41579

BeastyBlacksmith opened this issue Jul 14, 2021 · 1 comment

Comments

@BeastyBlacksmith
Copy link

BeastyBlacksmith commented Jul 14, 2021

There are different ways to express "apply function foo N times to x"

julia> using BenchmarkTools
julia> N = 10; x = 1; foo(x) = x + 1;
julia> @btime foldl(|>, Iterators.repeated($foo, $N), init=$x)
  0.988 ns (0 allocations: 0 bytes)
11

julia> @btime (fill($foo,$N)...)($x)
  6.034 μs (1 allocation: 80 bytes)
11

julia> @btime reduce(,repeat([$foo],$N))($x)
  1.811 μs (2 allocations: 160 bytes)
11

julia> @btime reduce(,Iterators.repeated($foo,$N))($x)
  2.182 μs (0 allocations: 0 bytes)
11

(collected from slack thread)
I think those are hard to remember and having an allocation free fast implementation in Base would be a good idea in my opinion.

The only names that come to my mind are:

  • apply(foo, N, x)
  • fold(foo, N)(x) (though having foldr, foldl and fold is probably too much)
@fredrikekre
Copy link
Member

#39042

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants