[ library(lists_of_structures) | Reference Manual | Alphabetic Index ]

group_values_with_key(+KeyPos, +ValPos, +Structs, -Grouped)

Partition a list into sublists by key
KeyPos
Key argument position (positive integer, or list of those)
ValPos
Value argument position (positive integer, or list of those)
Structs
List of structures
Grouped
Variable, or list of Key-ValueList structures

Description

The list Structs is partitioned into maximal sublists of consecutive elements with identical keys. If the original list was ordered according to the KeyPos'th argument, then the sublists represent a partitioning of the original elements according to their different key values. From each of these sublists, the ValPos'th arguments are extraced to form a ValueList for the corresponding key.

The output argument Grouped is bound to a list of Key-ValueList pairs, where Key is the value of the KeyPos'th argument that all the values in ValueList have in common.

Modes and Determinism

Exceptions

(4) instantiation fault
Arguments are insufficiently instantiated
(5) type error
Some argument or its components are of the wrong type
(6) out of range
Some structure does not have a Key'th argument

Examples

    ?- group_values_with_key(1, 2, [f(a,1), f(a,2), f(b,2), f(c,2), f(c,5)], Groups).
    Groups = [a - [1,2], b - [2], c - [2,5]]
    Yes (0.00s cpu)

    ?- group_values_with_key(2, 1, [f(a,1), f(a,2), f(b,2), f(c,2), f(c,5)], Groups).
    Groups = [1 - [a], 2 - [a,b,c], 5 - [c]]
    Yes (0.00s cpu)

See Also

group_with_key / 3