proplists 模块适用数据量较少的场景,处理配置文件和函数选项时常用.proplists对内部数据结构是Key-Value键值对形式,第一个元素做key用来查询和删除,如果一个key有多个值就会使用第一次出现的值,其它被忽略.proplists对于Key和Value的约束极其宽松,可以是任意term().甚至可以把{Atom,true}缩写成为Atom.也正是由于这样宽松的数据约束,proplists模块没有更新和追加数据项的方法,需要使用lists:replace/4.Key进行比较使用的是=:=精确等于,会判断类型和值.
 
 
5> proplists:get_value(1,[{1,a},{1.0,b},{1,c}]).
a
6> proplists:append_values(1,[{1,a},{1.0,b},{1,c}]).
[a,c]
8>

规范与压缩

  上面提到Atom缩写的形式,atom的形式成为压缩格式,{Atom,true}的形式成为规范形式.这是Property的两种形式,其定义是 -type property()   :: atom() | tuple().
模块里面有一个property方法专门进行数据规范化.
property({Key, true}) when is_atom(Key) ->
Key;
property(Property) ->
Property.
调用的形式:
16> proplists:property({a}).
{a}
17> proplists:property({a,true}).
a
压缩形式其实就是逐一对proplists内的元素执行property/1,即:[property(P) || P <- List].
10>  proplists:compact( [{a, true}, {b, true}, {a, 3}, {c, true}, {a, [4]}]).
[a,b,{a,3},c,{a,[4]}]
计算有压缩就会有展开的函数
unfold([P | Ps]) ->
if is_atom(P) ->
[{P, true} | unfold(Ps)];
true ->
[P | unfold(Ps)]
end;
unfold([]) ->
[]. 12> proplists:unfold([foo,bar,test,haha]).
[{foo,true},{bar,true},{test,true},{haha,true}]
13> proplists:unfold([foo,bar,{test,false},haha]).
[{foo,true},{bar,true},{test,false},{haha,true}]
14> proplists:unfold([foo,"zen",bar,{test,false},haha]).
[{foo,true},"zen",{bar,true},{test,false},{haha,true}]
15> proplists:unfold([foo,"zen",23,{test,false},haha]).
[{foo,true},"zen",23,{test,false},{haha,true}]
 

proplists 相关操作

下面看一下proplists的常规操作,有一些方法还是要注意一下细节的.

 append_values (注意上图中少拼写了一个s )将所有key值相同的数据项,value整合在list中.
1> proplists:append_values(a, [{a, [1,2]}, {b, 0}, {a, 3}, {c, -1}, {a, [4]}]).
[1,2,3,4]
2> proplists:append_values(a, [{a, b}, {b, 0}, {a, 3}, {c, -1}, {a, [4]}]).
[b,3,4]
3> proplists:append_values(a, [{a, b}, {b, 0}, {a, 3}, {c, -1}, {a, [[4]]}]).
[b,3,[4]] 1> proplists:append_values(a, [{a, [1,2]}, {"zen", 0}, {a, 3}, {c, -1}, {a, [4]}
]).
[1,2,3,4]
2> proplists:append_values(a, [{a, [1,2]},b]).
[1,2]
3> proplists:append_values(b, [{a, [1,2]},b]).
[true]
4>
 
delete方法会删除所有等于Key值的数据项:
%% delete(Key, List) -> List
18> proplists:delete(a, [{a, true}, {b, true}, {a, 3}, {c, true}, {a, [4]}]).
[{b,true},{c,true}]
 
compact将数据进行压缩
10>  proplists:compact( [{a, true}, {b, true}, {a, 3}, {c, true}, {a, [4]}]).
[a,b,{a,3},c,{a,[4]}]

 

get_all_values 获取所有等于Key值的数据项:

8> proplists:get_all_values(a, [{a, [1,2]}, {"zen", 0}, {a, 3}, {c, -1}, {a, [4]
}]).
[[1,2],3,[4]]
9>
get_bool 这个方法还是有点陷阱的,其意图是看Key值第一次出现时的值是true|false. 
get_bool(Key, [P | Ps]) ->
if is_atom(P), P =:= Key -> true; tuple_size(P) >= 1, element(1, P) =:= Key ->
case P of
{_, true} ->
true;
_ ->
%% Don't continue the search!
false
end;
true ->
get_bool(Key, Ps)
end;
get_bool(_Key, []) ->
false. 9> proplists:get_bool(a, [{a, [1,2]}, {"zen", 0}, {a, 3}, {c, -1}, {a, [4]}]).
false
10> proplists:get_bool(a, [{a, [1,2]}, {"zen", 0}, {a, 3},a, {c, -1}, {a, [4]}]).
false
11> proplists:get_bool(a, [a,{a, [1,2]}, {"zen", 0}, {a, 3},a, {c, -1}, {a, [4]}]).
true
12> proplists:get_bool(a, [{a,true},{a, [1,2]}, {"zen", 0}, {a, 3},a, {c, -1}, {a, [4]}]).
true
13> proplists:get_bool(a, [{a,false},{a, [1,2]}, {"zen", 0}, {a, 3},a, {c, -1},{a, [4]}]).
false
14> proplists:get_bool(q, [{a,true},{a, [1,2]}, {"zen", 0}, {a, 3},a, {c, -1}, {a, [4]}]).
false
15> proplists:get_bool(q, ["abc",{a,true},{a, [1,2]}, {"zen", 0}, {a, 3},a, {c,-1}, {a, [4]}]).
false
16> proplists:get_bool("abc", ["abc",{a,true},{a, [1,2]}, {"zen", 0}, {a, 3},a,{c, -1}, {a, [4]}]).
false
17> proplists:get_bool("abc", [{"abc",true},{a,true},{a, [1,2]}, {"zen", 0}, {a,
3},a, {c, -1}, {a, [4]}]).
true
 get_keys 获取所有不重复的keys
18> proplists:get_keys([{"abc",true},{a,true},{a, [1,2]}, {"zen", 0}, {a, 3},a,{c, -1}, {a, [4]}]).
["zen",a,c,"abc"]
19> proplists:get_keys([{a,true},{a,true},{a, [1,2]}, {"zen", 0}, {a, 3},a, {c,-1}, {a, [4]}]).
["zen",a,c]
get_value 按Key取值,取得是第一次出现的Value
get_value(Key, [P | Ps], Default) ->
if is_atom(P), P =:= Key ->
true;
tuple_size(P) >= 1, element(1, P) =:= Key ->
case P of
{_, Value} ->
Value;
_ ->
%% Don</code>t continue the search!
Default
end;
true ->
get_value(Key, Ps, Default)
end;
get_value(_Key, [], Default) ->
Default. 3> proplists:get_value([a,b], ["packet",[a,b],"login",22,2,s,f] , "none").
"none"
4> proplists:get_value("login", ["packet",[a,b],"login",22,2,s,f] , "none").
"none"
5> proplists:get_value(login, ["packet",[a,b],"login",22,2,s,f] , "none").
"none" 1> proplists:get_value([a,b], ["packet",{[a,b],bingo},"login",22,2,s,f] , "none").
bingo
2> proplists:get_value(s, ["packet",{[a,b],bingo},"login",22,2,s,f] , "none").
true
3>
look_up 与get_value不同的是这里返回的是{Key,Value}
6> proplists:lookup(a, [{a, b}, {b, 0}, {a, 3}, {c, -1}, {a, [[4]]}]).
{a,b}
7> proplists:lookup(a, [{a,1},{a, b}, {b, 0}, {a, 3}, {c, -1}, {a, [[4]]}]).
{a,1}
8>
lookup_all 
8> proplists:lookup_all(a, [{a,1},{a, b}, {b, 0}, {a, 3}, {c, -1}, {a, [[4]]}]).
[{a,1},{a,b},{a,3},{a,[[4]]}]
 
is_defined 是否存在特定Key值
6>  proplists:is_defined(s, ["packet",{s,kill},{[a,b],bingo},"login",22,2,s,f]).
true
7> proplists:is_defined(p, ["packet",{s,kill},{[a,b],bingo},"login",22,2,s,f]).
false
8>
split 按照Key值进行数据分组
9> 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]}
10> proplists:split([{c, 2}, {c,23},{a,false},{e, 1}, a, {c, 3, 4}, d, {b, 5}, b
], [a, b, c]).
{[[{a,false},a],[{b,5},b],[{c,2},{c,23},{c,3,4}]],[{e,1},d]}
11>

