-
Notifications
You must be signed in to change notification settings - Fork 9
/
maplike.erl
84 lines (65 loc) · 1.96 KB
/
maplike.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
%% @doc
%% map-like collection associates keys of type K to values of type V
-module(maplike).
-export([behaviour_info/1]).
-export([
build/2,
build/3,
keys/2
]).
behaviour_info(callbacks) ->
[
%%
%% append a new key/value pair to collection
%%
%% -spec append({key(_), val(_)}, datum:maplike(_, _)) -> datum:maplike(_, _).
{append, 2},
%%
%% insert a new a key/value pair to collection
%%
%% -spec insert(key(_), val(_), datum:maplike(_, _)) -> datum:maplike(_, _).
{insert, 3},
%%
%% optionally returns the value associated with key
%%
%% -spec lookup(key(_), datum:maplike(_, _)) -> datum:option( val(_) ).
{lookup, 2},
%%
%% remove key/value pair from collection
%%
%% -spec remove(key(_), datum:maplike(_, _)) -> datum:maplike(_, _).
{remove, 2},
%%
%% check if the collection has an association
%%
%% -spec has(key(_), datum:maplike(_, _)) -> true | false.
{has, 2},
%%
%% collects all keys of this collection to list
%%
%% -spec keys(datum:maplike(_, _)) -> [_].
{keys, 1},
%%
%% optionally apply a function to value associated with key
%%
%% -spec apply(key(_), fun((datum:option(_)) -> _), datum:maplike(_, _)) -> datum:maplike(_, _).
{apply, 3}
];
behaviour_info(_Other) ->
undefined.
%%
%% build map-like structure from another one
-spec build(atom(), [_]) -> datum:maplike(_, _).
build(Type, Any) ->
build(Type, fun datum:compare/2, Any).
%%
%% build map-like structure from another one
-spec build(atom(), datum:compare(_), [_]) -> datum:maplike(_, _).
build(Type, Ord, List)
when is_list(List) ->
lists:foldl(fun Type:append/2, Type:new(Ord), List).
%%
%% collects all keys of this collection to list
-spec keys(atom(), datum:maplike(_, _)) -> [_].
keys(Type, MapLike) ->
Type:foldr(fun({K, _}, Acc) -> [K|Acc] end, [], MapLike).