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:
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).
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 :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. ^.^
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)
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))