单独一组

下面这几个方法我们放在一起看
 
 expand 做的是把list中的Key替换成对应Value ,注意, 这个方法展开的对象是Property
8> proplists: expand([{foo, [bar, baz]}],[fie, foo, fum]).
[fie,bar,baz,fum]
9> proplists: expand([{foo, [bar, baz]},{fie,ok},{fum,100}],[fie, foo, fum]).
[ok,bar,baz,100]
10> proplists: expand([{foo, [bar, baz]},{fie,[[ok]]},{fum,"100"}],[fie, foo, fum]).
[[ok],bar,baz,49,48,48] 12> proplists: expand([{"fie",23},{1,{1}},{1.0,{29}},{foo, [bar, baz]},{fie,[[ok]]},{fum,"100"}],["fie",1, foo, fum]).
[102,105,101,1,bar,baz,49,48,48]
13> ${.
123
14> [102,105,101].
"fie"
15>
 substitute_aliases 将对应的key值替换为别名
1> proplists:substitute_aliases([{zen,"ligaoren"},{0,zero}],[zen,{zen,zen},{abc,zen},{zen,tick},0,{0,1},{23,0}]).
[{"ligaoren",true},{"ligaoren",zen},{abc,zen},{"ligaoren",tick},0,{zero,1},{23,0}]
2>
substitute_negations key值替换,value取反
2> proplists:substitute_negations([{zen,"ligaoren"},{0,zero}],[zen,{zen,zen},{abc,zen},{zen,tick},0,{0,1},{23,0}]).
[{"ligaoren",false},{"ligaoren",true},{abc,zen},{"ligaoren",true},0,zero,{23,0}] 3> proplists:substitute_negations([{zen,"ligaoren"},{0,zero}],[zen,{zen,zen},{abc,zen},{zen,tick},0,{0,true},{23,0}]).
[{"ligaoren",false},{"ligaoren",true},{abc,zen},{"ligaoren",true},0,{zero,false},{23,0}] 4> proplists:substitute_negations([{zen,"ligaoren"},{0,zero}],[zen,{zen,zen},{abc,zen},{zen,tick},0,{0,false},{23,0}]).
[{"ligaoren",false},{"ligaoren",true},{abc,zen},{"ligaoren",true},0,zero,{23,0}]
 normalize 整合了substitute_aliases   substitute_negations  expand
2> proplists:normalize( [a,b,c,d,e,f,g],[{aliases, [{b,b2},{e,email}]} ]).
[a,b2,c,d,email,f,g]
3> proplists:normalize([a,b,c,d,e,f,g],[{aliases, [{b,b2},{e,email}]} ]).
[a,b2,c,d,email,f,g]
4> proplists:normalize([a,b,c,d,e,f,g],[{aliases, [ {negations, [a,f]}]} ]).
[a,b,c,d,e,f,g]
5> proplists:normalize([a,b,c,d,e,f,g],[ {expand, [{d,do_it_by_yourself},{g,1000}]}]).
[a,b,c,do_it_by_yourself,e,f,1000]

应用举例

6> test:module_info(compile).
[{options,[{outdir,"/zen/temp"}]},
{version,"4.8"},
{time,{2012,6,15,2,3,23}},
{source,"/zen/temp/test.erl"}]
7> proplists:get_value(time,test:module_info(compile)).
{2012,6,15,2,3,23}
8>
 
mochiweb项目解析Header Cookie多处使用了proplist: 
parse_form_outer(eof, _, Acc) ->
lists:reverse(Acc);
parse_form_outer({headers, H}, FileHandler, State) ->
{"form-data", H1} = proplists:get_value("content-disposition", H),
Name = proplists:get_value("name", H1),
Filename = proplists:get_value("filename", H1),
case Filename of
undefined ->
fun (Next) ->
parse_form_value(Next, {Name, []}, FileHandler, State)
end;
_ ->
ContentType = proplists:get_value("content-type", H),
Handler = FileHandler(Filename, ContentType),
fun (Next) ->
parse_form_file(Next, {Name, Handler}, FileHandler, State)
end
end.

解析选项一例:

%%% 18> proplists:lookup(loop,[{ip, "127.0.0.1"},{loop, {mochiweb_http, default_body}}]).
%%% {loop,{mochiweb_http,default_body}} parse_options(Options) ->
{loop, HttpLoop} = proplists:lookup(loop, Options),
Loop = fun (S) ->
?MODULE:loop(S, HttpLoop)
end,
Options1 = [{loop, Loop} | proplists:delete(loop, Options)],
mochilists:set_defaults(?DEFAULTS, Options1).

最后:估计90%的情况下,我们只使用proplists:get_value   : )

2012-8-22更新

proplists:get_value的性能要比lists:keyfind差很多,

lists的下面几个方法都是BIF实现:%% Bifs: keymember/3, keysearch/3, keyfind/3

而proplists:get_value是Erlang实现,我觉得这是产生性能差异的根本原因;

下面有一个相关讨论基本上是同样的判断: http://www.ostinelli.net/erlang-listskeyfind-or-proplistsget_value/

-module(pvsl).
-define(LIST_SIZES, [10000, 100000, 1000000]).
-define(RETRIES, 1000).
-compile(export_all). start() ->
% test for different list sizes
lists:foreach(fun(N) -> test_list(N) end, ?LIST_SIZES). test_list(ListSize) ->
% generate a list of size ListSize of {Key, Val} entries
KeyList = [{K, K} || K <- lists:seq(1, ListSize)],
% test this list against both functions
lists:foreach(fun(Type) -> get_val(Type, now(), KeyList, ListSize, ?RETRIES) end,
[proplists, lists]). % test getting values, compute necessary time and output print results
get_val(Type, Start, _KeyList, ListSize, 0) ->
T = timer:now_diff(now(), Start),
io:format("computed ~p random key searches on a ~p-sized list in ~p ms using ~p~n",
[?RETRIES, ListSize, T/1000, Type]);
get_val(proplists, Start, KeyList, ListSize, Tries) ->
proplists:get_value(random:uniform(ListSize), KeyList),
get_val(proplists, Start, KeyList, ListSize, Tries - 1);
get_val(lists, Start, KeyList, ListSize, Tries) ->
lists:keyfind(random:uniform(ListSize), 1, KeyList),
get_val(lists, Start, KeyList, ListSize, Tries - 1). I ran this test on my MacBook Pro, Intel Core i5 2.4GHz with 4GB Memory, and Erlang R13B04, with Kernel Polling enabled. These are the results.
roberto$ erl +K true +P 1000000
Erlang R13B04 (erts-5.7.5) [source] [smp:4:4] [rq:4] [async-threads:0] [hipe] [kernel-poll:true] Eshell V5.7.5 (abort with ^G)
1> c(pvsl).
{ok,pvsl}
2> pvsl:start().
computed 1000 random key searches on a 10000-sized list in 323.373 ms using proplists
computed 1000 random key searches on a 10000-sized list in 12.897 ms using lists
computed 1000 random key searches on a 100000-sized list in 3273.973 ms using proplists
computed 1000 random key searches on a 100000-sized list in 130.592 ms using lists
computed 1000 random key searches on a 1000000-sized list in 34131.905 ms using proplists
computed 1000 random key searches on a 1000000-sized list in 2050.627 ms using lists
ok
3>

Erlang--proplists结构解析的更多相关文章

  1. iOS沙盒目录结构解析

    iOS沙盒目录结构解析 原文地址:http://blog.csdn.net/wzzvictory/article/details/18269713     出于安全考虑,iOS系统的沙盒机制规定每个应 ...

  2. H.264码流结构解析

    from:http://wenku.baidu.com/link?url=hYQHJcAWUIS-8C7nSBbf-8lGagYGXKb5msVwQKWyXFAcPLU5gR4BKOVLrFOw4bX ...

  3. Oracle的rowid结构解析

    SQL> select rowid,deptno from dept; ROWID                  DEPTNO ------------------ ---------- A ...

  4. EXT 结构解析

    EXT Demo 结构解析 创建项目 sencha -sdk F:\lib\ext-6.0.0 generate app demo F:\demo 预览项目 执行命令 sencha app build ...

  5. ionic项目结构解析

    ionic项目结构解析 原始结构 创建一个IonicDemo项目 'ionic start IonicDemo sidemenu' 这种结构多模块开发比较麻烦,因为view跟controller分开路 ...

  6. Redis源码剖析--源码结构解析

    请持续关注我的个人博客:https://zcheng.ren 找工作那会儿,看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识.在面试过程中,redis确实 ...

  7. InfluxDB源码目录结构解析

    操作系统 : CentOS7.3.1611_x64 go语言版本:1.8.3 linux/amd64 InfluxDB版本:1.1.0 influxdata主目录结构 [root@localhost ...

  8. [转帖]认识固态:SSD硬盘内外结构解析

    认识固态:SSD硬盘内外结构解析 来自: 中关村在线 收藏 分享 邀请 固态硬盘(Solid State Drive),简称固态盘(SSD),是用固态电子存储芯片阵列而制成的硬盘,由控制单元和存储单元 ...

  9. redis源代码结构解析

    看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识: 之前面试的时候被问到了这部分的内容,没有关注,好在还有时间,就把Redis的源码看了一遍. Redis ...

  10. MBR结构解析与fdisk的bash实现

    一.MBR结构解析 首先我们先介绍一些MBR的基本知识基础,再晾图片分析. MBR主要分为三大块各自是: 1.载入引导程序(446K) 2.分区表(64k) 3.标志结束位(2k) 载入引导程序:内容 ...

随机推荐

  1. ReactJS webpack实现JS模块化使用的坑

    从一个原生HTML/CSS/JS模式的网页改造到ReactJS模块化的结构,需要以下步骤: (1)引用ReactJS框架 ->(2)使用webpack 工具 -> (3)配置webpack ...

  2. vue2.0---模板语法

    ***插值 #文本 数据绑定最常见的形式就是双大括号的形式: <div id="app">{{message}}</div> 只要绑定的属性message上 ...

  3. MVC

    PureMVC--一款多平台MVC框架 http://www.jianshu.com/p/47deaced9eb3 MVC,MVP 和 MVVM 的图示 http://www.ruanyifeng.c ...

  4. System.Web.Http.Cors配置跨域访问的两种方式

    System.Web.Http.Cors配置跨域访问的两种方式 使用System.Web.Http.Cors配置跨域访问,众多大神已经发布了很多文章,我就不在详细描述了,作为小白我只说一下自己的使用心 ...

  5. centos环境搭建

    1.php -v 与phpinfo();    不符,查看centos是否有自带的php:更改centos环境变量 /etc/profile,source /etc/profile生效: 2.pecl ...

  6. df命令

    http://www.th7.cn/system/lin/201311/46839.shtml http://www.111cn.net/sys/CentOS/86335.htm

  7. oracle服务起不来以及无法监听问题解决

    改问题是在搭建起一个很早之前的数据库的时候碰见的,虽然这个问题网上已经有很多相关的帖子,但因最近碰见多次这样的问题,特此简单记录: 1.最开始碰见的问题是:The listener supports ...

  8. 利用gcc自带的功能-fstack-protector检测栈溢出及其实现

    最近又遇到了一个崩溃,栈回溯非常怪异. /lib/i386-linux-gnu/libc.so.(gsignal+0x4f) [0xb2b751df] /lib/i386-linux-gnu/libc ...

  9. Android如何一进入一个activity就唤醒键盘

    方法总结: 在AndroidManife.xml中对应的的Activity配置中加入以下配置项: android:windowSoftInputMode="stateVisible|adju ...

  10. jvm--1.class文件结构

    1.字节码(1)bytecode是构成平台无关性的基石 (2)当jvm发展到1.7-1.8的时候,jvm设计者通过,JSR-292,基本可以让其他语言运行在jvm上面. 如,Clojure , Gro ...