defmodule MedicalTranscription.Utilities do | |
@moduledoc """ | |
Contains general utility functions that are not specific to a domain or context in the system. | |
""" | |
@doc "Takes an enumerable of atoms and tallies a count for how often each appears." | |
def tally(enumerable) do | |
Enum.reduce(enumerable, %{}, fn item, acc -> | |
if Map.has_key?(acc, item) do | |
Map.put(acc, item, acc[item] + 1) | |
else | |
Map.put(acc, item, 1) | |
end | |
end) | |
end | |
@doc "Takes the output of `tally/1` and returns a string representation." | |
def tally_to_string(tallied_enumerable) do | |
tallied_enumerable | |
|> Enum.map_join(", ", fn {key, value} -> "#{key} (#{value})" end) | |
end | |
def map_set_toggle(map_set, item) do | |
if Enum.any?(map_set, &(&1 == item)) do | |
Enum.reject(map_set, &(&1 == item)) | |
else | |
map_set ++ [item] | |
end | |
end | |
end | |