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 ![]()
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #elixirconf-us
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 5- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
If that’s all you need then why not simply use
Enum.map/2instead?Creating a tuple that is not needed is just waste of time and resources.
This code is 40% (6 of 16 LOC) shorter and therefore definitely more readable.
afhuertass
Thanks!
I think this helps clarify things. The
fun_funfunction 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
Both of course are valid - they are equivalents in this example.
Enum.map/1is just much simpler here. You useupdate_in/3together withAccess.all/0in case you have to update a nested (non-recursive) structure, for example:However if in above example we may use the value of a map key as same as sublist, so we can use
Map.update!/3instead.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
itemvariable 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 toapply_function/1definition, for example:al2o3cr
+1 for the other suggestion about using
Enum.mapif 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 accessy.That could also make your recursion simpler, since then the rule is “if it’s a tuple, transform it; otherwise recurse”.
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.
or
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)
Elixiritself gives you freedom. Even if something is calledframeworkin our ecosystem it’s still library. For example you can storeSome.Modulewhere 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).