Packages
otpbp
4.65.2
7.18.0
7.16.1
7.15.0
7.14.0
7.13.0
7.12.0
7.11.0
7.10.2
7.9.3
7.9.2
7.9.1
7.8.7
7.8.6
7.8.5
7.8.4
7.7.1
7.7.0
7.6.3
7.6.2
7.6.1
7.6.0
7.5.1
7.5.0
7.4.0
7.3.4
7.3.3
7.3.2
7.3.1
7.3.0
7.2.0
7.1.1
7.1.0
7.0.4
7.0.3
7.0.2
7.0.1
6.26.0
6.25.0
6.24.0
6.23.0
6.22.2
6.21.3
6.21.2
6.21.1
6.20.2
6.19.1
6.19.0
6.18.3
6.18.2
6.18.1
6.18.0
6.17.1
6.17.0
6.16.0
6.15.4
6.15.3
6.15.2
6.15.1
6.15.0
6.14.0
6.13.5
6.13.4
6.13.3
6.13.2
6.13.1
6.13.0
6.12.0
6.11.2
6.11.1
6.10.0
6.9.2
6.9.1
6.9.0
6.8.0
6.7.1
6.7.0
6.6.4
6.6.3
6.6.2
6.6.1
6.6.0
6.5.0
6.4.0
6.3.3
6.3.2
6.3.1
6.3.0
6.2.1
6.2.0
6.1.0
6.0.0
5.39.0
5.38.0
5.37.0
5.36.0
5.35.2
5.34.3
5.34.2
5.34.1
5.33.1
5.32.1
5.32.0
5.31.3
5.31.2
5.31.1
5.31.0
5.30.1
5.30.0
5.29.0
5.28.4
5.28.3
5.28.2
5.28.1
5.28.0
5.27.0
5.26.5
5.26.4
5.26.3
5.26.2
5.26.1
5.26.0
5.25.0
5.24.2
5.24.1
5.23.0
5.22.2
5.22.1
5.22.0
5.21.0
5.20.1
5.20.0
5.19.4
5.19.3
5.19.2
5.19.1
5.19.0
5.18.0
5.17.0
5.16.3
5.16.1
5.16.0
5.15.1
5.15.0
5.14.0
5.13.1
5.13.0
5.12.1
5.12.0
5.11.2
5.11.1
5.10.1
5.10.0
5.9.0
5.8.0
5.7.0
5.6.1
5.6.0
5.5.0
5.4.2
5.4.1
5.4.0
5.3.0
5.2.2
5.2.1
5.2.0
5.1.2
5.1.1
4.79.0
4.78.0
4.77.0
4.76.2
4.75.3
4.75.2
4.75.1
4.74.0
4.73.1
4.73.0
4.72.3
4.72.2
4.72.1
4.72.0
4.71.1
4.71.0
4.70.0
4.69.4
4.69.3
4.69.2
4.69.1
4.69.0
4.68.0
4.67.5
4.67.4
4.67.3
4.67.2
4.67.0
4.66.0
4.65.2
4.65.1
4.64.0
4.63.2
4.63.1
4.63.0
4.62.0
4.61.1
4.61.0
4.60.4
4.60.3
4.60.2
4.60.1
4.60.0
4.59.0
4.58.0
4.57.2
4.57.1
4.57.0
4.56.2
4.56.1
4.56.0
4.55.0
4.54.0
4.53.0
4.52.1
4.52.0
4.51.3
4.51.2
4.51.0
4.50.0
4.49.3
4.49.2
OTP backports
Current section
Files
Jump to
Current section
Files
src/otpbp_maps.erl
-module(otpbp_maps).
-compile([{parse_transform, otpbp_pt}]).
-ifndef(HAVE_maps__update_with_3).
% OTP 19.0
-export([update_with/3]).
-endif.
-ifndef(HAVE_maps__update_with_4).
% OTP 19.0
-export([update_with/4]).
-endif.
-ifndef(HAVE_maps__take_2).
% OTP 19.0
-export([take/2]).
-endif.
-ifndef(HAVE_maps__iterator_1).
% OTP 21.0
-export([iterator/1]).
-endif.
-ifndef(HAVE_maps__next_1).
% OTP 21.0
-export([next/1]).
-endif.
-ifndef(HAVE_maps__merge_with_3).
% OTP 24.0
-export([merge_with/3]).
-endif.
-ifndef(HAVE_maps__intersect_with_3).
% OTP 24.0
-export([intersect_with/3]).
-endif.
-ifndef(HAVE_maps__intersect_2).
% OTP 24.0
-export([intersect/2]).
-endif.
-ifndef(HAVE_maps__filtermap_2).
% OTP 24.0
-export([filtermap/2]).
-endif.
-ifndef(HAVE_maps__from_keys_2).
% OTP 24.0
-export([from_keys/2]).
-endif.
-ifndef(HAVE_maps__foreach_2).
% OTP 24.0
-export([foreach/2]).
-endif.
-ifndef(HAVE_maps__groups_from_list_2).
% OTP 25.0
-export([groups_from_list/2]).
-endif.
-ifndef(HAVE_maps__groups_from_list_3).
% OTP 25.0
-export([groups_from_list/3]).
-endif.
-ifndef(HAVE_maps__update_with_4).
update_with(K, Fun, Init, M) ->
is_map(M) orelse error({badmap, M}, [K, Fun, Init, M]),
is_function(Fun, 1) orelse error(badarg, [K, Fun, Init, M]),
case M of
#{K := V} -> M#{K := Fun(V)};
_ -> M#{K => Init}
end.
-endif.
-ifndef(HAVE_maps__update_with_3).
update_with(K, Fun, M) ->
is_map(M) orelse error({badmap, M}, [K, Fun, M]),
is_function(Fun, 1) orelse error(badarg, [K, Fun, M]),
case M of
#{K := V} -> M#{K := Fun(V)};
_ -> error({badkey, K}, [K, Fun, M])
end.
-endif.
-ifndef(HAVE_maps__take_2).
take(K, M) ->
case M of
#{K := V} -> {V, maps:remove(K, M)};
#{} -> error;
_ -> error({badmap, M}, [K, M])
end.
-endif.
-ifndef(HAVE_maps__iterator_1).
iterator(M) when is_map(M) -> [0|M];
iterator(M) -> error({badmap, M}, [M]).
-endif.
-ifndef(HAVE_maps__next_1).
next({_K, _V, _I} = KVI) -> KVI;
next([0|Map]) when is_map(Map) -> erts_internal:map_next(0, Map, iterator);
next(none) -> none;
next(Iter) -> error(badarg, [Iter]).
-endif.
-ifndef(HAVE_maps__merge_with_3).
merge_with(C, M1, M2) ->
is_function(C, 3) orelse error(badarg, [C, M1, M2]),
is_map(M1) orelse error({badmap, M1}, [C, M1, M2]),
is_map(M2) orelse error({badmap, M2}, [C, M1, M2]),
maps:fold(fun(K, V2, A) -> maps:update_with(K, fun(V1) -> C(K, V1, V2) end, V2, A) end, M1, M2).
-endif.
-ifndef(HAVE_maps__intersect_with_3).
intersect_with(C, M1, M2) ->
is_function(C, 3) orelse error(badarg, [C, M1, M2]),
is_map(M1) orelse error({badmap, M1}, [C, M1, M2]),
is_map(M2) orelse error({badmap, M2}, [C, M1, M2]),
maps:fold(fun(K, V2, A) ->
case M1 of
#{K := V1} -> A#{K := C(K, V1, V2)};
_ -> maps:remove(K, A)
end
end,
M2, M2).
-endif.
-ifndef(HAVE_maps__intersect_2).
intersect(M1, M2) ->
is_map(M1) orelse error({badmap, M1}, [M1, M2]),
is_map(M2) orelse error({badmap, M2}, [M1, M2]),
maps:with(maps:keys(M1), M2).
-endif.
-ifndef(HAVE_maps__filtermap_2).
-ifdef(HAVE_maps__iterator_1).
filtermap(Fun, Map) when is_function(Fun, 2) ->
case Map of
#{} -> filter_map(Fun, Map);
[0|M] when is_map(M) -> filter_map(Fun, M);
[I|M] when is_integer(I), is_map(M) -> filter_iterator(Fun, Map);
{_, _, _} -> filter_iterator(Fun, Map);
none -> #{};
_ -> error({badmap, Map}, [Fun, Map])
end;
filtermap(Fun, Map) -> error(badarg, [Fun, Map]).
filter_iterator(Pred, Iter) ->
maps:from_list(maps:fold(fun(K, V, A) ->
case Pred(K, V) of
true -> [{K, V}|A];
{true, NewV} -> [{K, NewV}|A];
false -> A
end
end, [], Iter)).
-else.
filtermap(Fun, Map) when is_function(Fun, 2) ->
case Map of
#{} -> filter_map(Fun, Map);
[0|M] when is_map(M) -> filter_map(Fun, M);
none -> #{};
_ -> error({badmap, Map}, [Fun, Map])
end;
filtermap(Fun, Map) -> error(badarg, [Fun, Map]).
-endif.
filter_map(Pred, Map) ->
maps:fold(fun(K, V, A) ->
case Pred(K, V) of
true -> A;
{true, NewV} -> A#{K := NewV};
false -> maps:remove(K, A)
end
end, Map, Map).
-endif.
-ifndef(HAVE_maps__from_keys_2).
from_keys(Keys, Value) when is_list(Keys) ->
try [{Key, Value} || Key <- Keys] of
L -> maps:from_list(L)
catch
error:_ -> error(badarg, [Keys, Value]);
C:R -> erlang:C(R)
end;
from_keys(Keys, Value) -> error(badarg, [Keys, Value]).
-endif.
-ifndef(HAVE_maps__foreach_2).
foreach(Fun, MapOrIter) when is_function(Fun, 2) ->
maps:fold(fun(K, V, _) ->
Fun(K, V),
ok
end,
ok, MapOrIter);
foreach(Fun, MapOrIter) -> error(badarg, [Fun, MapOrIter]).
-endif.
-ifndef(HAVE_maps__groups_from_list_2).
groups_from_list(F, L) when is_function(F, 1) ->
try lists:reverse(L) of
List -> groups_from_list_1(F, List, #{})
catch
error:_ -> error(badarg, [F, L])
end;
groups_from_list(F, L) -> error(badarg, [F, L]).
groups_from_list_1(F, [H|T], A) -> groups_from_list_1(F, T, maps:update_with(F(H), fun(Vs) -> [H|Vs] end, [H], A));
groups_from_list_1(_F, [], A) -> A.
-endif.
-ifndef(HAVE_maps__groups_from_list_3).
groups_from_list(F, VF, L) when is_function(F, 1), is_function(VF, 1) ->
try lists:reverse(L) of
List -> groups_from_list_2(F, VF, List, #{})
catch
error:_ -> error(badarg, [F, VF, L])
end;
groups_from_list(F, VF, L) -> error(badarg, [F, VF, L]).
groups_from_list_2(F, VF, [H|T], A) ->
V = VF(H),
groups_from_list_2(F, VF, T, maps:update_with(F(H), fun(Vs) -> [V|Vs] end, [V], A));
groups_from_list_2(_F, _VF, [], A) -> A.
-endif.