Created
June 21, 2016 03:26
-
-
Save elbrujohalcon/ac2a75c13241ef335b682198d3a80e04 to your computer and use it in GitHub Desktop.
people module for my blog post
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(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