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

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?

Last Post!

xshyne88

xshyne88

In my particular app it doesn’t actually matter because all of our ID’s are global UUIDS. This is an Absinthe Batching function if that makes more sense. My resolvers are calling &SomeModule.first/3

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New

Other popular topics Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New

We're in Beta

About us Mission Statement