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)






















