मेरे पास एक ऐसा कार्य है जो कई चाबियों वाला नक्शा प्राप्त करता है, उनमें से कुछ वैकल्पिक हैं। वैकल्पिक कुंजी को कुछ डिफ़ॉल्ट करने की अनुमति देते हुए मैं मानचित्र को समझने के फ़ंक्शन हस्ताक्षर कैसे लिख सकता हूं?वैकल्पिक कुंजी के साथ पैटर्न मिलान मानचित्र
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
कुछ माणिक के खोजशब्द तर्क की तरह:
def handle_my_map(text: nil, print_times: 2, color: 'Blue')
इस 'handle_my_map/1' एक पैरामीटर लेता है? –
@ Зелёный - हाँ, एक नक्शा पैरामीटर –