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

屬性列表的支援函式。

屬性列表是普通的列表,其中包含的條目可以是元組,元組的第一個元素是用於查找和插入的鍵;也可以是原子,原子是元組 {Atom, true} 的簡寫。(列表中允許其他詞彙,但此模組會忽略它們。)如果列表中針對某個鍵有多個條目,通常第一個出現的條目會覆蓋後面的條目(無論元組的arity如何)。

屬性列表適用於表示繼承的屬性,例如傳遞給函式的選項,使用者可以在其中指定覆蓋預設設定的選項、物件屬性、註解等等。

如果兩個鍵匹配 (=:=),則視為相等。也就是說,數字會按字面比較,而不是按值比較,因此,例如,11.0 是不同的鍵。

摘要

類型

列表中的屬性項目

property/0 的列表,也稱為 proplist。

函式

類似於 get_all_values/2,但每個值都會包裝在列表中,除非它本身已是列表。產生的列表會被串接。這對於「遞增」選項通常很有用。

最小化列表中所有條目的表示形式。這等同於 [property(P) || P <- ListIn]

List 中刪除與 Key 相關聯的所有條目。

將特定的屬性擴展為對應的屬性集(或其他詞彙)。

將 map Map 轉換為屬性列表。

類似於 get_value/2,但會傳回 List所有條目 {Key, Value} 的值列表。如果沒有這樣的條目,則結果為空列表。

傳回布林值鍵/值選項的值。如果 lookup(Key, List) 會產生 {Key, true},則此函式會傳回 true,否則傳回 false

傳回 List 中使用的鍵的無序列表,不包含重複項。

傳回 List 中簡單鍵/值屬性的值。如果 lookup(Key, List) 會產生 {Key, Value},則此函式會傳回對應的 Value,否則傳回 Default

如果 List 包含至少一個與 Key 相關聯的條目,則傳回 true,否則傳回 false

如果 List 中存在與 Key 相關聯的第一個條目,則傳回該條目,否則傳回 none。對於列表中的原子 A,元組 {A, true} 是與 A 相關聯的條目。

傳回 List 中與 Key 相關聯的所有條目的列表。如果沒有這樣的條目,則結果為空列表。

透過一系列替換/擴展階段傳遞 ListIn。對於 aliases 操作,會使用指定的別名列表套用函式 substitute_aliases/2

建立屬性的正規化形式(最小)表示形式。如果 PropertyIn{Key, true},其中 Key 為原子,則傳回 Key,否則傳回整個詞彙 PropertyIn

建立簡單鍵/值屬性的正規化形式(最小)表示形式。如果 ValuetrueKey 為原子,則傳回 Key,否則傳回元組 {Key, Value}

List 分割為子列表和剩餘部分的列表。

替換屬性的鍵。對於 ListIn 中的每個條目,如果它與某個鍵 K1 相關聯,且 Aliases 中出現 {K1, K2},則條目的鍵會變更為 K2。如果同一個 K1Aliases 中出現多次,則僅使用第一次出現。

替換布林值屬性的鍵,並同時否定其值。

將屬性列表 List 轉換為 map。

在套用 Stages 中給定的正規化後,將屬性列表 List 轉換為 map。

ListIn 中所有出現的原子展開為元組 {Atom, true}

類型

-type property() :: atom() | tuple().

列表中的屬性項目

-type proplist() :: [property()].

property/0 的列表,也稱為 proplist。

函式

連結至此函式

append_values(Key, ListIn)

檢視原始碼
-spec append_values(Key, ListIn) -> ListOut when Key :: term(), ListIn :: [term()], ListOut :: [term()].

類似於 get_all_values/2,但每個值都會包裝在列表中,除非它本身已是列表。產生的列表會被串接。這對於「遞增」選項通常很有用。

範例

append_values(a, [{a, [1,2]}, {b, 0}, {a, 3}, {c, -1}, {a, [4]}])

傳回

[1,2,3,4]
-spec compact(ListIn) -> ListOut when ListIn :: [property()], ListOut :: [property()].

最小化列表中所有條目的表示形式。這等同於 [property(P) || P <- ListIn]

另請參閱 property/1unfold/1

-spec delete(Key, List) -> List when Key :: term(), List :: [term()].

List 中刪除與 Key 相關聯的所有條目。

連結至此函式

expand(Expansions, ListIn)

檢視原始碼
-spec expand(Expansions, ListIn) -> ListOut
                when
                    Expansions :: [{Property :: property(), Expansion :: [term()]}],
                    ListIn :: [term()],
                    ListOut :: [term()].

將特定的屬性擴展為對應的屬性集(或其他詞彙)。

對於 Expansions 中的每個配對 {Property, Expansion}:如果 EListIn 中與 Property 具有相同鍵的第一個條目,且 EProperty 具有等效的正規化形式,則 E 會被 Expansion 中的詞彙取代,並且會從 ListIn 中刪除任何後續具有相同鍵的條目。

例如,以下表達式都會傳回 [fie, bar, baz, fum]

expand([{foo, [bar, baz]}], [fie, foo, fum])
expand([{{foo, true}, [bar, baz]}], [fie, foo, fum])
expand([{{foo, false}, [bar, baz]}], [fie, {foo, false}, fum])

但是,在以下呼叫中不會進行任何擴展,因為 {foo, false} 遮蔽了 foo

expand([{{foo, true}, [bar, baz]}], [{foo, false}, fie, foo, fum])

請注意,如果要將原始屬性詞彙保留在擴展時的結果中,則必須包含在擴展列表中。插入的詞彙不會遞迴擴展。如果 Expansions 包含多個具有相同鍵的屬性,則僅使用第一個出現的屬性。

另請參閱 normalize/2

連結至此函式

from_map(Map)

檢視原始碼 (自 OTP 24.0 起)
-spec from_map(Map) -> List
                  when Map :: #{Key => Value}, List :: [{Key, Value}], Key :: term(), Value :: term().

將 map Map 轉換為屬性列表。

連結至此函式

get_all_values(Key, List)

檢視原始碼
-spec get_all_values(Key, List) -> [term()] when Key :: term(), List :: [term()].

類似於 get_value/2,但會傳回 List所有條目 {Key, Value} 的值列表。如果沒有這樣的條目,則結果為空列表。

-spec get_bool(Key, List) -> boolean() when Key :: term(), List :: [term()].

傳回布林值鍵/值選項的值。如果 lookup(Key, List) 會產生 {Key, true},則此函式會傳回 true,否則傳回 false

另請參閱 get_value/2lookup/2

-spec get_keys(List) -> [term()] when List :: [term()].

傳回 List 中使用的鍵的無序列表,不包含重複項。

-spec get_value(Key, List) -> term() when Key :: term(), List :: [term()].

等同於 get_value(Key, List, undefined)

連結至此函式

get_value(Key, List, Default)

檢視原始碼
-spec get_value(Key, List, Default) -> term() when Key :: term(), List :: [term()], Default :: term().

傳回 List 中簡單鍵/值屬性的值。如果 lookup(Key, List) 會產生 {Key, Value},則此函式會傳回對應的 Value,否則傳回 Default

另請參閱 get_all_values/2get_bool/2get_value/2lookup/2

-spec is_defined(Key, List) -> boolean() when Key :: term(), List :: [term()].

如果 List 包含至少一個與 Key 相關聯的條目,則傳回 true,否則傳回 false

-spec lookup(Key, List) -> none | tuple() when Key :: term(), List :: [term()].

如果 List 中存在與 Key 相關聯的第一個條目,則傳回該條目,否則傳回 none。對於列表中的原子 A,元組 {A, true} 是與 A 相關聯的條目。

另請參閱 get_bool/2get_value/2lookup_all/2

-spec lookup_all(Key, List) -> [tuple()] when Key :: term(), List :: [term()].

傳回 List 中與 Key 相關聯的所有條目的列表。如果沒有這樣的條目,則結果為空列表。

另請參閱 lookup/2

連結至此函式

normalize(ListIn, Stages)

檢視原始碼
-spec normalize(ListIn, Stages) -> ListOut
                   when
                       ListIn :: [term()],
                       Stages :: [Operation],
                       Operation :: {aliases, Aliases} | {negations, Negations} | {expand, Expansions},
                       Aliases :: [{Key, Key}],
                       Negations :: [{Key, Key}],
                       Expansions :: [{Property :: property(), Expansion :: [term()]}],
                       ListOut :: [term()].

透過一系列替換/擴展階段傳遞 ListIn。對於 aliases 操作,會使用指定的別名列表套用函式 substitute_aliases/2

  • 對於 negations 操作,會使用指定的否定列表套用 substitute_negations/2
  • 對於 expand 操作,會使用指定的擴展列表套用函式 expand/2

最終結果會自動壓縮(比較 compact/1)。

通常,您會想要先替換否定,然後替換別名,然後執行一個或多個擴展(有時您會想要在進行主要擴展之前預先擴展特定的條目)。您可能想要重複替換否定和/或別名,以允許在別名和擴展列表的右側使用這些形式。

另請參閱 substitute_negations/2

-spec property(PropertyIn) -> PropertyOut when PropertyIn :: property(), PropertyOut :: property().

建立屬性的正規化形式(最小)表示形式。如果 PropertyIn{Key, true},其中 Key 為原子,則傳回 Key,否則傳回整個詞彙 PropertyIn

另請參閱 property/2

-spec property(Key, Value) -> Property
                  when Key :: term(), Value :: term(), Property :: atom() | {term(), term()}.

建立簡單鍵/值屬性的正規化形式(最小)表示形式。如果 ValuetrueKey 為原子,則傳回 Key,否則傳回元組 {Key, Value}

另請參閱 property/1

-spec split(List, Keys) -> {Lists, Rest}
               when List :: [term()], Keys :: [term()], Lists :: [[term()]], Rest :: [term()].

List 分割為子列表和剩餘部分的列表。

Lists 包含 Keys 中每個鍵的子列表,順序對應。每個子列表中元素的相對順序會從原始 List 中保留。Rest 包含 List 中未與任何指定鍵相關聯的元素,同時也會保留其原始相對順序。

範例

split([{c, 2}, {e, 1}, a, {c, 3, 4}, d, {b, 5}, b], [a, b, c])

傳回

{[[a], [{b, 5}, b],[{c, 2}, {c, 3, 4}]], [{e, 1}, d]}
連結至此函式

substitute_aliases(Aliases, ListIn)

檢視原始碼
-spec substitute_aliases(Aliases, ListIn) -> ListOut
                            when
                                Aliases :: [{Key, Key}],
                                Key :: term(),
                                ListIn :: [term()],
                                ListOut :: [term()].

替換屬性的鍵。對於 ListIn 中的每個條目,如果它與某個鍵 K1 相關聯,且 Aliases 中出現 {K1, K2},則條目的鍵會變更為 K2。如果同一個 K1Aliases 中出現多次,則僅使用第一次出現。

例如,substitute_aliases([{color, colour}], L) 會將 L 中的所有元組 {color, ...} 替換為 {colour, ...},並將所有原子 color 替換為 colour

另請參閱 normalize/2substitute_negations/2

連結至此函式

substitute_negations(Negations, ListIn)

檢視原始碼
-spec substitute_negations(Negations, ListIn) -> ListOut
                              when
                                  Negations :: [{Key1, Key2}],
                                  Key1 :: term(),
                                  Key2 :: term(),
                                  ListIn :: [term()],
                                  ListOut :: [term()].

替換布林值屬性的鍵,並同時否定其值。

對於 ListIn 中的每個條目,如果它與某個鍵 K1 相關聯,且 Negations 中出現 {K1, K2}:如果條目為 {K1, true},則會被 {K2, false} 取代,否則會被 K2 取代,從而變更選項的名稱,並同時否定 get_bool(Key, ListIn) 指定的值。如果同一個 K1Negations 中出現多次,則僅使用第一次出現。

例如,substitute_negations([{no_foo, foo}], L) 會將 L 中的任何原子 no_foo 或元組 {no_foo, true} 替換為 {foo, false},並將任何其他元組 {no_foo, ...} 替換為 foo

另請參閱 get_bool/2normalize/2substitute_aliases/2

連結至此函式

to_map(List)

檢視原始碼 (自 OTP 24.0 起)
-spec to_map(List) -> Map
                when
                    List :: [Shorthand | {Key, Value} | term()],
                    Map :: #{Shorthand => true, Key => Value},
                    Shorthand :: atom(),
                    Key :: term(),
                    Value :: term().

將屬性列表 List 轉換為 map。

List 中的簡寫原子值將會被展開為 Atom => true 形式的關聯。在 List 中,形式為 {Key, Value} 的元組將會被轉換為 Key => Value 形式的關聯。其他任何形式的項目將會被靜默忽略。

如果相同的鍵在 List 中多次出現,結果映射中會包含最靠近 List 頭部的鍵值,也就是調用 get_value(Key, List) 所返回的值。

範例

to_map([a, {b, 1}, {c, 2}, {c, 3}])

傳回

#{a => true, b => 1, c => 2}
連結至此函式

to_map(List, Stages)

檢視原始碼 (自 OTP 24.0 起)
-spec to_map(List, Stages) -> Map
                when
                    List :: [term()],
                    Stages :: [Operation],
                    Operation :: {aliases, Aliases} | {negations, Negations} | {expand, Expansions},
                    Aliases :: [{Key, Key}],
                    Negations :: [{Key, Key}],
                    Expansions :: [{Property :: property(), Expansion :: [term()]}],
                    Map :: #{term() => term()}.

在套用 Stages 中給定的正規化後,將屬性列表 List 轉換為 map。

另請參閱 normalize/2, to_map/1

-spec unfold(ListIn) -> ListOut when ListIn :: [term()], ListOut :: [term()].

ListIn 中所有出現的原子展開為元組 {Atom, true}

另請參閱 compact/1