Playground can't figure out how to run overloads

Here's some code I had in a playground, Xcode 11.3.1 on macOS 10.14.6:

extension Sequence {

    /// Returns a collection with the given type of collections containing the
    /// elements of the sequence, segregated into groups that share the same
    /// value, as determined by the given predicate.
    public func groupIsovalues<T>(into type: T.Type, by areEquivalent: (Element, Element) throws -> Bool) rethrows -> T where T: RangeReplaceableCollection & MutableCollection, T.Element: RangeReplaceableCollection, T.Element.Element == Element {
        var result = T()
        for element in self {
            if let resultIndex = try result.firstIndex(where: { try $0.first.map({ try areEquivalent($0, element) }) ?? false }) {
                result[resultIndex].append(element)
            } else {
                result.append(T.Element(repeating: element, count: 1))
            }
        }
        return result
    }

    /// Returns an array of collections with the given type, each containing
    /// elements of the sequence, segregated into groups that share the same
    /// value, as determined by the given predicate.
    @inlinable
    public func groupIsovalues<T>(eachInto type: T.Type, by areEquivalent: (Element, Element) throws -> Bool) rethrows -> [T] where T: RangeReplaceableCollection, T.Element == Element {
        return try groupIsovalues(into: Array.self, by: areEquivalent)
    }

    /// Returns an array of arrays, each inner one containing elements of the
    /// sequence, segregated into groups that share the same value, as
    /// determined by the given predicate.
    @inlinable
    public func groupIsovalues(by areEquivalent: (Element, Element) throws -> Bool) rethrows -> [[Element]] {
        return try groupIsovalues(eachInto: Array.self, by: areEquivalent)
    }

}

extension Sequence where Element: Equatable {

    /// Returns an array of arrays, each inner one containing elements of the
    /// sequence, segregated into groups with equal values.
    @inlinable
    public func groupIsovalues() -> [[Element]] {
        return groupIsovalues(by: ==)
    }

}

extension RangeReplaceableCollection {

    /// Returns a collection of the given type with instances containing the
    /// elements of the collection, segregated into groups that share the same
    /// value, as determined by the given predicate.
    @inlinable
    public func groupIsovalues<T>(into type: T.Type, by areEquivalent: (Element, Element) throws -> Bool) rethrows -> T where T: RangeReplaceableCollection & MutableCollection, T.Element == Self {
        return try groupIsovalues(into: T.self, by: areEquivalent)
    }

    /// Returns an array of instances, each containing elements of the
    /// collection, segregated into groups that share the same value, as
    /// determined by the given predicate.
    @inlinable
    public func groupIsovalues(by areEquivalent: (Element, Element) throws -> Bool) rethrows -> [Self] {
        return try groupIsovalues(into: Array.self, by: areEquivalent)
    }

}

extension RangeReplaceableCollection where Element: Equatable {

    /// Returns an array of instances, each containing elements of the
    /// collection, segregated into groups with equal values.
    @inlinable
    public func groupIsovalues() -> [Self] {
        return groupIsovalues(by: ==)
    }

}

let groupingSample = [1, 2, 3, 1, 1, 2, 3, 4, 5, 6]
var groupResult = AnySequence(groupingSample).groupIsovalues()
groupResult = AnySequence(groupingSample).groupIsovalues { $0.magnitude % 2 == $1.magnitude % 2 }
groupResult

groupResult = groupingSample.groupIsovalues()
groupResult = groupingSample.groupIsovalues { $0.magnitude % 2 == $1.magnitude % 2 }
groupResult

I wrote the code for the Sequence extension blocks, then tested only the first set of groupResult code. They worked just fine. Then I added the RangeReplaceableCollection extension blocks with the overloads and the second set of groupResult code. Running that second set of code locks the playground. It seems to run forever without progress, and right now pressing the stop button runs forever without progress. I can still quit though.

However, the progress sidebar in the playground didn't show anything new. I would expect some lines of code rapidly going into the thousands, but I didn't see anything. Either the soft-lock kept the playground sidebar from updating, or the part of the system that determines which overload to use (since a RRC qualifies for both the RRC overloads and the Sequence originals) locked up. That latter case would be more disturbing.

Maybe someone here could figure out which one is happening, so we can determine if a bug report is needed.

Moving the groupIsovalues methods to a new file makes only a slight difference. (The groupResult test code is still in the playground.) Running the Sequence versions still work just fine. Running the RRC overloads still lock the playground. However, pressing the stop button works instantaneously instead of also locking.

Terms of Service

Privacy Policy

Cookie Policy