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

separate_by_key(+Key, +Value, +All, -Matches, -Others)

Partition the elements of a list according to a key value
Key
Key argument position (non-negative integer, or list of those)
Value
A term
All
List of structures
Matches
Variable, or list of structures
Others
Variable, or list of structures

Description

The list All is separated into two lists Matches and Others, such that Matches contains the elements whose Key argument is identical (in the sense of ==/2) to Value, and Others is a list with those matching elements deleted. The original list does not have to be ordered. The element order in the result lists corresponds to the order in the original list.

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

    ?- separate_by_key(1, b, [f(a,1),f(b,2),f(c,3),f(b,4)], Bs, Ns).
    Bs = [f(b,2), f(b,4)]
    Ns = [f(a,1), f(c,3)]
    Yes (0.00s cpu)

See Also

same_key_prefix / 5, group_by_key / 3