# Proposal: Add scan, takeWhile, dropWhile, and iterate to the stdlib

**URL:** <https://forums.swift.org/t/proposal-add-scan-takewhile-dropwhile-and-iterate-to-the-stdlib/816>\
**Category:** Discussion\
**Created:** [December 29, 2015, 2:28am UTC](https://forums.swift.org/t/proposal-add-scan-takewhile-dropwhile-and-iterate-to-the-stdlib/816 "2015-12-29T02:28:20Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![SusanCheng](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/susancheng/32/9439_2.png) [@SusanCheng](https://forums.swift.org/u/SusanCheng)\
**Post date:** [December 29, 2015, 2:28am UTC](https://forums.swift.org/t/proposal-add-scan-takewhile-dropwhile-and-iterate-to-the-stdlib/816/1 "2015-12-29T02:28:20Z")

</div>

Consider:

extension CollectionType where Generator.Element : Equatable {

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

extension CollectionType {

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

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

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

and here are my utilities:  
[https://github.com/SusanDoggie/Doggie/blob/master/Doggie/Foundation.swift](https://github.com/SusanDoggie/Doggie/blob/master/Doggie/Foundation.swift)
