Skip to content

Instantly share code, notes, and snippets.

@plvhx
Created August 1, 2021 09:25
Show Gist options
  • Save plvhx/4c12e2334b796451a24a3a310e4ee250 to your computer and use it in GitHub Desktop.
Save plvhx/4c12e2334b796451a24a3a310e4ee250 to your computer and use it in GitHub Desktop.
filtering dict like a boss.
-module(main).
-export([start/0]).
-import(io, [fwrite/2]).
%% internal callback when calling
%% dict:filter/2
filter_even(_, Value) ->
case lists:nth(1, Value) rem 2 of
0 -> true;
1 -> false
end.
debug(Value) -> fwrite("~p~n", [Value]).
start() ->
D0 = dict:append("1", 1, dict:new()),
D1 = dict:append("2", 2, D0),
D2 = dict:append("3", 3, D1),
D3 = dict:append("4", 4, D2),
D4 = dict:append("5", 5, D3),
debug(dict:fetch("1", D4)),
debug(dict:fetch("2", D4)),
debug(dict:fetch("3", D4)),
debug(dict:fetch("4", D4)),
debug(dict:fetch("5", D4)),
D5 = dict:filter(fun filter_even/2, D4),
debug(dict:fetch("2", D5)),
debug(dict:fetch("4", D5)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment