Understanding Elixir loop
As many of you already know Elixir does not have loop. There are 2 main things to understand about this.
a) You need the same method signature to check the loop count
b) Your method need to be handling the real actions / tasks and not loop checker.
Let's see this in action. You have 2 runs methods and this has to match, otherwise elixir will complaint.
Next, the run method which implement loop check, just do what it suppose to do, check the counter.
Depending on true / false, you method "run" (without loop checking) is call. That's why you need to have implementation code like :timer.sleep in there.
defmodule FakeProcess do
def run(n) when n <= 1 do
IO.puts "serving as a final loop run"
:timer.sleep(1000)
end
def run(n) do
IO.puts n
:timer.sleep(1000)
run(n - 1)
end
end
Comments