9,601
questions
315
votes
9
answers
32k
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
39k
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
122k
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
120k
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(...
104
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 ...
97
votes
11
answers
83k
views
Getting the current date and or time in Elixir
This seems like a really dumb question but how does one get and display the current date or time in Elixir? I tried going through the docs but couldn't find anything. Do I need to use an Erlang ...
97
votes
2
answers
49k
views
How to run an Elixir application?
What is the correct way to run an Elixir application?
I'm creating a simple project by:
mix new app
and after that I can do:
mix run
which basically compiles my app once. So when I add:
IO.puts "...
95
votes
16
answers
66k
views
How to convert map keys from strings to atoms in Elixir
What is the way to convert %{"foo" => "bar"} to %{foo: "bar"} in Elixir?
88
votes
2
answers
66k
views
Elixir Sleep / Wait for 1 Second
How to sleep / wait for one second?
Best I could find was something like this (in iex):
IO.puts "foo" ; :timer.sleep(1); IO.puts "bar"
But both of my puts happen with no delay.
86
votes
7
answers
42k
views
Run shell commands in Elixir
I want to execute a program through my Elixir code. What is the method to call a shell command to a given string? Is there anything which isn't platform specific?
82
votes
7
answers
26k
views
How do I raise a number to a power in Elixir?
How can I calculate a number with an exponent in Elixir?
For example, 23 would return 8.
81
votes
5
answers
17k
views
Are Elixir variables really immutable?
In Dave Thomas's book Programming Elixir he states "Elixir enforces immutable data" and goes on to say:
In Elixir, once a variable references a list such as [1,2,3], you know it will always ...
81
votes
3
answers
25k
views
In Elixir how do you initialize a struct with a map variable
I know its possible to create a struct via %User{ email: '[email protected]' }. But if I had a variable params = %{email: '[email protected]'} is there a way to create that struct using that variable for eg, %...
79
votes
3
answers
27k
views
How to create a map from a list of two item tuples in Elixir
What would be an elegant way for converting a list of two item tuples like [{1,2},{3,4}] into the map %{1=>2, 3=>4}?
Keyword list would be trivial, but what if we have arbitrary keys?
76
votes
4
answers
25k
views
In Elixir, is there any way to get a module to list its functions?
In the same way that we can get any object (or class) in Ruby to list its methods, is there any function in Elixir to list all functions belonging to a module? Something (at least remotely) like ...
73
votes
6
answers
96k
views
Turn postgres date representation into ISO 8601 string
I'm trying to format a Postgres date representation into a ISO 8601 string. I'm assuming that there is a Postgres function that can do it, but I found the documentation short on examples.
My query ...
73
votes
3
answers
47k
views
How do I recompile an Elixir project and reload it from within iex?
I'm currently learning Elixir by going through the OTP and mix tutorial on the elixir-lang website, and I'm having trouble finding out how to recompile and reload the project from within the shell.
...
73
votes
11
answers
46k
views
Good IDE for Elixir [closed]
I am looking for an IDE that does atleast some of the following
Code completion when typing variable names and possible
functions after pressing .
Show a functions signature and docs
Navigate to the ...
73
votes
6
answers
16k
views
Debugging and debugging tools in Elixir?
I've just started using Elixir, and have started a Phoenix project, which I enjoy a lot.
Now by having rails background, I'm used to being spoiled with debugging tools like "debugger", "byebug" and so ...
72
votes
3
answers
26k
views
Pattern match function against empty map
I'm playing around with pattern match and I found out, that it's not quite easy to pattern match parameters of a method against an empty map. I thought it would go something like this:
defmodule ...
72
votes
2
answers
34k
views
How to Log something in Controller when Phoenix Server is running?
I'm trying to print some debug information from one of my Controllers in my Phoenix app when the server is running.
defmodule PhoenixApp.TopicController do
use PhoenixApp.Web, :controller
def ...
71
votes
4
answers
16k
views
In Elixir's ExUnit, is it possible to just run one test?
In Python's nosetests you can just specify to run one test by calling its class followed by the test name. How can we do this with Elixir's ExUnit?
70
votes
5
answers
48k
views
Simulate ternary operator in Elixir
How to do the similar conditional one-line check in Elixir?
if (x > 0) ? x : nil
Is this the only equivalent in elixir world?
if true, do: 1, else: 2
70
votes
2
answers
37k
views
How to use IO.inspect on a long list without trimming it?
When I do:
IO.inspect [:right, :top, :left, ...very_long_list]
I only get the first items (it's a list of moves to solve a 15-puzzle) like this:
[:right, :top, :left, :bot, :bot, :left, :top, :top, :...
70
votes
3
answers
23k
views
How can I make Mix run only specific tests from my suite of tests?
How can I make Mix run only specific tests from my suite of tests?
When running mix test all tests are executed
69
votes
5
answers
120k
views
Add new element to list
I was trying to add a new element into a list as follow:
iex(8)> l = [3,5,7,7,8] ++ 3
[3, 5, 7, 7, 8 | 3]
iex(9)> l
[3, 5, 7, 7, 8 | 3]
Why did I get on the 5th position like
8 | 3
What it ...
69
votes
7
answers
12k
views
Erlang Processes vs Java Threads
I am reading "Elixir in Action" book by Saša Jurić, and in the first chapter it says:
Erlang processes are completely isolated from each other. They share
no memory, and a crash of one process ...
68
votes
5
answers
19k
views
In Elixir, how can a range be converted to a list?
I can declare a range as follows:
range = 1..10
Is there a way to convert the range to a list?
65
votes
4
answers
12k
views
Is there a way to test private functions in modules in ExUnit of Elixir?
Functions defined by defp aren't exported so I can't execute them in places other than in the module.
64
votes
6
answers
37k
views
How do you define constants in Elixir modules?
In Ruby, if one were defining constants in classes, they would define them using all caps. For example:
class MyClass
MY_FAVORITE_NUMBER = 13
end
How do you do this in Elixir? And if no such ...
64
votes
1
answer
31k
views
How do you pass a function as a parameter in Elixir?
How do you specify a function as a parameter to another function in Elixir? For instance, pass foo to bar so that bar can then call foo. What is the syntax both in the calling function and the ...
63
votes
4
answers
43k
views
Elixir convert struct to map
I'm trying to convert a struct to a map to be able to clean all the nil values
I'm currently using this code
case Nadia.get_updates do
{:ok, results} ->
Map.from_struct(results)
...
62
votes
1
answer
9k
views
Catch-all/wildcard route in Elixir's Phoenix?
In the Phoenix router is there a route that could act as a wildcard?
62
votes
1
answer
9k
views
In what sense are languages like Elixir and Julia homoiconic?
Homoiconicity in Lisp is easy to see:
(+ 1 2)
is both the function call to + with 1, 2 as arguments, as well as being a list containing +, 1, and 2. It is simultaneously both code and data.
In a ...
60
votes
5
answers
28k
views
How to read config variable in Phoenix / Elixir?
I'd like to set the title of my app in my/config/config.exs file:
config :my, My.Endpoint,
url: [host: "localhost"],
root: Path.dirname(__DIR__),
secret_key_base: "secret",
title: "My App"
...
58
votes
2
answers
6k
views
Pry while testing
I'm pretty new in Elixir, but have a lot fun with it!
I came from Ruby world, so start looking analogy. And there is exist debugging tool pry. Using binding.pry I can interrupt any session. I found ...
58
votes
3
answers
15k
views
Elixir lists interpreted as char lists
I'm just starting out with Elixir. I'm writing some tests using ExUnit for simple Enumerable functions that I am implementing myself, without using the standard Enum module.
In my tests I'm finding ...
58
votes
2
answers
21k
views
How to alter a column name in an Ecto model with a migration?
Does anyone know of (or can provide) an example of how to alter a table column name using the ecto DSL
I'm guessing I just use mix ecto.gen.migration but then would I just fill in the empty created ...
57
votes
3
answers
11k
views
How to use the "with" keyword in Elixir and what is it for?
In elixir 1.2 they've included the keyword "with", but it's not completely clear to me what it is for.
How and in which situation would I use it?
57
votes
2
answers
12k
views
How to connect two Elixir nodes via local network?
How can I connect two Erlang/Elixir-nodes of two different machines via network connection?