Карта сопоставления моделей с дополнительными ключами
У меня есть функция, которая получает карту со многими ключами, некоторые из них являются необязательными. Как я могу написать подпись функции, понимающую карту, позволяя необязательным клавишам по умолчанию что-то?
def handle_my_map(%{text: text,
print_times: print_times, # this I want to default to 2
color: color # this I want to default to "Blue"
}) do
Enum.each(1..print_times, fn (_) -> IO.puts ["(", color, "): ", text] end)
end
Test.handle_my_map(%{text: "here", print_times: 5, color: "Red"})
# (Red): here
# (Red): here
# (Red): here
# (Red): here
# (Red): here
handle_my_map(%{text: "there"})
# => MatchError!
Я бы хотел:
handle_my_map(%{text: "where", print_times: 3})
# (Blue): where
# (Blue): where
# (Blue): where
handle_my_map(%{text: "there"})
# (Blue): there
# (Blue): there
Что-то вроде аргументов ключевого слова ruby:
def handle_my_map(text: nil, print_times: 2, color: 'Blue')
Ответы
Ответ 1
Вы можете использовать Map.merge/2
:
defmodule Handler do
@defaults %{print_times: 2, color: "Blue"}
def handle_my_map(map) do
%{text: text, print_times: times, color: color} = merge_defaults(map)
Enum.each(1..times, fn (_) -> IO.puts ["(", color, "): ", text] end)
end
defp merge_defaults(map) do
Map.merge(@defaults, map)
end
end
Если вы хотите разрешить nils, вы можете использовать Map.merge/3
и изменить merge_defaults/1
на:
defp merge_defaults(map) do
Map.merge(@defaults, map, fn _key, default, val -> val || default end)
end
Ответ 2
Вероятно, я бы сказал что-то вроде этого:
defmodule Handler do
@defaults %{print_times: 2, color: "Blue"}
def handle_my_map(map) do
%{text: text, print_times: times, color: color} = Dict.put_new(map, @defaults)
Enum.each(1..times, fn (_) -> IO.puts ["(", color, "): ", text] end)
end
end
Если вам нужно обрабатывать значения nil
с помощью существующих ключей, вы можете сделать:
defmodule Handler do
@defaults %{print_times: 2, color: "Blue"}
def handle_my_map(map) do
%{text: text, print_times: times, color: color} = @defaults
|> Dict.merge(map)
|> Enum.into %{}, fn
{key, nil} -> {key, @defaults[key]}
{key, val} -> {key, val}
end
Enum.each(1..times, fn (_) -> IO.puts ["(", color, "): ", text] end)
end
end
Ответ 3
Ну. Я думаю, вам нужно написать еще одну функцию handle_my_map
с аргументом %{a: a, b: b}
. Вот так:
def handle_my_map(%{a: a, b: b, optional_c: c}) do
a + b + c
end
def handle_my_map(%{a: a, b: b}) do
a + b
end
YourModule.handle_my_map %{a: 1, b: 2}
#=> 3
YourModule.handle_my_map %{a: 1, b: 2, optional_c: 3}
#=> 6
Elixir будет искать функцию handle_my_map
, которая соответствует вашим аргументам до тех пор, пока функции с arity 1 end