owaisqayum

owaisqayum

Make changes to a tuple via pattern matching

I want to change a specific part of a tuple. Is it possible to use pattern matching?

{1, [{:reach, [:s, :d]}, [link: [:s, :d]]]}

I want to change reach to :reach_1. I can fetch the reach field but cannot modify the whole tuple.

{id, [{item, _}, _]} =  {1, [{:reach, [:s, :d]}, [link: [:s, :d]]]}

:"#{item}_#{id}"

How can I add it back to the tuple replacing :reach ?

Thanks

Most Liked

al2o3cr

al2o3cr

FWIW, this sort of thing is what get_in / put_in / etc and the Access module are for:

iex(1)> v = {1, [{:reach, [:s, :d]}, [link: [:s, :d]]]}
{1, [{:reach, [:s, :d]}, [link: [:s, :d]]]}

iex(2)> get_in v, [Access.elem(1), Access.at(0), Access.elem(0)]
:reach

iex(3)> put_in v, [Access.elem(1), Access.at(0), Access.elem(0)], :reach_1
{1, [{:reach_1, [:s, :d]}, [link: [:s, :d]]]}
dimitarvp

dimitarvp

Only if you know the exact size and shape of the tuple. Then you can use pattern-matching at any level of complexity you can handle. :slight_smile:

Marcus

Marcus

Hello @owaisqayum

You have to create a new tuple:

iex(2)> {id, [{_item, values}, rest]} =  {1, [{:reach, [:s, :d]}, [link: [:s, :d]]]}
{1, [{:reach, [:s, :d]}, [link: [:s, :d]]]}
iex(3)> {id, [{:reach_1, values}, rest]}
{1, [{:reach_1, [:s, :d]}, [link: [:s, :d]]]}
iex(4)>

Where Next?

Popular in Questions Top

tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
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 I...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Other popular topics Top

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
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31013 112
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 48342 226
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement