[ library(lists) | Reference Manual | Alphabetic Index ]
collection_to_array(+Collection, -List)
Convert a "collection" into a list
- Collection
- A term to be interpreted as a collection
- List
- Output array
Description
Converts various "collection" data structures into an array. Fails if it
does not know how to do this. The supported collection types are:
- List
-
The list is converted into an array using array_list/2.
- Array
-
The array is returned unchanged.
- Subscript reference Array[...]
-
subscript/3 is called to evaluate the subscript reference. If this
results in a single array element, a one-element array is returned.
If subscript/3 results in a sub-array, this is returned.
- flatten(N, Collection)
-
If the collection is nested (multi-dimensional), the top N nesting
levels of the structure are converted into a flat array.
- flatten(Collection)
-
If the collection is nested (multi-dimensional), all nesting
structure is removed and a flat array is returned. All subterms that
look like list or array will be interpreted as such (including []).
Modes and Determinism
- collection_to_array(+, -) is semidet
Fail Conditions
Collection is not a collection
Examples
?- collection_to_array([a,b,[c,d]], Array).
Array = [](a, b, [c, d])
Yes
?- collection_to_array(flatten([a,b,[c,d]]), Array).
Array = [](a, b, c, d)
Yes
?- collection_to_array(flatten([](a,b,[c,d])), Array).
Array = [](a, b, c, d)
Yes
?- A = []([](a,b,c),[](d,e,f)),
collection_to_array(flatten(A[1..2,2..3]), Array).
Array = [](b, c, e, f)
Yes
?- L = [[a,b],[[c,d],[e,f]],g],
collection_to_array(flatten(1, L), Array).
Array = [](a, b, [c, d], [e, f], g)
Yes
See Also
collection_to_list / 2, subscript / 3, array_flat / 3