Today’s Erlang Thursday is on ordsets:subtract/2.
ordsets:subtract/2
takes two ordered sets as its arguments, and returns a ordered set containing the items of the first ordered set that are not in the second ordered set.
OrderedSetA = ordsets:from_list([5, 4, 3, 2, 1]). # [1,2,3,4,5] OrderedSetB = ordsets:from_list([1, 1, 2, 3, 5, 8, 13]). # [1,2,3,5,8,13] OrderedSetC = ordsets:from_list([2, -2, 4, -4, 16, -16]). # [-16,-4,-2,2,4,16] EmptySet = ordsets:new(). # [] ordsets:subtract(OrderedSetA, OrderedSetB). # [4] ordsets:subtract(OrderedSetA, EmptySet). # [1,2,3,4,5] ordsets:subtract(OrderedSetB, EmptySet). # [1,2,3,5,8,13] ordsets:subtract(EmptySet, OrderedSetA). # [] ordsets:subtract(OrderedSetB, OrderedSetC). # [1,3,5,8,13]
And note that ordsets:subtract/2
is not commutative, unlike ordsets:union/2
or ordsets:intersection/2
.
ordsets:subtract(OrderedSetA, OrderedSetC). # [1,3,5] ordsets:subtract(OrderedSetC, OrderedSetA). # [-16,-4,-2,16]
And again, your friendly reminder if you haven’t been following along, just because Ordered Sets in Erlang are represented as a List, doesn’t mean that Lists are Ordered Sets.
–Proctor