I found this weird behavior of Enum.each/2
when I use Enum.each/2 within the function, the repo_all/2 returns the same value 3 times but it is supposed to return different values
when I use Enum.each/2 directly the repo_all/2 returning different values
Background: I was creating a helper function to reduce code(Enum.each/2) repetition
defmodule Helper do
def generate_seed_data(str, opts) do
Enum.each(0..2, fn _x ->
repo_all(str, opts)
end)
end
def repo_all(str, opts) do
IO.puts("#{str}, #{opts}")
end
end
Helper.generate_seed_data(Faker.Person.name(), Faker.Person.name())
As a quick note, please always post code as text not screen shots. I’m going to have to re-type what you wrote to provide suggestions, and this takes up a lot of extra time.
As far as solving your question goes, this will make more sense if you step through how your program executes.
What you’re doing is callingFaker.Person.name(). It returns a string, so name1 has a single string value, and name2 has a single string value at that moment. Then you pass those two concrete strings to your helper and ultimately Enum.each.