What is the benefit of Keyword Lists?

后端 未结 3 1432
失恋的感觉
失恋的感觉 2020-12-12 12:01

In elixir we have Maps:

> map = %{:a => \"one\", :b => \"two\"} # = %{a: \"one\", b: \"two\"}
> map.a                             # = \"one\"
>         


        
3条回答
  •  囚心锁ツ
    2020-12-12 12:41

    Maps allow only one entry for a particular key, whereas keyword lists allow the key to be repeated. Maps are efficient (particularly as they grow), and they can be used in Elixir’s pattern matching.

    In general, use keyword lists for things such as command-line parameters and for passing around options, and use maps (or another data structure, the HashDict ) when you want an associative array.

提交回复
热议问题