apple/swift-algorithms

Clarify `endIndex` behavior of `partitioningIndex(where:)` in documentation

mdznr opened this issue · 0 comments

mdznr commented

/// Returns the index of the first element in the collection that matches
/// the predicate.
///
/// The collection must already be partitioned according to the predicate.
/// That is, there should be an index `i` where for every element in
/// `collection[..<i]` the predicate is `false`, and for every element in
/// `collection[i...]` the predicate is `true`.
///
/// - Parameter belongsInSecondPartition: A predicate that partitions the
/// collection.
/// - Returns: The index of the first element in the collection for which
/// `predicate` returns `true`.

In the documentation for partitioningIndex(where:), it only mentions what happens when the predicate returns true. But, if the predicate returns false in all cases, it is unclear what the return value is. The documentation should be written so that it is clear that the return value is endIndex if belongsInSecondPartition returns false for all elements.

I think this should at least be reflected in the “Returns” section of the inline documentation, but could probably be made more clear by rephrasing the first few sentences. The first sentence makes it sound like this is identical to firstIndex(where:), but the second and third sentences make the behavior more clear, indicating that this function is only to be used on collections that have already been partitioned.