Questions tagged [elixir]

Elixir is an open-source, dynamic, compiled, general purpose functional programming language. It was designed to be fully compatible with the Erlang platform and is well suited to writing fault-tolerant, distributed applications with soft real-time guarantees and the ability for hot-code-swapping.

elixir
Filter by
Sorted by
Tagged with
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 ...
George Taveras's user avatar
10 votes
1 answer
6k views

Integer List printed as String in Elixir [duplicate]

I was trying my hand on Enum.map. I found this weird output, when I added 100 to all the elements of the list. Why such an output? It turns out that I am getting a string when I add 100 but works ...
srajappa's user avatar
  • 482
314 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, ...
Alex Marandon's user avatar
236 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?
NoDisplayName's user avatar
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 ...
Odhran Roche's user avatar
  • 1,111
68 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 ...
Wand Maker's user avatar
  • 18.7k
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, %...
bullfrog's user avatar
  • 1,633
31 votes
1 answer
3k views

Why can't I chain String.replace?

I'm working on a price format function, which takes a float, and represent it properly. ex. 190.5, should be 190,50 This is what i came up with def format_price(price) do price |> ...
MartinElvar's user avatar
  • 5,764
61 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?
Jason G's user avatar
  • 965
9 votes
1 answer
6k views

Install elixir on M1 mac

I tried to install Elixir on my M1 mac using brew, as explained here. After installation, elixir --version returns segmentation fault elixir --version.. After reading this, I tried the suggested ...
Sventies's user avatar
  • 2,590
8 votes
1 answer
6k views

Render many to many relationship JSON in Phoenix Framework

I have the next models defmodule App.User do use App.Web, :model alias App.User schema "users" do field :name, :string has_many :roles_users, App.RolesUser has_many :roles, ...
Gidrek's user avatar
  • 750
68 votes
2 answers
35k 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, :...
ItsASecret's user avatar
  • 2,619
47 votes
5 answers
10k views

What is the "pin" operator for, and are Elixir variables mutable?

Currently trying to understand the "^" operator in Elixir. From the website: The pin operator ^ can be used when there is no interest in rebinding a variable but rather in matching against its ...
Charlie's user avatar
  • 4,260
42 votes
3 answers
27k views

Elixir - Looping through and adding to map

I'm rebuilding something in Elixir from some code I built in C#. It was pretty hacked together, but works perfectly (although not on Linux, hence rebuild). Essentially what it did was check some RSS ...
William Dunne's user avatar
22 votes
7 answers
43k views

How To Convert An Elixir Binary To A String?

So I'm trying to convert a binary to a string. This code: t = [{<<71,0,69,0,84,0>>}] String.from_char_list(t) But I'm getting this when I try this conversion: ** (ArgumentError) ...
Onorio Catenacci's user avatar
19 votes
1 answer
7k views

How can I call a module function inside Enum.map without getting an "Undefined reference" error?

I have a simple module containing a single function: defmodule Funcs do def double(x) do x*2 end end When I start iex with the file name as argument, I can call the function just fine: ...
Frank Schmitt's user avatar
18 votes
3 answers
9k views

how can i set session in setup when i test phoenix action which need user_id in session?

