librarylibs/Sets/src/Sets.xtl

Sets: vectors as sets -- union, intersection, difference, subset, and how often each item occurs. Import it with an alias of your choice: "se:" u_se< "Sets". Put libs/Sets/src on XETAL_PATH ("just path"); the reference is libs/Sets/docs. Names with l: are exported; those under h: are private to this file.

A set is a vector of any type with equality (numbers, characters, boxes); results have no repeats and keep the order in which items are first seen, left argument first.

source

Combining

ˡu̲nion : Eq a => a -> a -> a

function · line 14

a u_nion b: the items of either.

ˡu̲nion ← { a b → u̲nique a c̲at b }

ˡi̲ntersect : Eq a => a -> a -> a

function · line 17

a i_ntersect b: the items of a that are also in b.

ˡi̲ntersect ← { a b → u ← u̲nique a◆ (u m̲ember? b) r̲eplicate u }

ˡd̲ifference : Eq a => a -> a -> a

function · line 20

a d_ifference b: the items of a that are not in b.

ˡd̲ifference ← { a b → u ← u̲nique a◆ (n̲ot u m̲ember? b) r̲eplicate u }
Used in: ˡs̲ymmetric

ˡs̲ymmetric : Eq a => a -> a -> a

function · line 23

a s_ymmetric b: the items in one but not both.

ˡs̲ymmetric ← { a b → (a ˡd̲ifference b) c̲at b ˡd̲ifference a }

Comparing

ˡs̲ubset? : (Eq a, Truthy b) => a -> a -> b

function · line 28

a s_ubset? b: whether every item of a is in b.

ˡs̲ubset? ← { a b → '∧ r̲/ a m̲ember? b }
Used in: ˡs̲ame?

ˡs̲ame? : (Eq a, Truthy b) => a -> a -> b

function · line 32

a s_ame? b: whether a and b hold the same items, ignoring order and repeats.

ˡs̲ame? ← { a b → (a ˡs̲ubset? b) ∧ b ˡs̲ubset? a }

ˡd̲isjoint? : (Eq a, Truthy b) => a -> a -> b

function · line 35

a d_isjoint? b: whether no item is in both.

ˡd̲isjoint? ← { a b → n̲ot '∨ r̲/ a m̲ember? b }

Counting

ˡc̲ounts : Eq a => a -> Int

function · line 40

c_ounts v: how often each item of u_nique v occurs in v, in that order.

ˡc̲ounts ← { v → '{ t̲ally w̲here v = ⍵ } e̲ach u̲nique v }

ˡm̲ode : Eq a => a -> a

function · line 43

m_ode v: the items that occur most often, first seen first.

ˡm̲ode ← { v →
  c ← ˡc̲ounts v
  (c = 'm̲ax r̲/ c) r̲eplicate u̲nique v
}