檢視原始碼 dict (stdlib v6.2)

一個鍵值字典。

字典的表示方式未定義。

此模組提供與 orddict 模組相同的介面。一個差異是,當此模組認為兩個鍵如果它們不匹配 ( =:= ) 則為不同時,orddict 僅在兩個鍵不比較相等 ( == ) 時才認為它們不同。

注意事項

包含 appendappend_list 函數,以便可以將鍵值儲存在列表累加器中,例如

> D0 = dict:new(),
  D1 = dict:store(files, [], D0),
  D2 = dict:append(files, f1, D1),
  D3 = dict:append(files, f2, D2),
  D4 = dict:append(files, f3, D3),
  dict:fetch(files, D4).
[f1,f2,f3]

這省去了先獲取鍵值、將新值附加到儲存值列表以及儲存結果的麻煩。

如果已知鍵在字典中,則使用 fetch 函數,否則使用 find 函數。

另請參閱

gb_treesorddict

摘要

類型

new/0 返回的字典。

函數

將新的 Value 附加到與 Key 關聯的當前值列表。

將值列表 ValList 附加到與 Key 關聯的當前值列表。如果與 Key 關聯的初始值不是值列表,則會產生異常。

從字典中刪除具有給定鍵的所有項目。

返回字典 Dict 中與 Key 關聯的值。此函數假定 Key 存在於字典 Dict 中,如果 Key 不在字典中,則會產生異常。

返回字典 Dict 中所有鍵的列表。

Dict2 是一個字典,包含 Dict1 中所有使 Pred(Key, Value)true 的鍵和值。

在字典 Dict 中搜尋鍵。返回 {ok, Value},其中 Value 是與 Key 關聯的值,如果該鍵不存在於字典中,則返回 error

在字典 Dict 的連續鍵和值上調用 Fun,並帶有一個額外參數 Acc (累加器的縮寫)。Fun 必須返回一個新的累加器,該累加器會傳遞給下一次調用。如果字典為空,則返回 Acc0。評估順序未定義。

Key-Value 列表 List 轉換為字典 Dict

如果字典 Dict 沒有元素,則返回 true,否則返回 false

測試 Key 是否包含在字典 Dict 中。

在字典 Dict1 的連續鍵和值上調用 Fun,以返回每個鍵的新值。評估順序未定義。

合併兩個字典 Dict1Dict2,以建立新的字典。兩個字典中的所有 Key-Value 對都包含在新的字典中。如果一個鍵同時出現在兩個字典中,則會使用該鍵和兩個值調用 Fun,以返回新值。merge 可以定義如下,但更快

建立新的字典。

返回字典 Dict 中的元素數。

Key-Value 對儲存在字典 Dict2 中。如果 Key 已經存在於 Dict1 中,則關聯的值會被 Value 取代。

此函數從字典中返回一個值,並返回一個不含此值的新字典。如果該鍵不存在於字典中,則返回 error

將字典 Dict 轉換為列表表示法。

通過在值上調用 Fun 來獲取新值,從而更新字典中的值。如果 Key 不存在於字典中,則會產生異常。

通過在值上調用 Fun 來獲取新值,從而更新字典中的值。如果 Key 不存在於字典中,則 Initial 會儲存為第一個值。例如,append/3 可以定義為

Increment 加到與 Key 關聯的值,並儲存此值。如果 Key 不存在於字典中,則 Increment 會儲存為第一個值。

類型

-type dict() :: dict(_, _).
-opaque dict(Key, Value)

new/0 返回的字典。

函數

連結到此函數

append(Key, Value, Dict1)

檢視原始碼
-spec append(Key, Value, Dict1) -> Dict2 when Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).

將新的 Value 附加到與 Key 關聯的當前值列表。

另請參閱 注意事項 一節。

連結到此函數

append_list(Key, ValList, Dict1)

檢視原始碼
-spec append_list(Key, ValList, Dict1) -> Dict2
                     when Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value), ValList :: [Value].

將值列表 ValList 附加到與 Key 關聯的當前值列表。如果與 Key 關聯的初始值不是值列表,則會產生異常。

另請參閱 注意事項 一節。

-spec erase(Key, Dict1) -> Dict2 when Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).

從字典中刪除具有給定鍵的所有項目。

-spec fetch(Key, Dict) -> Value when Dict :: dict(Key, Value).

返回字典 Dict 中與 Key 關聯的值。此函數假定 Key 存在於字典 Dict 中,如果 Key 不在字典中,則會產生異常。

另請參閱 注意事項 一節。

-spec fetch_keys(Dict) -> Keys when Dict :: dict(Key, Value :: term()), Keys :: [Key].

返回字典 Dict 中所有鍵的列表。

-spec filter(Pred, Dict1) -> Dict2
                when
                    Pred :: fun((Key, Value) -> boolean()),
                    Dict1 :: dict(Key, Value),
                    Dict2 :: dict(Key, Value).

Dict2 是一個字典,包含 Dict1 中所有使 Pred(Key, Value)true 的鍵和值。

-spec find(Key, Dict) -> {ok, Value} | error when Dict :: dict(Key, Value).

