mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
In a naive implementation of a Sequence adapter, adapting a non-self-destructive (multi-pass) Sequence having a reference-semantics Generator produces a self-destructive adapted view. This is technically correct because Sequences are allowed to be self-destructive, and theoretically every multi-pass Sequence would be a Collection. However Sequences are much easier to build than Collections, so users are likely to make them, and it would be extremely surprising if adapting a self-preserving Sequence yielded a self-destructive one. Swift SVN r18350
72 lines
1.9 KiB
Swift
72 lines
1.9 KiB
Swift
//===--- Map.swift - Lazily map the elements of a Sequence ---------------===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
struct MapSequenceGenerator<Base: Generator, T>: Generator, Sequence {
|
|
mutating func next() -> T? {
|
|
let x = _base.next()
|
|
if x {
|
|
return _transform(x!)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func generate() -> MapSequenceGenerator {
|
|
return self
|
|
}
|
|
|
|
var _base: Base
|
|
var _transform: (Base.Element)->T
|
|
}
|
|
|
|
struct MapSequenceView<Base: Sequence, T> : Sequence {
|
|
func generate() -> MapSequenceGenerator<Base.GeneratorType,T> {
|
|
return MapSequenceGenerator(
|
|
_base: _base.generate(), _transform: _transform)
|
|
}
|
|
|
|
var _base: Base
|
|
var _transform: (Base.GeneratorType.Element)->T
|
|
}
|
|
|
|
struct MapCollectionView<Base: Collection, T> : Collection {
|
|
var startIndex: Base.IndexType {
|
|
return _base.startIndex
|
|
}
|
|
|
|
var endIndex: Base.IndexType {
|
|
return _base.endIndex
|
|
}
|
|
|
|
subscript(index: Base.IndexType) -> T {
|
|
return _transform(_base[index])
|
|
}
|
|
|
|
func generate() -> MapSequenceView<Base, T>.GeneratorType {
|
|
return MapSequenceGenerator(_base: _base.generate(), _transform: _transform)
|
|
}
|
|
|
|
var _base: Base
|
|
var _transform: (Base.GeneratorType.Element)->T
|
|
}
|
|
|
|
func map<S:Sequence, T>(
|
|
source: S, transform: (S.GeneratorType.Element)->T
|
|
) -> MapSequenceView<S, T> {
|
|
return MapSequenceView(_base: source, _transform: transform)
|
|
}
|
|
|
|
func map<C:Collection, T>(
|
|
source: C, transform: (C.GeneratorType.Element)->T
|
|
) -> MapCollectionView<C, T> {
|
|
return MapCollectionView(_base: source, _transform: transform)
|
|
}
|