1

Iam trying to compute the mode for a list in elixir but for one particular case when iam filtering out the keys from the resulting ranked_map, iam getting \n as output. Here is my code

defmodule Stats.CentralTendency.Mode do



    def mode(nums) when is_list(nums) do
      ranked_map = nums |> Enum.frequencies()

      ranked_map |> Map.values() |> Enum.max() |> mode_func(ranked_map)
    end

    defp mode_func(1, _ranked_map), do: nil
    defp mode_func(max,ranked_map) do
      final_map = ranked_map |> Map.filter(fn {_key,value} -> value == max end)
      Map.keys(final_map)
    end


end

Here is my other code snippet where iam using Stats module to call this particular method

defmodule Stats do
  alias Stats.CentralTendency.{Mean,Median,Mode}

  #def population_mean(nums), do: Mean.population_mean(nums)

  defdelegate population_mean(nums), to: Mean

  defdelegate sample_mean(nums), to: Mean

  defdelegate median(nums_list), to: Median

  defdelegate mode(nums_list), to: Mode
end

Output in terminal

I was expecting [10] for the particular case but i got \n instead

0 Answers0