thantez

thantez

How to pattern matching on function calls

Hi. Suppose that I have this map:

session = %{
  user: %{
    get_image: fn -> %{image: "abcd.jpg"} end
  }
}

And I want to get image name by pattern matching like this:

%{user: get_image.(): %{image: image}} = session
IO.inspect image # I expect this result: "abdc.jpg"
# but I will get unexpected token error.

How can I do that?
On other words, I want to pattern matching on result of calling get_images. Without pattern matching we can do this and it will work:

IO.inspect session.user.get_image.().image # result is: "abcd.jpg"

PS: I think that this approach is not according to functional paradigm, but I want to know that is this possible or not!

Marked As Solved

dorgan

dorgan

Pattern matching is useful to match on the shape of data and maybe extract data from it.
What you’re trying to do is to match on a pattern and also run functions in a pattern.

It’s also not clear what you’re trying to do and this looks like an XY Problem

In this map, the get_image field is a function, but here:

The syntax is not valid, and also they look like completely different data structures, since in the pattern you expect another map to be in the user map.

From your last snippet:

I may be wrong, but it looks like you’re trying to have code that looks like it would do in an OOP language. It’s not clear why would a get_image function exist in a data structure that holds data about the user. It may make sense in the OOP world where classes mix data and behaviour and objects kind of know how to work with themselves, but in functional land this doesn’t hold.

Let’s say you have a user map that looks like this:
user = %{image: nil}
And your session:
session = %{user: user}

If you want to populate the user’s image field, you need a function to do that:

# Let's also suppose the user may have other fields besides `image`
get_image = fn user -> %{user | image: "abcd.jpg"} end

It takes a user, and updates the image field.

So now that you have that function, you need to use it to update the user inside the session, you can do that with update_in or put_in:
session = update_in(session.user, get_image)
It will run get_image in the user and return the updated session

Now that you have your session populated, in some other part of your code you can use pattern matching to extract the image from it:

%{user: %{image: image}} = session
IO.inspect(image) # "abcd.jpg"

What I’m trying to show is that transforming data and pattern matching are two completely different things, so you should work with them separately. When you’re in a pattern, you’re not building new data, you’re, well, defining a pattern. At the point you pattern match, the data was already transformed and you want to know something about it or assert it has a given shape.

Also Liked

Dusty

Dusty

If you pattern match, you generally won’t need a function to get the image, because you can capture the value of image whenever you pass the session as a parameter:

session = %{user: %{image: "user_image.jpg"}}

def some_function_using_the_session_image(%{user: %{image: image}} = _session) do
  # image is available here
end

result = some_function_using_the_session_image(session)

Where Next?

Popular in Questions Top

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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 52341 488
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement