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?

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

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
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
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
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
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

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
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 49522 488
New
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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New

We're in Beta

About us Mission Statement