From: Siraaj Khandkar Date: Tue, 12 Aug 2014 17:53:51 +0000 (-0400) Subject: Implement a Key->Value list dictionary. X-Git-Tag: 1.0.0~27 X-Git-Url: https://git.xandkar.net/?p=hope.git;a=commitdiff_plain;h=03ac148f8fdf338c85532a61b59ca1152ed7a5d0 Implement a Key->Value list dictionary. --- diff --git a/src/hope_kv_list.erl b/src/hope_kv_list.erl new file mode 100644 index 0000000..0b80a4a --- /dev/null +++ b/src/hope_kv_list.erl @@ -0,0 +1,81 @@ +%%%---------------------------------------------------------------------------- +%%% Equivalent to stdlib's orddict, but with a pretty (IMO), uniform interface. +%%%---------------------------------------------------------------------------- +-module(hope_kv_list). + +-behavior(hope_dictionary). + +-export_type( + [ t/2 + ]). + +-export( + [ empty/0 + , get/2 + , set/3 + , update/3 + , iter/2 + , map/2 + , filter/2 + , fold/3 + , of_kv_list/1 + , to_kv_list/1 + ]). + + +-type t(K, V) :: + [{K, V}]. + + +%% ============================================================================ +%% API +%% ============================================================================ + +-spec empty() -> + []. +empty() -> + []. + +get(T, K) -> + case lists:keyfind(K, 1, T) + of false -> none + ; {K, V} -> {some, V} + end. + +set(T, K, V) -> + lists:keystore(K, 1, T, {K, V}). + +update(T, K, F) -> + V1Opt = get(T, K), + V2 = F(V1Opt), + set(T, K, V2). + +iter(T, Map1) -> + Map2 = lift_map_into_list(Map1), + lists:foreach(Map2, T). + +map(T, Map1) -> + Map2 = lift_map_into_list(Map1), + lists:map(Map2, T). + +filter(T, Map1) -> + Map2 = lift_map_into_list(Map1), + lists:filter(Map2, T). + +fold(T, F1, Accumulator) -> + F2 = fun ({K, V}, Acc) -> F1(K, V, Acc) end, + lists:foldl(F2, T, Accumulator). + +to_kv_list(T) -> + T. + +of_kv_list(List) -> + List. + + +%% ============================================================================ +%% Helpers +%% ============================================================================ + +lift_map_into_list(Map) -> + fun ({K, V}) -> {K, Map(K, V)} end.