by searchableguy · 6y ago · 0 comments
it's easy to write the average function in the article in this way.
def average(arg) when Enum.all(arg, &is_string/1) do
# implementation
end
def average(arg) when Enum.all(arg, &is_integer/1) # for the int
which would be a more proper abstraction as it hides details of the type of your data.or using pattern matching in the arguments.
def shape("circle", ...) do
# implementation
end
def shape("square", ...) do
# different implementation
end
When you have map as an argument, you can do def is_good(%{ hn: HN }) do
IO.puts "#{HN.someprop} is good"
end
def is_good(%{reddit: Reddit}) do
IO.puts "#{Reddit.someprop} is bad"
end
def multiply_on_two_numbers_otherwise_square(a), do: a * a
def multiply_on_two_numbers_otherwise_square(a, b), do: a * b
My examples are trivial but this really gives you some awesome refactoring powers.No comments yet.