9,611
questions
316
votes
9
answers
33k
views
Why are there two kinds of functions in Elixir?
I'm learning Elixir and wonder why it has two types of function definitions:
functions defined in a module with def, called using myfunction(param1, param2)
anonymous functions defined with fn, ...
237
votes
10
answers
40k
views
How can I schedule code to run every few hours in Elixir or Phoenix framework?
So let's say I want to send a bunch of emails or recreate sitemap or whatever every 4 hours, how would I do that in Phoenix or just with Elixir?
195
votes
10
answers
123k
views
How to join strings in Elixir?
How do I join two strings in a list with a space, like:
["StringA", "StringB"]
becomes
"StringA StringB"
176
votes
12
answers
121k
views
How do you check for the type of variable in Elixir
In Elixir how do you check for type such as in Python:
>>> a = "test"
>>> type(a)
<type 'str'>
>>> b =10
>>> type(b)
<type 'int'>
I read in Elixir ...
164
votes
6
answers
32k
views
Elixir: use vs import
What's the difference between use and import?
use is a simple mechanism for using a given module into the current context
https://hexdocs.pm/elixir/Kernel.SpecialForms.html#import/2
Imports ...
129
votes
5
answers
44k
views
Are there things Elixir can do that Erlang cannot, or vice versa?
This question is in the context of the Beam VM and the capabilities that it provides, not in the general context of what a Turing complete language can do. I want to invest some time to learn either ...
125
votes
9
answers
120k
views
Convert Elixir string to integer or float
I need to convert a string to a floating point value or an integer. There was no method such as,
string_to_integer
119
votes
1
answer
14k
views
Where does Elixir/erlang fit into the microservices approach? [closed]
Lately I've been doing some experiments with docker compose in order to deploy multiple collaborating microservices. I can see the many benefits that microservices provide, and now that there is a ...
117
votes
3
answers
12k
views
What is the benefit of Keyword Lists?
In elixir we have Maps:
> map = %{:a => "one", :b => "two"} # = %{a: "one", b: "two"}
> map.a # = "one"
> map[:a] # = "one"
We ...
110
votes
5
answers
102k
views
How to check if an item exists in an Elixir list or tuple?
This is seemingly simple, but I can't seem to find it in the docs. I need to simply return true or false if an item exists in a list or tuple. Is Enum.find/3 really the best way to do this?
Enum.find(...
105
votes
3
answers
14k
views
IEx - How to cancel multiline command?
When I use IEx and make a typo like additional bracket or ", then most of the time I get a syntax error. But there are cases like this one:
iex(3)> Enum.each '12345', &(IO.puts(&1"))
....
104
votes
4
answers
29k
views
Elixir: When to use .ex and when .exs files
Elixir's documentation states that
In addition to the Elixir file extension .ex, Elixir also supports
.exs files for scripting. Elixir treats both files exactly the same
way, the only ...
104
votes
4
answers
24k
views
Is there a Phoenix equivalent to Rails Console
I'm just learning Phoenix and Elixir and I'm coming from Ruby/Rails where I work in the REPL using pry to inspect my database and application state.
I'm trying to figure out how to interact with my ...
103
votes
3
answers
72k
views
How to generate a random number in Elixir?
I need to generate a random number. I found the Enum.random/1 function, but it expects an enumerable such as a list or range of numbers.
Is that the only way to get a random number?
102
votes
2
answers
28k
views
What's the difference between `def` and `defp`
I'm going through the Programming Phoenix book and I am wondering what the difference between def and defp is.
There are several functions in my controller - most of them are actions like this:
def ...