How to run a function N times
Ever want to do something N times?
If you come from the Ruby on Rails world (like I did), you might be looking for something like 5.times { do_something }
.
This is one way I like to do this:
-
Create a stream with the function you want to run repeatedly with
Stream.repeatedly/1
. -
Then, when you want to run that
N
times, youEnum.take(N)
!
Check it out! 👇
https://www.elixirstreams.com/tips/run-elixir-function-n-times
Read next A small language (Ovo2) in Elixir : a graphical stateful environment