當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Elixir Map.get_and_update!用法及代碼示例

Elixir語言中 Map.get_and_update! 相關用法介紹如下。

用法:

get_and_update!(map, key, fun)
@spec get_and_update!(
  map(),
  key(),
  (value() | nil -> {current_value, new_value :: value()} | :pop)
) ::
  {current_value, map()}
when current_value: value()

key 獲取值並更新它,一次完成。如果沒有 key 則引發。

行為與 get_and_update/3 完全相同,但如果 keymap 中不存在,則會引發 KeyError 異常。

例子

iex> Map.get_and_update!(%{a: 1}, :a, fn current_value ->
...>   {current_value, "new value!"}
...> end)
{1, %{a: "new value!"}}

iex> Map.get_and_update!(%{a: 1}, :b, fn current_value ->
...>   {current_value, "new value!"}
...> end)
** (KeyError) key :b not found in: %{a: 1}

iex> Map.get_and_update!(%{a: 1}, :a, fn _ ->
...>   :pop
...> end)
{1, %{}}

相關用法


注:本文由純淨天空篩選整理自elixir-lang.org大神的英文原創作品 Map.get_and_update!(map, key, fun)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。