ollran
Pattern matching against arbitrary sized tuple
How can I pattern match against an arbitrary sized tuple? This works:
{status, _, _} = {:ok, 1, 2}
But what if I don’t know the structure of the tuple on the right side and I only care about the first element? This doesn’t work:
{status, _} = {:ok, 1, 2, 3, 4, 5, 6}
{status, _} = {:ok, {:random, :things}, [1, 2, 3]}
Is it possible to get the first value from the tuple with pattern matching?
Marked As Solved
NobbZ
No. A tuple is of fixed size.
You can either use status = elem({:ok, 1, 2, 3}, 0) or [status | _] = Tuple.to_list({:ok, 1, 2, 3}.
7
Also Liked
lpil
Creator of Gleam
Tuple.to_list/1 will allocate a new list, Kernel.elem/2 can be used to retrieve the first element without creating this intermediate list 
7
glmeocci
I’d love it!
{:file_info, file_sieze | _the_rest_as_tuple} <- info
2
dkuku
would be handy to have some macro that generates the missing underscores:
{:ok, info} <- :file.read_file_info(pid),
{:file_info, file_size, _, _, _, _, _, _, _, _, _, _, _, _} <- info,
{:file_info, file_size, _escape_next(12)} <- info,
1
Popular in Questions
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
Hi,
I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I fore...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Other popular topics
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service.
Currently when I de...
New
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch.
This project took far...
New
Hello guys,
I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I fore...
New
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine)
This is a plugin that adds support for Elixir to JetBrains IntelliJ...
New
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New







