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
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]]]}
5
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. ![]()
3
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)>
1
Popular in Questions
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
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
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
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
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
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
The documentation above suggests that while ...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
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
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
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
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
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
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
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
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
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
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...
New
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
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









