erlang 数据结构---proplists
来源:互联网 发布:微信团购源码 编辑:程序博客网 时间:2024/06/06 09:48
从今天开始准备写一系列关于erlang 数据结构的文章。
今天是proplists::
一种key-value 数据结构。
一种元素为{key, Value}| term() 的lists(定义极其宽松)。
或者可以说proplists定义了一组关于lists的操作。 所以最好从他的函数来看这个数据机构,而不是定义。
1. 函数
1> L = [{1, [a,b,c]}, {1, [[d,e,f]]}, {1, g}, {1.0, h}].
[{1,[a,b,c]},{1,[[d,e,f]]},{1,g},{1.0,h}]
1.1 append_values(Key, List): 将Key对应的Value 中的元素组成一个lists返回
2> proplists:append_values(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]
note: 注意其于append_values的区别。
1.3 delete(key, List): 删除与key相同的元组
4> proplists:delete(1, L).
[{1.0,h}]
1.4 get_bool(Key, List) -> boolean()
1> L1 = [{1, a}, {1,true}].
2> proplists:get_bool(1, L1).
false
notes: 基本可以解释为 lookup(Key, List) =:= {Key, true}
1.5 get_value(Key, List, Default) -> Value:: term() | undefined
notes:返回Key对应的第一个Value, 否则返回default
1.6 get_keys/1 ->
1> L1 = [{1, a}, {1,true}].
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).
[{1,[a,b,c]},{1,[[d,e,f]]},{1,g}]
notes:返回所有key等于Key的tuple list
1.10 property/1, property/2: 返回proplist的key-value tuple 的最简形式
1> proplists:property({a, false}).
{a,false}
2> proplists:property({a, true}).
a
note: 其他就是将 {Key::atom(), true} 形式的tuple表示为Key来简化。 其他tuple形式不变
1.11 compact(ListIn) -> List
1> proplists:compact([a, {a, true}, {a, 1}, {1, a}, {1, true}]).
[a,a,{a,1},{1,a},{1,true}]
note:等价于[property(P) || P <- ListIn]
1.12 expand(Expansions, ListIn) -> ListOut
Expansions:[{Property::property(), Expansion::[term()]}]
notes: 就是一次替换操作。 如果property(即{key, Value}) 中的key和listsIn中Key(第一次)相同,就用Expansion 中的term 替换掉 1> proplists:expand([{foo, [bar, baz]}], [fie, foo, fum]).
[fie,bar,baz,fum]
2> proplists:expand([{{foo, true}, [bar, baz]}], [fie, foo, fum]).
[fie,bar,baz,fum]
3> proplists:expand([{{foo, false}, [bar, baz]}], [fie, {foo, false}, fum]).
[fie,bar,baz,fum]
4> proplists:expand([{{foo, true}, [bar, baz]}], [{foo, false}, fie, foo, fum]).
[{foo,false},fie,foo,fum]
1.13 split(List, Keys) -> {Lists, Rest}
1> proplists: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]}
notes: 将lists 分为Keys 和 非Keys两拨(顺序还是原来的顺序)
1.14 unfold(List)
2> proplists:unfold([a, 1, {1,true}, {a, a}]).
[{a,true},1,{1,true},{a,a}]
notes: 把所有的atom用 {atom, true}替换
1.15 substitute_aliases(Aliases, ListIn) -> ListOut
32> proplists:substitute_aliases([{a, 1}, {a,2}], [a, {a,z}, {a,m}]).
[{1,true},{1,z},{1,m}]
notes:替换所有key的名字,但是如果Aliases中的key相同, 那么只会是第一个生效,正如例子中a没有替换成2,而是替换成了1
1.16 substitute_negations(Negations, ListIn) -> ListOut
1> proplists:substitute_negations([{a, 1}, {a, 2}], [a, {a,z}, {a,false}, {a,true}]).
[{1,false},{1,true},{1,true},{1,false}]
notes:替换加取反
1.17 normalize(ListIn, Stages) -> ListOut
notes: 调用的是 substitute_aliases/2, substitute_negations/2, expand/2, compact/1
2. 应用场景
我们一般会在读取配置等和配置相关的时候用到。 例如ranch中就是这样。
3. notes
3.1. 判断相等用的是=:=
3.2 由于get_value 等在erlang层实现,自然没有lists:keyfind 在c实现高效。
- erlang 数据结构---proplists
- [Erlang 0065] Erlang proplists
- erlang 数据结构
- .record转proplists proplists转record
- erlang 常用数据结构
- erlang 数据结构--(ord)dict
- erlang 数据结构 -- gb_trees
- erlang数据结构-- sets
- erlang 数据结构 -- queue
- erlang 数据结构 --maps
- erlang 数据结构总结
- Erlang标准数据结构的选择
- erlang 数据结构-- 有向图
- Erlang
- Erlang
- Erlang
- erlang
- Erlang
- 使用IIS在Windows上托管ASP.NET Core(本文仅针对Window服务器)
- 用C++实现直接插入排序
- Windows 修改桌面Desktop的默认路径为D盘
- sparkSQL操作结果集
- 数据结构实战java实现队列
- erlang 数据结构---proplists
- shiro从入门到精通
- request.getQueryString()与request.getParameter()乱码
- 用户运营,如何做好活跃用户的数据分析
- apache 编译安装
- 数据结构-二叉树 JAVA语言实现
- 赌博游戏设计逻辑顺序题目
- RegCreateKeyEx 函数簇详解
- ArcGIS server使用本地api加载地图预览