shahryarjb

shahryarjb

Is there possible to find parent module in nested module?

Hi, I have this nested module:

defmodule AA do
  defmodule BB do
    defmodule CC do
      ...
    end
  end
end

now is there a way get all the parents reversely?

For example:

[AA.BB.CC, AA.BB, AA]

It should noted, maybe the user named a module like Testy.AA, I just need to explore nested module, I mean module inside the other module. and I have always the last one module name, AA.BB.CC

Thank you

Most Liked

tomekowal

tomekowal

I’d do it this way:

defmodule MyModuleHelpers do
  def parent(module) do
    module # e.g. Very.Long.Module
    |> Module.split() # ["Very", "Long", "Module"]
    |> remove_last()
    |> Module.concat() # opposite to split Very.Long
  end

  # Elixir modules are atoms underneath that always start with "Elixir"
  # e.g. :"Elixir.Very" is pritned in console as Very
  # We use the `:Elixir` atom as recursion end
  # and we say that :Elixir does not have parents
  def parents(:Elixir), do: []

  # here is the recursive clause
  # the returned list starts with current module
  # and then calls itself recursively with parent
  # we use the fact that list constructor is itself recursive
  # [Very.Long.Module, Very.Long, Very] == [Very.Long.Module | [Very.Long | [Very | []]]]
  def parents(module) do
    [module | parents(parent(module))]
  end

  # there is no "remove last element" function, so the trick is to reverse and remove first
  defp remove_last(list) do
    list # ["Very", "Long", "Module"]
    |> Enum.reverse() # ["Module", "Long", "Very"]
    |> tl() # tail function gives everything BUT the first element ["Long", "Very"]
    |> Enum.reverse() # ["Very", "Long"]
  end
end
codeanpeace

codeanpeace

In case it gives you more ideas, there’s a Module.__info__/1 as well.

iex(5)> AA.BB.CC.__info__(:module)
AA.BB.CC

It’s also worth noting that modules aren’t truly nested in Elixir since they’re all flattened into a top level if I recall correctly. The superficial “nesting” via the dot/period is just a namespacing convention.

al2o3cr

al2o3cr

There’s not a good way, and IMO that’s intentional - the apparent hierarchy of dotted names (Foo.Bar.Baz) doesn’t correspond to any real nesting.

For instance, naming a module Foo.Bar.Baz says precisely nothing about the existence of modules named Foo and Foo.Bar. They could exist, but they aren’t required to like in other languages (for instance, Ruby).

There’s no way to go in the opposite direction either: given a module named Foo.Bar, there’s no API to retrieve every module named Foo.Bar.<whatever>.

Where Next?

Popular in Questions Top

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
JorisKok
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
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
stefanchrobot
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
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
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
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
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
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
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement