afhuertass

afhuertass

Traverse nested list and if the list has two elements apply function

Hi!

I have been dealing with some problem where I have a nested list, I want to keep the nested structure, but if the list is a two elements list, then i want to apply a custom function (I’m dealing with multypolygon coordinates and want to apply some transform to the coordinates’ system)

So far, I came up with something using recursion:

defmodule NiceModule do
  def apply_function(nested_list) do
    update_in(nested_list, [Access.all()], fn item ->
      case item do
        [x, y] when is_number(x) and is_number(y) -> fun_fun(x, y)  
        sublist when is_list(sublist) -> apply_function(sublist)  
        other -> other  
      end
    end)
  end
  def fun_fun(x,y) do
    {a,b} ={ x+3 , y+2}
    [a,b]
  end
end

aa = [
  [2, 5.0 , [ 1 , 1 ]],
  [[13.0, 17.0], [25.0, 0.0], [0, 0]] ,
  [ 2 ,2 ]
]

NiceModule.apply_function(aa)

Which I think works, but I’m wondering if I’m doing something anti-elixir or if there is a better way (needless to say, I’m new)

Thanks for your time and answers :slight_smile:

Most Liked

Eiji

Eiji

If that’s all you need then why not simply use Enum.map/2 instead?

    Enum.map(nested_list, fn
      [x, y] when is_number(x) and is_number(y) -> fun_fun(x, y)  
      sublist when is_list(sublist) -> apply_function(sublist)  
      other -> other  
    end)

Creating a tuple that is not needed is just waste of time and resources.

defmodule NiceModule do
  def apply_function(nested_list) do
    Enum.map(nested_list, fn
      [x, y] when is_number(x) and is_number(y) -> [x + 3, y + 2]
      sublist when is_list(sublist) -> apply_function(sublist)  
      other -> other  
    end)
  end
end

This code is 40% (6 of 16 LOC) shorter and therefore definitely more readable.

Eiji

Eiji

Both of course are valid - they are equivalents in this example. Enum.map/1 is just much simpler here. You use update_in/3 together with Access.all/0 in case you have to update a nested (non-recursive) structure, for example:

defmodule NiceModule do
  def apply_function(map_with_nested_list) do
    update_in(map_with_nested_list, [:aa, Access.all()], &do_apply_function/1)
  end

  defp do_apply_function([x, y]) when is_number(x) and is_number(y), do: [x + 3, y + 2]
  # Pay attention that apply_function/1 call here was changed to do_apply_function1
  defp do_apply_function(sublist) when is_list(sublist), do: do_apply_function(sublist)  
  defp do_apply_function(other), do: other 
end

map = %{
  # aa in your example becomes a key in map
  aa: [
    [2, 5.0 , [ 1 , 1 ]],
    [[13.0, 17.0], [25.0, 0.0], [0, 0]] ,
    [ 2 ,2 ]
  ]
}

NiceModule.apply_function(map)

However if in above example we may use the value of a map key as same as sublist, so we can use Map.update!/3 instead.

defmodule NiceModule do
  def apply_function(map_with_nested_list) do
    Map.update!(map_with_nested_list, :aa, fn
      # the difference is that specified map value can be a list with 2 numbers (not nested)
      [x, y] when is_number(x) and is_number(y) -> [x + 3, y + 2]
      sublist when is_list(sublist) -> apply_function(sublist)
      # similar case here - we allow everything else as a map value
      other -> other  
    end)
  end
end

but nested data does not need to be that simple (nested list or map with nested list), but be more complex. Simply try to find the API that do what you expect in simplest form.

In your example you did not support root list to not be nested or be something else as the item variable is the item in the root list and not the root list itself. To do same in my first example we would need to move pattern-matching to apply_function/1 definition, for example:

defmodule NiceModule do
  # the difference is that specified map value can be a list with 2 numbers (not nested)
  def apply_function([x, y]) when is_number(x) and is_number(y), do: [x + 3, y + 2]
  def apply_function(sublist) when is_list(sublist), do: Enum.map(sublist, &apply_function/1)  
  # similar case here - we allow everything else as a map value
  def apply_function(other), do: other 
end
al2o3cr

al2o3cr

+1 for the other suggestion about using Enum.map if you’re doing a simple “loop over every element in a list”

Another small nitpick: if you’re representing coordinates, an {x, y} tuple would usually be preferred over a two-element list [x, y]. It’s very slightly more space-efficient, and very very very slightly faster to access y.

That could also make your recursion simpler, since then the rule is “if it’s a tuple, transform it; otherwise recurse”.

Where Next?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
hariharasudhan94
Lets say i have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => "XXX...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
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
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
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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