<div dir="ltr"><div dir="auto" style="word-wrap:break-word"><pre style="white-space:pre-wrap;background-color:rgb(255,255,255)">Consider this:
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:
<a href="https://github.com/SusanDoggie/Doggie/blob/master/Doggie/Foundation.swift" target="_blank">https://github.com/SusanDoggie/Doggie/blob/master/Doggie/Foundation.swift</a></pre><div><br></div><div style="direction:ltr"><blockquote type="cite"><div>Kevin Ballard <<a href="mailto:kevin@sb.org" target="_blank">kevin@sb.org</a>> 於 2015年12月29日 上午7:59 寫道:</div><br><div><div>## Introduction<br><br>Add a few more functional sequence utilities to the standard library.<br><br>## Motivation<br><br>We have map, filter, and reduce, but we're missing a bunch of useful utilities like scan, iterate, takeWhile, and dropWhile. Interestingly, the stdlib includes an implementation of scan in the doc comment for LazySequenceType, it just doesn't actually provide it as API.<br><br>## Proposed solution<br><br>We extend SequenceType with 3 new methods scan, takeWhile, and dropWhile. We also add a single global function iterate.<br><br>## Detailed design<br><br>We add the following extension to SequenceType:<br><br>extension SequenceType {<br> func scan<T>(initial: T, @noescape combine: (T, Self.Generator.Element) throws -> T) rethrows -> [T]<br> func dropWhile(@noescape dropElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element]<br> func takeWhile(@noescape takeElement: (Self.Generator.Element) throws -> Bool) rethrows -> [Self.Generator.Element]<br>}<br><br>These all take functions, so to follow convention they're @noescape and return arrays. We also provide an extension of CollectionType that overrides a couple of these methods:<br><br>extension CollectionType {<br> func dropWhile(@noescape dropElement: (Self.Generator.Element) throws -> Bool) rethrows -> Self.SubSequence<br> func takeWhile(@noescape takeElement: (Self.Generator.Element) throws -> Bool) rethrows -> Self.SubSequence<br>}<br><br>We also provide lazy versions:<br><br>extension LazySequenceType {<br> func scan<T>(initial: T, combine: (T, Self.Generator.Element) -> T) -> LazyScanSequence<Self.Elements, T><br> func dropWhile(dropElement: (Self.Generator.Element) -> Bool) -> LazyDropWhileSequence<Self.Elements><br> func takeWhile(takeElement: (Self.Generator.Element) -> Bool) -> LazyTakeWhileSequence<Self.Elements><br>}<br><br>extension LazyCollectionType {<br> func dropWhile(dropElement: (Self.Generator.Element) -> Bool) -> LazyDropWhileCollection<Self.Elements><br> func takeWhile(takeElement: (Self.Generator.Element) -> Bool) -> LazyTakeWhileCollection<Self.Elements><br>}<br><br>No collection variant of scan is provided because that would require storing the last value in the index itself, which would cause problems if the combine function isn't pure.<br><br>LazyDropWhileCollection would behave similarly to LazyFilterCollection in that it runs the predicate against the elements to drop when accessing startIndex; unlike LazyFilterCollection, because there's nothing else to skip after that point, the index itself can actually be Self.Elements.Index (just like a slice). LazyTakeWhileCollection also runs the predicate against the first element when accessing startIndex, but it does need a unique index type (because endIndex has to be some sentinel value, as it doesn't know where the end is until you reach that point; this index type would therefore only conform to ForwardIndexType).<br><br>And finally, we provide a global function<br><br>func iterate<T>(initial: T, _ f: T -> T) -> IterateSequence<T><br><br>This function is inherently lazy and yields an infinite list of nested applications of the function, so iterate(x, f) yields a sequence like [x, f(x), f(f(x)), ...].<br><br>-Kevin Ballard<br></div></div></blockquote></div><br></div></div>