emmap erlang_erlang的map基本使用
maps
適用于需要在運(yùn)行時(shí)改變數(shù)據(jù)結(jié)構(gòu)(record則不行)的場(chǎng)景,可以動(dòng)態(tài)增加key
數(shù)據(jù)量不宜過大,具體多大沒有實(shí)際數(shù)據(jù),
maps from_list ?如果list表很長(zhǎng),則相應(yīng)的耗時(shí)時(shí)間會(huì)很長(zhǎng),此時(shí)最好用lists模塊。
由于map是動(dòng)態(tài)結(jié)構(gòu),速度上必然無法匹敵record。
內(nèi)存大小介于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/2start()->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 模塊封裝了一些函數(shù)
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
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
總結(jié)
以上是生活随笔為你收集整理的emmap erlang_erlang的map基本使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电子科大电气工程导师介绍绍_电子科技大学
- 下一篇: python如何进行双色球预测最准确_【