Current section

Files

Jump to
gleam_stdlib src gleam@map.erl
Raw

src/gleam@map.erl

-module(gleam@map).
-compile(no_auto_import).
-export([size/1, to_list/1, from_list/1, has_key/2, new/0, get/2, insert/3, map_values/2, keys/1, values/1, filter/2, take/2, merge/2, delete/2, drop/2, update/3, fold/3]).
-export_type([map_/2]).
-type map_(Key, Value) :: any() | {gleam_phantom, Key, Value}.
-spec size(map_(any(), any())) -> integer().
size(Map) ->
maps:size(Map).
-spec to_list(map_(ATT, ATU)) -> list({ATT, ATU}).
to_list(Map) ->
maps:to_list(Map).
-spec from_list(list({AUD, AUE})) -> map_(AUD, AUE).
from_list(List) ->
maps:from_list(List).
-spec has_key(map_(AUN, any()), AUN) -> boolean().
has_key(Map, Key) ->
maps:is_key(Key, Map).
-spec new() -> map_(any(), any()).
new() ->
maps:new().
-spec get(map_(AVD, AVE), AVD) -> {ok, AVE} | {error, nil}.
get(From, Get) ->
gleam_stdlib:map_get(From, Get).
-spec insert(map_(AVP, AVQ), AVP, AVQ) -> map_(AVP, AVQ).
insert(Map, Key, Value) ->
maps:put(Key, Value, Map).
-spec map_values(map_(AWB, AWC), fun((AWB, AWC) -> AWF)) -> map_(AWB, AWF).
map_values(Map, Fun) ->
maps:map(Fun, Map).
-spec keys(map_(AWP, any())) -> list(AWP).
keys(Map) ->
maps:keys(Map).
-spec values(map_(any(), AXA)) -> list(AXA).
values(Map) ->
maps:values(Map).
-spec filter(map_(AXJ, AXK), fun((AXJ, AXK) -> boolean())) -> map_(AXJ, AXK).
filter(Map, Property) ->
maps:filter(Property, Map).
-spec take(map_(AXV, AXW), list(AXV)) -> map_(AXV, AXW).
take(Map, Desired_keys) ->
maps:with(Desired_keys, Map).
-spec merge(map_(AYJ, AYK), map_(AYJ, AYK)) -> map_(AYJ, AYK).
merge(Map, New_entries) ->
maps:merge(Map, New_entries).
-spec delete(map_(AYZ, AZA), AYZ) -> map_(AYZ, AZA).
delete(Map, Key) ->
maps:remove(Key, Map).
-spec drop(map_(AZL, AZM), list(AZL)) -> map_(AZL, AZM).
drop(Map, Disallowed_keys) ->
gleam@list:fold(Disallowed_keys, Map, fun delete/2).
-spec update(map_(AZS, AZT), AZS, fun((gleam@option:option(AZT)) -> AZT)) -> map_(AZS, AZT).
update(Map, Key, Fun) ->
_pipe = Map,
_pipe@1 = get(_pipe, Key),
_pipe@2 = gleam@option:from_result(_pipe@1),
_pipe@3 = Fun(_pipe@2),
insert(Map, Key, _pipe@3).
-spec do_fold(list({AZZ, BAA}), BAC, fun((BAC, AZZ, BAA) -> BAC)) -> BAC.
do_fold(List, Initial, Fun) ->
case List of
[] ->
Initial;
[{K, V} | Tail] ->
do_fold(Tail, Fun(Initial, K, V), Fun)
end.
-spec fold(map_(BAD, BAE), BAH, fun((BAH, BAD, BAE) -> BAH)) -> BAH.
fold(Map, Initial, Fun) ->
_pipe = Map,
_pipe@1 = to_list(_pipe),
do_fold(_pipe@1, Initial, Fun).