在字典 Dict 中搜尋鍵。返回 {ok, Value},其中 Value 是與 Key 關聯的值,如果該鍵不存在於字典中,則返回 error

另請參閱 注意事項 一節。

-spec fold(Fun, Acc0, Dict) -> Acc1
              when
                  Fun :: fun((Key, Value, AccIn) -> AccOut),
                  Dict :: dict(Key, Value),
                  Acc0 :: Acc,
                  Acc1 :: Acc,
                  AccIn :: Acc,
                  AccOut :: Acc.

在字典 Dict 的連續鍵和值上調用 Fun,並帶有一個額外參數 Acc (累加器的縮寫)。Fun 必須返回一個新的累加器,該累加器會傳遞給下一次調用。如果字典為空,則返回 Acc0。評估順序未定義。

-spec from_list(List) -> Dict when Dict :: dict(Key, Value), List :: [{Key, Value}].

Key-Value 列表 List 轉換為字典 Dict

連結到此函數

is_empty(Dict)

檢視原始碼 (自 OTP 17.0 起)
-spec is_empty(Dict) -> boolean() when Dict :: dict().

如果字典 Dict 沒有元素,則返回 true,否則返回 false

-spec is_key(Key, Dict) -> boolean() when Dict :: dict(Key, Value :: term()).

測試 Key 是否包含在字典 Dict 中。

-spec map(Fun, Dict1) -> Dict2
             when
                 Fun :: fun((Key, Value1) -> Value2),
                 Dict1 :: dict(Key, Value1),
                 Dict2 :: dict(Key, Value2).

在字典 Dict1 的連續鍵和值上調用 Fun,以返回每個鍵的新值。評估順序未定義。

連結到此函數

merge(Fun, Dict1, Dict2)

檢視原始碼
-spec merge(Fun, Dict1, Dict2) -> Dict3
               when
                   Fun :: fun((Key, Value1, Value2) -> Value),
                   Dict1 :: dict(Key, Value1),
                   Dict2 :: dict(Key, Value2),
                   Dict3 :: dict(Key, Value).

合併兩個字典 Dict1Dict2,以建立新的字典。兩個字典中的所有 Key-Value 對都包含在新的字典中。如果一個鍵同時出現在兩個字典中,則會使用該鍵和兩個值調用 Fun,以返回新值。merge 可以定義如下,但更快

merge(Fun, D1, D2) ->
    fold(fun (K, V1, D) ->
                 update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D)
         end, D2, D1).
-spec new() -> dict().

建立新的字典。

-spec size(Dict) -> non_neg_integer() when Dict :: dict().

返回字典 Dict 中的元素數。

連結到此函數

store(Key, Value, Dict1)

檢視原始碼
-spec store(Key, Value, Dict1) -> Dict2 when Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value).

Key-Value 對儲存在字典 Dict2 中。如果 Key 已經存在於 Dict1 中,則關聯的值會被 Value 取代。

連結到此函數

take(Key, Dict)

檢視原始碼 (自 OTP 20.0 起)
-spec take(Key, Dict) -> {Value, Dict1} | error
              when Dict :: dict(Key, Value), Dict1 :: dict(Key, Value), Key :: term(), Value :: term().

此函數從字典中返回一個值,並返回一個不含此值的新字典。如果該鍵不存在於字典中,則返回 error

-spec to_list(Dict) -> List when Dict :: dict(Key, Value), List :: [{Key, Value}].

將字典 Dict 轉換為列表表示法。

連結到此函數

update(Key, Fun, Dict1)

檢視原始碼
-spec update(Key, Fun, Dict1) -> Dict2
                when
                    Dict1 :: dict(Key, Value),
                    Dict2 :: dict(Key, Value),
                    Fun :: fun((Value1 :: Value) -> Value2 :: Value).

通過在值上調用 Fun 來獲取新值,從而更新字典中的值。如果 Key 不存在於字典中,則會產生異常。

連結到此函數

update(Key, Fun, Initial, Dict1)

檢視原始碼
-spec update(Key, Fun, Initial, Dict1) -> Dict2
                when
                    Dict1 :: dict(Key, Value),
                    Dict2 :: dict(Key, Value),
                    Fun :: fun((Value1 :: Value) -> Value2 :: Value),
                    Initial :: Value.

通過在值上調用 Fun 來獲取新值,從而更新字典中的值。如果 Key 不存在於字典中,則 Initial 會儲存為第一個值。例如,append/3 可以定義為

append(Key, Val, D) ->
    update(Key, fun (Old) -> Old ++ [Val] end, [Val], D).
連結到此函數

update_counter(Key, Increment, Dict1)

檢視原始碼
-spec update_counter(Key, Increment, Dict1) -> Dict2
                        when Dict1 :: dict(Key, Value), Dict2 :: dict(Key, Value), Increment :: number().

Increment 加到與 Key 關聯的值,並儲存此值。如果 Key 不存在於字典中,則 Increment 會儲存為第一個值。

可以定義如下,但更快

update_counter(Key, Incr, D) ->
    update(Key, fun (Old) -> Old + Incr end, Incr, D).