Skip to main content

Array.partition_with_index

partition_with_indexpredicate

Documentation

Partitions the array into Vectors of elements which satisfy a given predicate and ones that do not. Returns a Pair whose first element is the Vector of elements satisfying the predicate and the second element is a Vector of elements which did not satisfy it. The relative order of elements kept in each returned list is the same as in the input array.

Arguments

  • predicate: A function that takes an index and an element and returns a boolean value.

Examples

Splitting an array into elements at even and odd positions.

      ["a", "b", "c", "d"].to_array.partition_with_index (ix -> _ -> ix % 2 == 0) == (Pair ["a", "c"].to_array ["b", "d"].to_array)