Skip to content

Instantly share code, notes, and snippets.

@elbrujohalcon
Created June 21, 2016 03:26
Show Gist options
  • Save elbrujohalcon/ac2a75c13241ef335b682198d3a80e04 to your computer and use it in GitHub Desktop.
Save elbrujohalcon/ac2a75c13241ef335b682198d3a80e04 to your computer and use it in GitHub Desktop.
people module for my blog post
-module(people).
-export([count/2]).
-export([test/0]).
-type user() :: binary().
-type country() :: atom().
-type group() :: #{ country => country()
, members => [user()]
}.
-spec count(country(), [group()]) -> non_neg_integer().
count(Country, Groups) ->
lists:sum(
lists:map(
fun(Group) ->
case Group of
#{country := Country, members := Members} -> length(Members);
_ -> 0
end
end, Groups)).
test() ->
Groups = [ #{country => ar, members => [<<"m1">>, <<"m2">>]}
, #{country => ar, members => [<<"m3">>]}
, #{country => uk, members => [<<"m4">>, <<"m5">>]}
],
0 = people:count(us, Groups),
2 = people:count(uk, Groups),
3 = people:count(ar, Groups),
ok.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment