erlang的map基本使用
maps
适用于需要在运行时改变数据结构(record则不行)的场景,可以动态增加key
数据量不宜过大,具体多大没有实际数据,
maps from_list 如果list表很长,则相应的耗时时间会很长,此时最好用lists模块。
由于map是动态结构,速度上必然无法匹敌record。
内存大小介于tuple与list之间
lists:sort([1,#{}, {}, []]).
[1,{},#{},[]]
| Operations | Records | Maps | Dict |
| Immutable | ✓ | ✓ | ✓ |
| Keys of any type | ✓ | ✓ | |
| Usable with maps/folds | ✓ | ✓ | |
| Content opaque to other modules | ✓ | ||
| Has a module to use it | ✓ | ✓ | |
| Supports pattern matching | ✓ | ✓ | |
| All keys known at compile-time | ✓ | ||
| Can merge with other instance | ✓ | ✓ | |
| Testing for presence of a key | ✓ | ✓ | |
| Extract value by key | ✓ | ✓ | ✓ |
| Per-key Dialyzer type-checking | ✓ | * | |
| Conversion from/to lists | ✓ | ✓ | |
| Per-element default values | ✓ | ||
| Standalone data type at runtime | ✓ | ||
| Fast direct-index access | ✓ |
* The EEP recommends making this possible for keys known at compile-time, but has no ETA on when or if this will happen.
示例代码
-module (test).
-compile([export_all]). % maps:
% find/2 fold/3 from_list/1 get/2 get/3
% is_key/2 keys/1 map/2 merge/2 module_info/0
% module_info/1 new/0 put/3 remove/2 size/1
% to_list/1 update/3 values/1 with/2 without/2 start() ->
A = #{k1 => 1, k2 => 2, k3 => 3},
#{k1 := K1} = A,
A1 = A#{ k1 := K1 + 1}. %
make() ->
% maps: new | from_list
M1 = maps:new(),
% insert new one
M2 = M1#{ k1 => 1},
% update
M3 = M2#{ k1 := 2}, % maps 模块封装了一些函数
maps:put(k2, 2, M3). % 匹配
match(#{k1 := K1}) -> io:format("~p ~n", [K1]);
match(#{nothing := N}) -> io:format("~p ~n", [N]). raw() ->
[ {I, I}|| I <- lists:seq(1, 10000000)]. get() ->
L = raw(),
lists:keyfind(999, 1, L). get2() ->
M = maps:from_list(raw()), maps:get(999, M).
更多阅读:
http://learnyousomeerlang.com/maps
http://erlang.org/doc/man/maps.html
erlang的map基本使用的更多相关文章
- 8种Nosql数据库系统对比
导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型NoSQL数据库的文章. 虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只 ...
- 8种NOsql
虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 但是NoSQL数据库之间的不同,远超过两 SQ ...
- 【转】8种Nosql数据库系统对比
导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型nosql数据库的文章.文章由敏捷翻译 – 唐尤华编译.如需转载,请参见文后声明. 虽然SQL数据库是非常有 ...
- Data Base 关于nosql的讲解
Data Base 关于nosql的讲解 nosql非关系型数据库. 优点: 1.可扩展 2.大数据量,高性能 3.灵活的数据模型 4.高可用 缺点: 1.不正式 2.不标准 非关系型数据库有哪些: ...
- NoSQL 数据库系统对比
虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只是时间问题:被迫使用关系数据库,但最终发现不能适应需求的情况不胜枚举. 但是NoSQL数据库之间的不同,远超过两 SQ ...
- 8 种 NoSQL 数据库系统对比
导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型NoSQL数据库的文章. 虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只 ...
- 数据库 --> 8种NoSQL数据库对比
8 种 NoSQL 数据库对比 NoSQL是一项全新的数据库革命性运动,NoSQL的拥护者们提倡运用非关系型的数据存储.现今的计算机体系结构在数据存储方面要求具备庞大的水平扩展性,而NoSQL致力于改 ...
- 对比Cassandra、 Mongodb、CouchDB、Redis、Riak、 Membase、Neo4j、HBase
转自:http://www.cnblogs.com/alephsoul-alephsoul/archive/2013/04/26/3044630.html 导读:Kristóf Kovács 是一位软 ...
- 8种Nosql数据库系统对比(转)
导读:Kristóf Kovács 是一位软件架构师和咨询顾问,他最近发布了一片对比各种类型NoSQL数据库的文章. 虽然SQL数据库是非常有用的工具,但经历了15年的一支独秀之后垄断即将被打破.这只 ...
随机推荐
- Win8 恢复传统启动菜单 for 多系统
如果你安装了Win7和Win8,启动的时候得先启动到Win8然后再启动到Win7,很怀念原来的启动选择器,这里给你方法了~ 当前系统是Win8的输入 bcdedit /set {current} bo ...
- NorFlash linux分区分析
一般情况下,与板卡相关的内容都在bsp中(即arch/arm/mach-xxx/board-xxx.c)中,但norflash的分区直接放在norflash驱动中.由于norflash应用基于mtd, ...
- JavaScript框架——jquery
1.jQuery编程常识 ————————如何进行jQuery插件开发 2.五星评分——jQuery Raty 一个很棒的jQuery评分插件—jQuery Raty 3.能感 ...
- 初识python轻量web框架flask
1.使用pip安装Python包 大多数Python包都使用pip实用工具安装,使用pyvenv创建的虚拟环境会自动安装pip. 1.使用pip安装Flask(其它Python包同理) pip ins ...
- winform学习
1:http://www.cnblogs.com/yieryi/category/704334.html 系列文章 2:http://www.easyicon.net/iconsearch/login ...
- for (const k in v){ 变量作用域
for (const k in v){ const a=[11,22,33,44]for(let i in a ){console.log(i)i=i+1}console.log('--- ...
- 给this添加属性
const f =()=>{ console.log(this) let a=5 console.log(this) console.log(this) this.ak =3} f() let ...
- Facebook Gradient boosting 梯度提升 separate the positive and negative labeled points using a single line 梯度提升决策树 Gradient Boosted Decision Trees (GBDT)
https://www.quora.com/Why-do-people-use-gradient-boosted-decision-trees-to-do-feature-transform Why ...
- 【ELK】Elasticsearch的备份和恢复
非原创,只是留作自己查询使用,转自http://keenwon.com/1393.html Elasticsearch的备份和恢复 备份 Elasticsearch的一大特点就是使用简单,api也比较 ...
- 常用js方法函数
常用方法函数 1.深复制 // 1.深复制 function deepCopy(source) { var result = {}; for (var key in source) { result[ ...