Created
August 1, 2021 09:25
-
-
Save plvhx/4c12e2334b796451a24a3a310e4ee250 to your computer and use it in GitHub Desktop.
filtering dict like a boss.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-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