当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Elixir Keyword.update!用法及代码示例

Elixir语言中 Keyword.update! 相关用法介绍如下。

用法:

update!(keywords, key, fun)
@spec update!(t(), key(), (current_value :: value() -> new_value :: value())) :: t()

使用给定函数更新 key 下的值。

如果 key 不存在,则引发 KeyError

删除所有重复的键,只更新第一个。

例子

iex> Keyword.update!([a: 1, b: 2, a: 3], :a, &(&1 * 2))
[a: 2, b: 2]
iex> Keyword.update!([a: 1, b: 2, c: 3], :b, &(&1 * 2))
[a: 1, b: 4, c: 3]

iex> Keyword.update!([a: 1], :b, &(&1 * 2))
** (KeyError) key :b not found in: [a: 1]

相关用法


注:本文由纯净天空筛选整理自elixir-lang.org大神的英文原创作品 Keyword.update!(keywords, key, fun)。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。