Erlang Thursday – ordsets:is_subset/2

Today’s Erlang Thursday covers ordsets:is_subset/2.

ordsets:is_subset/2 takes two Ordered Sets, and checks if the ordered set passed in as the first argument is a subset of ordered set passed in as the argument. For a given set, Set A, to be a subset of another set, Set B, every item in Set A must also be a member of Set B.

SetA = ordsets:from_list(lists:seq(1, 10)).
% [1,2,3,4,5,6,7,8,9,10]
SetB = ordsets:from_list(lists:seq(2, 10, 2)).
% [2,4,6,8,10]
SetC = ordsets:from_list(lists:seq(1, 15, 3)).
% [1,4,7,10,13]
EmptySet = ordsets:new().
% []
ordsets:is_subset(SetB, SetA).
% true
ordsets:is_subset(SetA, SetB).
% false
ordsets:is_subset(SetC, SetA).
% false

And for those who aren’t as familiar with set theory, a few quick facts about sets. First, the empty set is a subset of all sets; second, a set is considered a sub-set of itself; and lastly, a given Set B is a superset of Set A, if Set A is subset of Set B.

ordsets:is_subset(EmptySet, SetA).
% true
ordsets:is_subset(EmptySet, SetB).
% true
ordsets:is_subset(EmptySet, SetC).
% true
ordsets:is_subset(EmptySet, EmptySet).
% true
ordsets:is_subset(SetA, SetA).
% true

–Proctor

Leave a Reply

Your email address will not be published. Required fields are marked *