Are Elixir variables really immutable?

Don’t think of “variables” in Elixir as variables in imperative languages, “spaces for values”. Rather look at them as “labels for values”. Maybe you would better understand it when you look at how variables (“labels”) work in Erlang. Whenever you bind a “label” to a value, it remains bound to it forever (scope rules apply … Read more

Elixir Sleep / Wait for 1 Second

Timer uses milliseconds not seconds, update to: IO.puts “foo” ; :timer.sleep(1000); IO.puts “bar” Documentation of :timer in Erlang’s doc: Suspends the process calling this function for Time amount of milliseconds and then returns ok, or suspend the process forever if Time is the atom infinity. Naturally, this function does not return immediately. http://erlang.org/doc/man/timer.html#sleep-1

What’s the difference between `def` and `defp`

From Elixir’s documentation on functions within modules: Inside a module, we can define functions with def/2 and private functions with defp/2. A function defined with def/2 can be invoked from other modules while a private function can only be invoked locally. So defp defines a private function.

How to run an Elixir application?

mix run does run your app. It’s just that when you simply put IO.puts “something” in a file that line is only evaluated in compile-time, it does nothing at runtime. If you want something to get started when you start your app you need to specify that in your mix.exs. Usually you want a top-level … Read more

Elixir: When to use .ex and when .exs files

.ex is for compiled code, .exs is for interpreted code. ExUnit tests, for example, are in .exs files so that you don’t have to recompile every time you make a change to your tests. If you’re writing scripts or tests, use .exs files. Otherwise, just use .ex files and compile your code. As far as … Read more