Algosim documentation: collapse

collapse

Counts and collapses runs of identical values in a container.

Syntax

Description

If L is a list or some other suitable container, then collapse(L) returns the list obtained from L by replacing each run of identical elements x of length c (possibly 1) by the pair (x, c).

collapse(L, ε) has the same behaviour, except that floating-point comparisons are made with epsilon ε.

Examples

L ≔ '(3, 2, 2, 2, 2, 5, 1, 0, 0, 0, 0, 2, 3, 4, 4, 4, 1);
collapse(L)
(3, 1)
(2, 4)
(5, 1)
(1, 1)
(0, 4)
(2, 1)
(3, 1)
(4, 3)
(1, 1)
collapse(characters("aaaaabbcbbaaccccac"))
(a, 5)
(b, 2)
(c, 1)
(b, 2)
(a, 2)
(c, 4)
(a, 1)
(c, 1)
join(filter(collapse(SequenceList(2, 100) @ IsPrime), (x ↦ ¬x[1])) @ (x ↦ x[2]))
1, 1, 3, 1, 3, 1, 3, 5, 1, 5, 3, 1, 3, 5, 5, 1, 5, 3, 1, 5, 3, 5, 7, 3

See also