Unofficial Elixr Un-FAQ
I decided to create a document containing a list of all the small, irrelevant or stupid questions that I have while learning Elixir.
- Why cant I access/set values on Enums with square brackets (eg whatever[x])?
According to Jose Valim, this is by design. The access operation is linear, so you should avoid it if you can.
- How do I activate command line history in IEX?
Suprisingly, there is currently no way to do it. This is a legacy issue with the erlang shell.
- How can I persist the value of a variable? How can I change it if its immutable?
One way is to store it in process. Have a recursive function that stores it and feeds the new value to itself:
def loop(value)
receive do
# get or create a new copy the value here, then loop
loop(new_value)
end
end- Is there a way to define an anonymous function with dynamic arity? (ie, arity of N where N is not known at compile time)
You could use a list as arguments. Or you can dynamically create a function with:
:erlang.make_fun(mod, name, arity)- How do you use the new Logger?
Add :logger to the application list, and then require it in the modules you will use it.
- Is there a process viewer?
Erlang does have one, and it works for elixir too:
:etop.start()