newbie1
Hi, I have an array of structs:
array = [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 3, img: 3},
%{count: 1, img: 4}]```
What i want is return the struct with minimum value. if there is more than 1 struct which contains minimum value like “1” it should return all of them. in this case it should return:
array = [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 1, img: 4}]
The check is on “count”
Thanks in advance
Trending in Questions
Hi all,
I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used?
I’ve got two Github accoun...
New
Other Trending Topics
Hey there,
I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first.
...
New
Hi everyone :waving_hand:
I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #umbrella
- #elixirconf-us
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #elixirconf-eu
- #processes
- #security
- #api
- #forms
- #performance










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
A list of maps actually …
There is a
Enum.min_by/3but that returns only a single value. But all these functions are linked to the source so you can find out how that works - it turns out that it is based on -Lists.foldl/3- a function you can use to solve your problem (orEnum.reduce/3).Now both
reduceandfoldlwork on an accumulator value - my choice would be a tuple like(min_value, [maps_with_min_value]). So every time a new element of the list is inspected a decision has to be made:min_value? If yes, start a new list for the new min value resulting in a tuple(new_min_value, [map_with_new_min_value])min_value? If yes, add the new map to the list, i.e.(min_value,[new_map| others])Note also that you should also account for being handed an empty list - i.e. there is no minimum - then the resulting list should simply be empty. But if there are any maps to process in the list you simply take the first map as your initial minimum for the accumulator and process the remainder of the list with
foldlorreduce.PS: With pattern matching you can just use the list as an accumulator (i.e. peek inside the first element of the list rather than holding
min_valueseparately).workin4dmiddleclass
I think peerreynders gave the most professional answer.
You can use this function,
in your case put in your
and
:countas arguments and it should work.I’m sure there’s a more elegant way to solve your problem, this is just what I came up with.
OvermindDL1
If you want it to output in exactly the same output as you stated (so the same order as the original) then I’d just do this:
peerreynders
It’s worth noting that this approach relies on term ordering:
i.e. because
nilis actually:nil(i.e. an atom)is always
trueOvermindDL1
Correct, this is precisely one of the aspects I was using.
/me often compares a number to an atom in such cases, it is very convenient, an old habit from erlang…
I actually originally typed
:infinitethere as that is what I usually do from erlang, butnilI figured is more understandable for Elixir’ians as:infinitecould be deemed as magical when it is not. ^.^jfeng
Another option for your entertainment:
edit Hah, teaches me to skim too quickly, looks like thats pretty much exactly what peerreynders described but without dealing with empty lists. Here’s a better recursive version:
xoron
Here Simple solution not for used just for practice.Suggest any improvement.
Please Confirm that recursion tail call applied and idiomatic code thanks.