partition()

Synopsis

Partitions members of a list into a list of lists , each containing the members of the list that met the key and test criteria. Neither the order of the subsets nor the order of their elements should be relied upon.

Syntax

partition ( list As List, _
            Optional key As Name = :Identity, _
            Optional test As Name = :Equal) As List 
Argument Type Description
list List The list to partition
key Name Optional; the name of function to be called with each item in the list ; default is :Identity. The return value of this function is compared using test function
test Name Optional; the name of test function to be applied to key results of items in the lists ; default is :Equal

Example 1

Intent ->partition({"a", "b", "c", "c", "e", "z", "x", "c", "e"}) 
--> {{"x"}, {"z"}, {"e", "e"}, {"c", "c", "c"}, {"b"}, {"a"}} 

Example 2

Intent ->partition({"a", "c", "b", "c", 1, 3, "c", 3, 2, 1}) 
--> {{2}, {3, 3}, {1, 1}, {"b"}, {"c", "c", "c"}, {"a"}} 

Example 3

Intent ->partition({:a, :c, "b", :c, 1.1, 3, :c, 3, 2, 1}, key := :integer?) 
--> {{ 1, 2, 3, 3}, {c, 1.1, c, "b", c, a}} 
In this example, the items were partitioned into two lists - one list with integers and the other list with non-integers using the following custom function.
Function integer?(item As Any) As Boolean 
    integer? = (typeName(item) = :integer) 
End Function 

Example 4

Intent >partition({"abc", "123", "24", "xyz", "35"}, key := :length) 
--> {{"35", "24"}, {"xyz", "123", "abc"}} 
Result may not necessarily be in this order.