Bazı tuşları isteğe bağlı olan bir çok tuşa sahip bir harita alan bir fonksiyonum var. İsteğe bağlı anahtarların bir şeylere varsayılan olarak izin verirken haritayı anlama işlev imzasını nasıl yazabilirim?İsteğe bağlı tuşları ile desen eşleştirme haritası
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!
ben olmak istiyorum: Ruby'nin anahtar kelime argümanlar gibi
handle_my_map(%{text: "where", print_times: 3})
# (Blue): where
# (Blue): where
# (Blue): where
handle_my_map(%{text: "there"})
# (Blue): there
# (Blue): there
şey:
def handle_my_map(text: nil, print_times: 2, color: 'Blue')
bu 'handle_my_map/1' bir parametre alır? –
@ Зелёный - evet, tek bir harita parametresi –