afhuertass

afhuertass

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:

Showing Posts 1 to 5

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.

afhuertass

afhuertass OP

Thanks!

I think this helps clarify things. The fun_fun function was just an example, as what I need is a more complex function. But your clarification helps. If I may ask another question, what might be the use case of Access.all vs Enum.map ?

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”.

Eiji

Eiji

Agree, but there is no “standard” for storing coordinates. In some cases we expect map, struct or tuple - from all of those tuple is indeed the fastest. Anyway I agree that list is used very rarely for such a use case. I did not proposed this as we have no idea from where input comes as in some cases it’s not up to developer to decide about input format.

defmodule NiceModule do
  # skip function guards as we expect every 2-element tuple to always contain numbers
  def apply_function({x, y}), do: {x + 3, y + 2}
  def apply_function(list) when is_list(list), do: Enum.map(list, &apply_function/1)
  def apply_function(other), do: other 
end

or

defmodule NiceModule do
  def apply_function(nested_list) do
    Enum.map(nested_list, fn
      # skip function guards as we expect every 2-element tuple to always contain numbers
      {x, y} -> {x + 3, y + 2}
      sublist when is_list(sublist) -> apply_function(sublist)  
      other -> other  
    end)
  end
end

in case we require list as input.

Going back to topic there is no such thing as “anti-Elixir”. While following so-called community standards or common patterns is much better seen (especially when applying for job) Elixir itself gives you freedom. Even if something is called framework in our ecosystem it’s still library. For example you can store Some.Module where you want - there is no fixed naming and compilation errors. There is no magic check happening in background (except helpers like type checking which simply checks if the code is going to fail).

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews