[swift-evolution] Proposal: Add scan, takeWhile, dropWhile, and iterate to the stdlib

Susan Cheng susan.doggie at gmail.com
Mon Dec 28 20:28:20 CST 2015


Consider:

extension CollectionType where Generator.Element : Equatable {

    /// Returns a subsequence, until a element equal to `value`, containing
the
    /// initial elements.
    ///
    /// If none of elements equal to `value`, the result contains all
    /// the elements of `self`.
    ///
    /// - Complexity: O(`self.count`)
    @warn_unused_result
    public func prefixUntil(element: Self.Generator.Element) ->
Self.SubSequence {
        return self.prefixUpTo(self.indexOf(element) ?? self.endIndex)
    }
}


extension CollectionType {

    /// Returns a subsequence, until a element satisfying the predicate,
containing the
    /// initial elements.
    ///
    /// If none of elements satisfying the predicate, the result contains
all
    /// the elements of `self`.
    ///
    /// - Complexity: O(`self.count`)
    @warn_unused_result
    public func prefixUntil(@noescape predicate: (Self.Generator.Element)
throws -> Bool) rethrows -> Self.SubSequence {
        return self.prefixUpTo(try self.indexOf(predicate) ?? self.endIndex)
    }
}


extension CollectionType where Generator.Element : Equatable, Index :
BidirectionalIndexType {
    /// Returns a subsequence, until a element equal to `value`, containing
the
    /// final elements of `self`.
    ///
    /// If none of elements equal to `value`, the result contains all
    /// the elements of `self`.
    ///
    /// - Complexity: O(`self.count`)
    @warn_unused_result
    public func suffixUntil(element: Self.Generator.Element) ->
Self.SubSequence {
        return self.suffixFrom(self.reverse().indexOf(element)?.base ??
self.startIndex)
    }
}


extension CollectionType where Index : BidirectionalIndexType {
    /// Returns a subsequence, until a element satisfying the predicate,
containing the
    /// final elements of `self`.
    ///
    /// If none of elements satisfying the predicate, the result contains
all
    /// the elements of `self`.
    ///
    /// - Complexity: O(`self.count`)
    @warn_unused_result
    public func suffixUntil(@noescape predicate: (Self.Generator.Element)
throws -> Bool) rethrows -> Self.SubSequence {
        return self.suffixFrom(try self.reverse().indexOf(predicate)?.base
?? self.startIndex)
    }
}

and here are my utilities:
https://github.com/SusanDoggie/Doggie/blob/master/Doggie/Foundation.swift
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151229/e6e0bb2a/attachment.html>


More information about the swift-evolution mailing list