To paraphrase Rich Hickey--"Simplicity isn't easy". Figuring out those
simple function signatures is not the first thing that comes to mind for
most of us. But it's worth the effort.
On Saturday, May 6, 2017 at 7:14:45 PM UTC-4, Josh Bourgeois wrote:
>
> I thought of practicing, but then I just wound up doing a lot of thinking.
> Then it clicked 😬
>
> defmodule Words do
> def count("" <> string), do: parse(string) |> Enum.reduce(%{}, &count/2)
>
> defp count("", map), do: map
> defp count("" <> word, map) do
> word |> String.replace("_", "-")
> |> increment(map)
> end
>
> defp increment(word, map), do: put_in(map[word], (map[word] || 0) + 1)
> defp parse(string), do: string |> String.downcase |> String.replace("-",
> "&hyph;") |> String.replace("_", "-") |> String.replace("&hyph;", "_") |>
> String.split(~r/\W/u)
> end
>
> It's just a challenge to think of meaningful names sometimes. Breaking
> function/method bodies into smaller discrete blocks of work is obviously a
> fundament to making code more readable, but it takes discipline and,
> sometimes, a thesaurus
>
> Thank you for entertaining this zombie discussion once more 😶
>>
>>
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/459f17fb-88ca-4960-9f89-13dbb3e4c018%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.