17. 普通のreverseっぽい
extension SequenceType {
/// Return an `Array` containing the elements of `self` in reverse
/// order.
///
/// Complexity: O(N), where N is the length of `self`.
@warn_unused_result
public func reverse() -> [Self.Generator.Element]
}
18. 発見!O(1)とか書いてる
extension CollectionType where Index : BidirectionalIndexType {
/// Return the elements of `self` in reverse order.
///
/// - Complexity: O(1)
@warn_unused_result
public func reverse() -> ReverseCollection<Self>
}
extension CollectionType where Index : RandomAccessIndexType {
/// Return the elements of `self` in reverse order.
///
/// - Complexity: O(1)
@warn_unused_result
public func reverse() -> ReverseRandomAccessCollection<Self>
}
19. ReverseCollection
/// A Collection that presents the elements of its `Base` collection
/// in reverse order.
///
/// - Note: This type is the result of `x.reverse()` where `x` is a
/// collection having bidirectional indices.
///
/// The `reverse()` method is always lazy when applied to a collection
/// with bidirectional indices, but does not implicitly confer
/// laziness on algorithms applied to its result. In other words, for
/// ordinary collections `c` having bidirectional indices:
///
/// * `c.reverse()` does not create new storage
/// * `c.reverse().map(f)` maps eagerly and returns a new array
/// * `c.lazy.reverse().map(f)` maps lazily and returns a `LazyMapCollection`
///
/// - See also: `ReverseRandomAccessCollection`
20. ReverseRandomAccessCollection
/// A Collection that presents the elements of its `Base` collection
/// in reverse order.
///
/// - Note: This type is the result of `x.reverse()` where `x` is a
/// collection having random access indices.
/// - See also: `ReverseCollection`
21. この型にだけextensionを実装してみる
extension CollectionType where Index : RandomAccessIndexType {
func foldr_loop2<T>(accm:T, @noescape f: (Self.Generator.Element, T) -> T) -> T {
var result = accm
for temp in self.reverse() {
result = f(temp, result)
}
return result
}
func foldr_reduce2<T>(accm:T, @noescape f: (T, Self.Generator.Element) -> T) -> T {
return self.reverse().reduce(accm) { f($0, $1) }
}
func foldr_forEach2<T>(accm:T, @noescape f: (Self.Generator.Element, T) -> T) -> T {
var result = accm
self.reverse().forEach { (t) -> () in
result = f(t, result)
}
return result
}
}