xshyne88

xshyne88

Generate map pattern matching functions

I have a goal to code generate the following module

defmodule C do 
  def first(%{company_id: company_id}, user}, do: do_first(company_id, user)
  def first(%{sales_id: sales_id}, user}, do: do_first(sales_id, user)
  #... etc
end

I’d like to create a macro potentially? Or even a function where I can give it a list of those atoms, and have it delegate those arguments to the second function
i.e

defmodule C do
  use FirstMacro, [:company_id, :sales_id]  #... could be a very long list
end

I keep writing macros but am unable to figure out how to code pattern match on a map macro the quoted ast for a map is something like {:%{}, , [key: value]} but I’m not able grab value like I would in defining a function so I can’t achieve my goal here, can someone point me in the right direction kindly ^^. Thanks for any help!

my attempt on the 100th iteration looks something like this

defmodule C do
  defmacro first_list(list, user) do
    quote bind_quoted: [list: list, user: user] do
      Enum.map(list, fn x ->
        def first(unquote({:%{}, [], []}), user),
          do: do_first(:need_this_variable, user)
      end)
    end
  end
end

But I know its all wrong :frowning:

Most Liked Responses

NobbZ

NobbZ

As %{foo: foo} is syntactic sugar for %{:foo => foo} the following should work:

[:company_id, :sales_id]
|> Enum.each(fn key ->
  def first(%{unquote(key) => val}, user), do: do_first(val, user)
end)

But to be honest, how is do_first/2supposed to know what kind of id you are passing in?

xshyne88

xshyne88

Thank you very much sir this is exactly what I needed.

NobbZ

NobbZ

Whats a wrong ID?

first(%{company_id: 1}, user) #=> do_first(1, user)
first(%{sales_id:   1}, user) #=> do_first(1, user)

So how does do_first/2 know whether the 1 refers to a sales_id or to a company_id?

Where Next?

Popular in Questions Top

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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
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
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
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

Other popular topics Top

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
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
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
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
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
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

We're in Beta

About us Mission Statement