newbie1

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

Showing Posts 1 to 7

peerreynders

peerreynders

A list of maps actually …

There is a Enum.min_by/3 but 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 (or Enum.reduce/3).

Now both reduce and foldl work 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:

  • Is this lower than 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])
  • Is this equal to min_value? If yes, add the new map to the list, i.e. (min_value,[new_map| others])
  • Otherwise leave the tuple unchanged.

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 foldl or reduce.

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_value separately).

workin4dmiddleclass

workin4dmiddleclass

I think peerreynders gave the most professional answer.

You can use this function,

  def getmin(maplst, key) do
    m = maplst |> Enum.map(fn(x) -> Map.get(x, key) end) |> Enum.min
    Enum.filter(maplst, fn(x) -> Map.get(x, key) == m end)
  end

in your case put in your

array = [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 3, img: 3}, %{count: 1, img: 4}]

and :count as 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

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:

╰─➤  iex
Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:2:2] [ds:2:2:10] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.6.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> array = [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 3, img: 3}, %{count: 1, img: 4}]
[
  %{count: 1, img: 1},
  %{count: 1, img: 2},
  %{count: 3, img: 3},
  %{count: 1, img: 4}
]
iex(2)> {_, array} = Enum.reduce(Enum.reverse(array), {nil, []}, fn
...(2)>   %{count: count} = v, {c, a} when count < c -> {count, [v]}
...(2)>   %{count: count} = v, {count, a} -> {count, [v | a]}
...(2)>   _, acc -> acc
...(2)> end)
{1, [%{count: 1, img: 1}, %{count: 1, img: 2}, %{count: 1, img: 4}]}

peerreynders

peerreynders

It’s worth noting that this approach relies on term ordering:

number < atom < reference < function < port < pid < tuple < map < list < bitstring

i.e. because nil is actually :nil (i.e. an atom)

number < nil

is always true

OvermindDL1

OvermindDL1

Correct, this is precisely one of the aspects I was using. :slight_smile:

/me often compares a number to an atom in such cases, it is very convenient, an old habit from erlang…

I actually originally typed :infinite there as that is what I usually do from erlang, but nil I figured is more understandable for Elixir’ians as :infinite could be deemed as magical when it is not. ^.^

jfeng

jfeng

Another option for your entertainment:

def min_values([head | tail]) do
  acc_min = fn(x, [a | _] = acc) ->
    cond do
      x.count == a.count -> [x | acc]
      x.count > a.count -> acc
      x.count < a.count -> [x]
    end
  end

  List.foldl(tail, [head], acc_min) |> Enum.reverse()
end

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:

def min_values(list, acc \\ [])
def min_values([head | tail], []), do: min_values(tail, [head])
def min_values([head | tail], [%{count: val} | _] = acc) do
  cond do
    head.count == val -> min_values(tail, [head | acc])
    head.count > val -> min_values(tail, acc)
    head.count < val -> min_values(tail, [head])
  end
end
def min_values([], acc), do: Enum.reverse(acc)
xoron

xoron

Here Simple solution not for used just for practice.Suggest any improvement.
Please Confirm that recursion tail call applied and idiomatic code thanks.

defmodule ListOperator do
  def findValue([head | tail]) do
    minValues(tail, [head])
  end

  defp minValues([%{count: count} | tail], [%{count: min_count} | _tail] = main_head)
       when count > min_count do
    minValues(tail, main_head)
  end

  defp minValues([%{count: count} = head | tail], [%{count: min_count} | _tail])
       when count < min_count do
    minValues(tail, [head])
  end

  defp minValues([%{count: count} = head | tail], [%{count: min_count} | _tail] = main_head)
       when count == min_count do
    minValues(tail, [head | main_head])
  end

  defp minValues([], value), do: value
end

array = [
  %{count: 4, img: 1},
  %{count: 8, img: 2},
  %{count: 3, img: 3},
  %{count: 5, img: 4},
  %{count: 5, img: 1},
  %{count: 7, img: 2},
  %{count: 3, img: 3},
  %{count: 9, img: 4},
  %{count: 4, img: 1},
  %{count: 8, img: 2},
  %{count: 3, img: 3},
  %{count: 5, img: 4},
  %{count: 5, img: 1},
  %{count: 7, img: 2},
  %{count: 3, img: 3},
  %{count: 9, img: 4}
]

IO.inspect(ListOperator.findValue(array))

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
Blokh
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
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
kszambelanczyk
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
Onor.io
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
jaybe78
Hello, I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter). The diffic...
New
Trolleger
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

Other Trending Topics Top

garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews