When I execute this it gave me like ’ ) ', ’ * ', ’ , ’ … .
So I tried to put initial values in familyIds like [0] and then the first value got in the list but…
The familyIds list didn’t updated. It was always [0]. Why is this happening?
So, is there a way not inserting intial value like [0] and work like [41, 42, 43 …] and
Is there a way updating familyIds at every loop?
While immutability sounds like a reasonable cause, this has nothing to do with immutability. Variables can be reassigned no problem without invalidating immutability (of values). The issue is the lexical scoping rules of elixir, which prevent (re-)assigning variables of an outer scope. Variables are only ever assigned for the current scope. In this case the anonymous function is the scope boundary.
fn family ->
familyIds = [family | familyIds]
end
This code accesses familyIds. The variable at this point is not present in the current lexical scope, so outer scopes are checked. familyIds = [] is found in the next outer scope. family is prepended to the []. The result is assigned for the variable familyIds within the scope of the anonymous function. The result is returned from the anonymous function (to be dropped by Enum.each). Then the code within the scope is completed and the scope variables are dropped. Enum.each calls the anonymous function with the next item and we start from the top.