As far as I understand how Elixir works, using Map makes the code DRYer, but it doesn't leverage Elixir and BEAM optimization process.
With @p1_wins_table[p1] Map is accessed during runtime, where def ("scissors"), do:... are optimized during compilation and there's no need to lookup for proper values in the Map on runtime. For the sake of this kata, this may seem irrelevant but may play a significant role in real-world use cases.
Beware! You should not convert unknown strings to atoms. Atoms are not garbage collected, so you could end with a memory leak or a security vulnerability
As far as I understand how Elixir works, using Map makes the code DRYer, but it doesn't leverage Elixir and BEAM optimization process.
With
@p1_wins_table[p1]
Map is accessed during runtime, wheredef ("scissors"), do:...
are optimized during compilation and there's no need to lookup for proper values in the Map on runtime. For the sake of this kata, this may seem irrelevant but may play a significant role in real-world use cases.More info: https://medium.com/@amuino/maps-vs-pattern-matching-in-elixir-e69b7bb11b5d
Thank you for this info.
If you want, you can do this in one line
Would be DRYer to use a Map instead of defining 3 helper functions
This comment is hidden because it contains spoiler information about the solution
Beware! You should not convert unknown strings to atoms. Atoms are not garbage collected, so you could end with a memory leak or a security vulnerability
FYI - You can split using a regex rather than doing a replace + split.