I have a test which need to set user_id to session before testing, because this action need to know the current_user. setup do %User{ id: 123456, username: "lcp", email: "...
LcpMarvel's user avatar
  • 203
11 votes
4 answers
14k views

How do you extend/inherit an elixir module?

Let's say an elixir library defines: defmodule Decoder do def decode(%{"BOOL" => true}), do: true def decode(%{"BOOL" => false}), do: false def decode(%{"BOOL" => "true"}), do: ...
anshul's user avatar
  • 5,985
10 votes
5 answers
4k views

How can I get permutations of a list?

How can I get permutations of a list in Elixir? Eg, for ["a", "b", "c"], I would expect: # [["a", "b", "c"], ["a", "c", "b"], # ["b", "a", "c"], ["b", "c", "a"], # ["c", "a", "b"], ["c", "b", "a"]]
Nathan Long's user avatar
8 votes
1 answer
2k views

Enumerate a string in Elixir

In Elixir, if I have a string such as "José1 José2", how do I enumerate it? If I try to use Enum or for comprehensions, I get the following error: ** (Protocol.UndefinedError) protocol ...
Adam Millerchip's user avatar
6 votes
1 answer
2k views

Making conn.assigns available in multiple Phoenix views/templates

I need a user authentication token defined in the SessionControllerto be available in layout/app.html.eex. My SessionController defines a token and assigns it to a conn. token = Phoenix.Token.sign(...
Jason O.'s user avatar
  • 3,230
127 votes
4 answers
42k 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 ...
Saša Šijak's user avatar
  • 8,943
116 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 ...
greggreg's user avatar
  • 12k
69 votes
6 answers
90k 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 ...
CallMeNorm's user avatar
  • 2,455
69 votes
3 answers
25k 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 ...
leifg's user avatar
  • 8,848
57 votes
2 answers
11k 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?
ChaosSteffen's user avatar
50 votes
8 answers
30k views

How to use raw sql with ecto Repo

I have an upsert requirement, so I need to call a postgres stored procedure or use a common table expression. I also use the pgcrypto exgtension for passwords and would like to use postgres functions ...
Krut's user avatar
  • 4,192
49 votes
2 answers
26k views

How can we easily time function calls in elixir?

How can we easily time function calls in Elixir? Is there any hidden switch in IEx to enable this?
Charles Okwuagwu's user avatar
39 votes
1 answer
12k views

Encoding a Ecto Model to JSON in elixir

I am going over the following tutorial in an attempt to get my head around elixir and phoenix: https://thoughtbot.com/blog/testing-a-phoenix-elixir-json-api I am running into an issue with the test, ...
Allister's user avatar
  • 851
37 votes
1 answer
24k views

How to print out a map's array values in Elixir?

New to Elixir from Ruby and I'm trying to print out a map's array values using the following code: map-script.ex list = [0, 1] map = %{0 => [1, 2], 1 => [2, 3]} Enum.each list, fn n -> ...
Nona's user avatar
  • 5,392
33 votes
8 answers
13k views

Return statement in Elixir

I need a function with some kind of a step-by-step logic and I wonder how I can make one. Let's take a log in process on a site as an example, so I need the following logic: 1) Email is present? ...
NoDisplayName's user avatar
30 votes
2 answers
7k views

Phoenix - controller with multiple render

Trying to create an app with Elixir + Phoenix, that would be able to handle both "browser" and "api" requests to handle its resources. Is it possible to do it without having to do something like that ...
Kernael's user avatar
  • 3,280
30 votes
1 answer
11k views

How to broadcast a message from a Phoenix Controller to a Channel?

Is there a way to broadcast a message to a channel from outside that channel? Maybe something like Channel.broadcast topic, event, data? I saw something like this here but the final version of ...
Philip Claren's user avatar
26 votes
2 answers
3k views

How to save/log the output of the iex shell to get persistent command history?

I just started working with Elixir and have been using the iex shell quite a bit. Is it possible for me to save / log a "session" to a file? Thank you.
damianesteban's user avatar
26 votes
1 answer
7k views

How to detect if a user left a Phoenix channel due to a network disconnect?

I have an Elixir/Phoenix server app and the clients connect through the build in channels system via websockets. Now I want to detect when an user leaves a channel. Sidenote: I'm using the javascript ...
Philip Claren's user avatar
23 votes
3 answers
18k views

Elixir: Set variable in if statement

I'm very new to Elixir and this simple problem is driving me nuts. a = 0 if true do a = 1 + 1 end a = a + 1 IO.puts (a) Interestingly this gives the correct value but also gives a warning: ...
iphaaw's user avatar
  • 6,984
19 votes
1 answer
12k views

How to render raw HTML code in Phoenix Framework?

I'm storing raw html from a contenteditable tag in my RethinkDB database. Now I want to display the content after retrieving it. html.eex <div id="contentEditableText"> <%= for %{"...
mesosteros's user avatar
  • 1,511
16 votes
1 answer
2k views

Why Elixir's MapSet becomes unordered after 32 elements?

iex> MapSet.new(1..32) |> Enum.to_list [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32] iex> MapSet.new(1..33) |> ...
sbs's user avatar
  • 4,142
15 votes
2 answers
2k views

Get console user input as typed, char by char

I have a console application in Elixir. I need to interpret user’s input on by keypress basis. For instance, I need to treat “q” as a command to end the session, without user to explicitly press ⏎ a.k....
Aleksei Matiushkin's user avatar
14 votes
3 answers
1k views

Elixir: rationale behind allowing rebinding of variables

What is the rationale behind allowing rebinding of variables in Elixir, when Erlang doesn't allow that?
tldr's user avatar
  • 12k
14 votes
2 answers
6k views

Elixir ecto 2 create many_to_many association

How can I make a many to many relation with ecto 2? As an example app I want to create a Post which can be in multiple categories. The categories already exist. For example: [%Category{id: "1", name:...
Roel Kramer's user avatar
14 votes
1 answer
1k views

What is the "|>" symbol's purpose in Elixir?

I've searched the Elixir and Phoenix docs, as well as a few other sites like Learn Elixir with no luck. Here is what it looks like: defp update_positions(item_ids) do item_ids = String.split(...
bright-star's user avatar
  • 6,217
13 votes
6 answers
12k views

Efficient way to filter a Map by value in Elixir

In Elixir, what would be an efficient way to filter a Map by its values. Right now I have the following solution %{foo: "bar", biz: nil, baz: 4} |> Enum.reject(fn {_, v} -> is_nil(v) end) |>...
Boris Bera's user avatar
13 votes
1 answer
6k views

Phoenix additional layout variables like @inner

I am looking to add additional layout parameters like the @inner for the layout. For example @title for the <title>@title</title> and an area for onload javascript for individual pages. ...
rockerBOO's user avatar
  • 3,000
11 votes
4 answers
4k views

Is there a transpose function in Elixir?

Hi I look for a transpose function in Elixir. For example I have this kind of array a and after calling a function the result should be b: a = [[1, 2], [3, 4], [5, 6]] b = transpose(a) b => [[1, 3,...
Furi's user avatar
  • 153
10 votes
2 answers
2k views

Change Url to accept a string instead id in phoenix framework (elixir)

I am trying to implement permalinks in phoenix app. The goal is to change localhost:4000/products/1 to localhost:4000/products/productname I tried following Ryan Bates episode on permalinks ...
Robin Solanki's user avatar
10 votes
1 answer
5k views

Fill a List with a for-loop

Why can't I fill a list with this simple for-loop? new_data = [] for data <- old_data do new_data = List.insert_at(new_data, -1, data) end After this operation my new_data list is still empty, ...
0xAffe's user avatar
  • 1,176
10 votes
2 answers
752 views

Specify arity using only or except when importing function on Elixir

I'm studying Elixir and when I use only or except operators when importing functions from a module I need to specify an arity number. Why? e.g. import :math, only: [sqrt: 1] or import :math, ...
Guilherme Carlos's user avatar
8 votes
3 answers
2k views

Ecto 2.0 SQL Sandbox Error on tests

I recently upgraded my phoenix project to Ecto 2.0.2. I have some code that is using Task.Supervisor.async_nolink to make some updates to the db on its own thread. I am getting the following error ...
jeffskelton3's user avatar
8 votes
3 answers
4k views

Elixir: Split list into odd and even elements as two items in tuple

I am quiet new to Elixir programming and stuck badly at splitting into two elements tuple. Given a list of integers, return a two element tuple. The first element is a list of the even numbers from ...
ANIL MANE's user avatar
  • 1,405

1
2 3 4 5
13