ollran

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

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}.

Also Liked

lpil

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 :slight_smile:

https://hexdocs.pm/elixir/Kernel.html#elem/2

glmeocci

glmeocci

I’d love it!

{:file_info, file_sieze | _the_rest_as_tuple}  <- info
dkuku

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,
dkuku

dkuku

Thanks, this makes the code much more readable, not much examples for this module, if someone needs one for this particular case:

defmodule FileInfo do
  require Record
  Record.defrecord :file_info, Record.extract(:file_info, from_lib: "kernel/include/file.hrl")
end

and then instead my previous line I have a keyword list that can be accessed with

iex(3)> import FileInfo
FileInfo
iex(4)> info
{:file_info, 2333891, :regular, :read, {{2020, 12, 29}, {19, 18, 12}},
 {{2020, 12, 29}, {19, 27, 58}}, {{2020, 12, 29}, {19, 27, 58}}, 33188, 1,
 66310, 0, 302070, 33, 19}
iex(5)> file_info(info)
[
  size: 2333891,
  type: :regular,
  access: :read,
  atime: {{2020, 12, 29}, {19, 18, 12}},
  mtime: {{2020, 12, 29}, {19, 27, 58}},
  ctime: {{2020, 12, 29}, {19, 27, 58}},
  mode: 33188,
  links: 1,
  major_device: 66310,
  minor_device: 0,
  inode: 302070,
  uid: 33,
  gid: 19
]
iex(6)> file_info(info, :size)
2333891

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
fireproofsocks
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
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
malloryerik
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
9mm
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement