erlang 資料結構 proplists

2021-08-09 09:47:42 字數 3528 閱讀 6480

從今天開始準備寫一系列關於erlang 資料結構的文章。

今天是proplists::

一種key-value 資料結構。

一種元素為| term() 的lists(定義極其寬鬆)。

或者可以說proplists定義了一組關於lists的操作。 所以最好從他的函式來看這個資料機構,而不是定義。

1> l = [, , , ].

[,,,]

[a,b,c,[d,e,f],g]

note: 在l中 [d, e, f] 在返回返回的時候依然為[d,e,f], 也就是說, 只是將value中的元素乙個個的新增在乙個列表之中, 不是flatten的。

1.2 get_all_values(key, list): 將key對用的value 組成乙個lists返回

3> proplists:get_all_values(1, l).

[[a,b,c],[[d,e,f]],g]1.3 delete(key, list): 刪除與key相同的元組

4> proplists:delete(1, l).

1.4 get_bool(key, list) -> boolean()

1> l1 = [, ].

2> proplists:get_bool(1, l1).

false

notes: 基本可以解釋為 lookup(key, list) =:=

1.5 get_value(key, list, default) -> value:: term() | undefined

notes:返回key對應的第乙個value, 否則返回default

1.6 get_keys/1 ->

1> l1 = [, ].

2> proplists:get_keys(l1).

[1]

notes:返回所有的key(不重複)

1.7 is_defined(key, list) -> boolean()

notes:list是否有key值等於key存在,有返回true, 反之為false

1.8 lookup(key, list) -> none | tuple()

9> proplists:get_value(1, l).

[a,b,c]

notes:類似於get_value, 只不過返回的是整個tuple

1.9 lookup(key, list) -> [tuple()]

10> proplists:lookup_all(1, l).

[,,]

notes:返回所有key等於key的tuple list

1.10 property/1, property/2: 返回proplist的key-value tuple 的最簡形式

1> proplists:property().

2> proplists:property().

a

note: 其他就是將 形式的tuple表示為key來簡化。 其他tuple形式不變

1.11 compact(listin) -> list

1> proplists:compact([a, , , , ]).

[a,a,,,]

note:等價於[property(p) || p <- listin]

1.12 expand(expansions, listin) -> listout

expansions:

notes: 就是一次替換操作。 如果property(即) 中的key和listsin中key(第一次)相同,就用expansion 中的term 替換掉

1> proplists:expand(, [fie, foo, fum]).

[fie,bar,baz,fum]

2> proplists:expand([, [bar, baz]}], [fie, foo, fum]).

[fie,bar,baz,fum]

3> proplists:expand([, [bar, baz]}], [fie, , fum]).

[fie,bar,baz,fum]

4> proplists:expand([, [bar, baz]}], [, fie, foo, fum]).

[,fie,foo,fum]

1.13 split(list, keys) ->

1> proplists:split([, , a, , d, , b], [a, b, c]).

,b],[,]],[,d]}

notes: 將lists 分為keys 和 非keys兩撥(順序還是原來的順序)

1.14 unfold(list)

2> proplists:unfold([a, 1, , ]).

[,1,,]

notes: 把所有的atom用 替換

1.15 substitute_aliases(aliases, listin) -> listout

32> proplists:substitute_aliases([, ], [a, , ]).

[,,]

notes:替換所有key的名字,但是如果aliases中的key相同, 那麼只會是第乙個生效,正如例子中a沒有替換成2,而是替換成了1

1.16 substitute_negations(negations, listin) -> listout

1> proplists:substitute_negations([, ], [a, , , ]).

[,,,]

notes:替換加取反

1.17 normalize(listin, stages) -> listout

notes: 呼叫的是 substitute_aliases/2, substitute_negations/2, expand/2, compact/1

我們一般會在讀取配置等和配置相關的時候用到。 例如ranch中就是這樣。

3.1. 判斷相等用的是=:=

3.2 由於get_value 等在erlang層實現,自然沒有lists:keyfind 在c實現高效。

erlang 資料結構 ord dict

1.今天是dict 和 orddict 都是key value 資料機構 dict 判斷key是否等於用的是 也就是說1,1.0 是不同的key,而orddict判斷等於用的是 orddict 在learnyousomeerlang 上說的是適用於75個元素以下的資料量,在之上可以用dict,或者g...

erlang資料結構 sets

1.型別 在erlang中sets的資料結構有四種,ordsets,sets,gb sets,sofs。2.定義 ordsets 有序集合 sets 集合 gb sets aa 平衡二叉樹實現的有序集合 sofs sets of sets sofs 集合的集合 3.介面函式 ordsets,sets...

Erlang資料結構及流程控制

資料結構反映乙個資料的內部構成。資料的邏輯結構是指成分資料的邏輯關係,可分為單一型別和復合型別。單一型別 數值整數 理論上允許是任意長度 只要不超過機器限制 浮點數 64bit 原子 用單引號括起來 復合型別 可無限巢狀 元組 不適用於遞推 列表 可按序訪問 用 適用於遞推 字串 是一種特殊的列表,...