diff options
author | Björn-Egil Dahlberg <[email protected]> | 2016-04-28 12:11:14 +0200 |
---|---|---|
committer | Björn-Egil Dahlberg <[email protected]> | 2016-04-28 12:11:14 +0200 |
commit | ac2f1c71d5b5169d49a5cd5fd73d28a702a58024 (patch) | |
tree | b7d5a1fd173ac55e252c37e1da33826e479dab36 /lib/stdlib/src | |
parent | 85ccc38d59dd9751dfd7cead0e4ed0c1e6c169ad (diff) | |
parent | eeac08ca0ed6358a87f265f87a62bea8d0feb3b1 (diff) | |
download | otp-ac2f1c71d5b5169d49a5cd5fd73d28a702a58024.tar.gz otp-ac2f1c71d5b5169d49a5cd5fd73d28a702a58024.tar.bz2 otp-ac2f1c71d5b5169d49a5cd5fd73d28a702a58024.zip |
Merge branch 'egil/maps-api-additions/PR-1025/OTP-13522'
* egil/maps-api-additions/PR-1025/OTP-13522:
stdlib: Document maps:update_with/3,4
stdlib: Add tests for maps:update_with/3,4
stdlib: Add maps:update_with/3,4
erts: Add tests for maps:take/2
stdlib: Document maps:take/2
erts: Add BIF maps:take/2
Diffstat (limited to 'lib/stdlib/src')
-rw-r--r-- | lib/stdlib/src/maps.erl | 51 |
1 files changed, 45 insertions, 6 deletions
diff --git a/lib/stdlib/src/maps.erl b/lib/stdlib/src/maps.erl index a52928f77f..ba9ad4f549 100644 --- a/lib/stdlib/src/maps.erl +++ b/lib/stdlib/src/maps.erl @@ -20,15 +20,15 @@ -module(maps). --export([get/3,filter/2,fold/3, map/2, - size/1, +-export([get/3, filter/2,fold/3, + map/2, size/1, + update_with/3, update_with/4, without/2, with/2]). - -%%% BIFs +%% BIFs -export([get/2, find/2, from_list/1, is_key/2, keys/1, merge/2, - new/0, put/3, remove/2, + new/0, put/3, remove/2, take/2, to_list/1, update/3, values/1]). -spec get(Key,Map) -> Value when @@ -102,6 +102,13 @@ put(_,_,_) -> erlang:nif_error(undef). remove(_,_) -> erlang:nif_error(undef). +-spec take(Key,Map1) -> {Value,Map2} | error when + Key :: term(), + Map1 :: map(), + Value :: term(), + Map2 :: map(). + +take(_,_) -> erlang:nif_error(undef). -spec to_list(Map) -> [{Key,Value}] when Map :: map(), @@ -127,8 +134,40 @@ update(_,_,_) -> erlang:nif_error(undef). values(_) -> erlang:nif_error(undef). +%% End of BIFs + +-spec update_with(Key,Fun,Map1) -> Map2 when + Key :: term(), + Map1 :: map(), + Map2 :: map(), + Fun :: fun((Value1 :: term()) -> Value2 :: term()). + +update_with(Key,Fun,Map) when is_function(Fun,1), is_map(Map) -> + try maps:get(Key,Map) of + Val -> maps:update(Key,Fun(Val),Map) + catch + error:{badkey,_} -> + erlang:error({badkey,Key},[Key,Fun,Map]) + end; +update_with(Key,Fun,Map) -> + erlang:error(error_type(Map),[Key,Fun,Map]). + + +-spec update_with(Key,Fun,Init,Map1) -> Map2 when + Key :: term(), + Map1 :: Map1, + Map2 :: Map2, + Fun :: fun((Value1 :: term()) -> Value2 :: term()), + Init :: term(). + +update_with(Key,Fun,Init,Map) when is_function(Fun,1), is_map(Map) -> + case maps:find(Key,Map) of + {ok,Val} -> maps:update(Key,Fun(Val),Map); + error -> maps:put(Key,Init,Map) + end; +update_with(Key,Fun,Init,Map) -> + erlang:error(error_type(Map),[Key,Fun,Init,Map]). -%%% End of BIFs -spec get(Key, Map, Default) -> Value | Default when Key :: term(), |