mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
The rule changes are as follows: * All functions (introduced with the 'func' keyword) have argument labels for arguments beyond the first, by default. Methods are no longer special in this regard. * The presence of a default argument no longer implies an argument label. The actual changes to the parser and printer are fairly simple; the rest of the noise is updating the standard library, overlays, tests, etc. With the standard library, this change is intended to be API neutral: I've added/removed #'s and _'s as appropriate to keep the user interface the same. If we want to separately consider using argument labels for more free functions now that the defaults in the language have shifted, we can tackle that separately. Fixes rdar://problem/17218256. Swift SVN r27704
174 lines
5.7 KiB
Swift
174 lines
5.7 KiB
Swift
//===--- Map.swift - Lazily map over a SequenceType -----------*- swift -*-===//
|
|
//
|
|
// 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
/// The `GeneratorType` used by `MapSequenceView` and `MapCollectionView`.
|
|
/// Produces each element by passing the output of the `Base`
|
|
/// `GeneratorType` through a transform function returning `T`
|
|
public struct MapSequenceGenerator<
|
|
Base: GeneratorType, T
|
|
>: GeneratorType, SequenceType {
|
|
/// Advance to the next element and return it, or `nil` if no next
|
|
/// element exists.
|
|
///
|
|
/// Requires: `next()` has not been applied to a copy of `self`
|
|
/// since the copy was made, and no preceding call to `self.next()`
|
|
/// has returned `nil`.
|
|
public mutating func next() -> T? {
|
|
let x = _base.next()
|
|
if x != nil {
|
|
return _transform(x!)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/// `MapSequenceGenerator` is also a `SequenceType`, so it
|
|
/// `generate`\ 's a copy of itself
|
|
public func generate() -> MapSequenceGenerator {
|
|
return self
|
|
}
|
|
|
|
var _base: Base
|
|
var _transform: (Base.Element)->T
|
|
}
|
|
|
|
//===--- Sequences --------------------------------------------------------===//
|
|
|
|
/// A `SequenceType` whose elements consist of those in a `Base`
|
|
/// `SequenceType` passed through a transform function returning `T`.
|
|
/// These elements are computed lazily, each time they're read, by
|
|
/// calling the transform function on a base element.
|
|
public struct MapSequenceView<Base : SequenceType, T> : SequenceType {
|
|
/// Return a *generator* over the elements of this *sequence*.
|
|
///
|
|
/// Complexity: O(1)
|
|
public func generate() -> MapSequenceGenerator<Base.Generator,T> {
|
|
return MapSequenceGenerator(
|
|
_base: _base.generate(), _transform: _transform)
|
|
}
|
|
|
|
public func _prext_underestimateCount() -> Int {
|
|
return underestimateCount(_base)
|
|
}
|
|
|
|
var _base: Base
|
|
var _transform: (Base.Generator.Element)->T
|
|
}
|
|
|
|
/// Return an `Array` containing the results of mapping `transform`
|
|
/// over `source`.
|
|
public func map<S : SequenceType, T>(
|
|
source: S, _ transform: (S.Generator.Element) -> T
|
|
) -> [T] {
|
|
// FIXME(prext): remove this function when protocol extensions land.
|
|
return source._prext_map(transform)
|
|
}
|
|
|
|
/// Return an `Array` containing the results of mapping `transform`
|
|
/// over `source` and flattening the result.
|
|
public func flatMap<S : SequenceType, T>(
|
|
source: S, @noescape _ transform: (S.Generator.Element) -> [T]
|
|
) -> [T] {
|
|
// FIXME(prext): remove this function when protocol extensions land.
|
|
return source._prext_flatMap(transform)
|
|
}
|
|
|
|
//===--- Collections ------------------------------------------------------===//
|
|
|
|
/// A `CollectionType` whose elements consist of those in a `Base`
|
|
/// `CollectionType` passed through a transform function returning `T`.
|
|
/// These elements are computed lazily, each time they're read, by
|
|
/// calling the transform function on a base element.
|
|
public struct MapCollectionView<Base : CollectionType, T> : CollectionType {
|
|
/// The position of the first element in a non-empty collection.
|
|
///
|
|
/// In an empty collection, `startIndex == endIndex`.
|
|
public var startIndex: Base.Index {
|
|
return _base.startIndex
|
|
}
|
|
|
|
/// The collection's "past the end" position.
|
|
///
|
|
/// `endIndex` is not a valid argument to `subscript`, and is always
|
|
/// reachable from `startIndex` by zero or more applications of
|
|
/// `successor()`.
|
|
public var endIndex: Base.Index {
|
|
return _base.endIndex
|
|
}
|
|
|
|
/// Access the element at `position`.
|
|
///
|
|
/// Requires: `position` is a valid position in `self` and
|
|
/// `position != endIndex`.
|
|
public subscript(position: Base.Index) -> T {
|
|
return _transform(_base[position])
|
|
}
|
|
|
|
/// Return a *generator* over the elements of this *sequence*.
|
|
///
|
|
/// Complexity: O(1)
|
|
public func generate() -> MapSequenceView<Base, T>.Generator {
|
|
return MapSequenceGenerator(_base: _base.generate(), _transform: _transform)
|
|
}
|
|
|
|
public func _prext_underestimateCount() -> Int {
|
|
return underestimateCount(_base)
|
|
}
|
|
|
|
var _base: Base
|
|
var _transform: (Base.Generator.Element)->T
|
|
}
|
|
|
|
/// Return an `Array` containing the results of mapping `transform`
|
|
/// over `source`.
|
|
public func map<C : CollectionType, T>(
|
|
source: C, _ transform: (C.Generator.Element) -> T
|
|
) -> [T] {
|
|
// FIXME(prext): remove this function when protocol extensions land.
|
|
return source._prext_map(transform)
|
|
}
|
|
|
|
/// Return an `Array` containing the results of mapping `transform`
|
|
/// over `source` and flattening the result.
|
|
public func flatMap<C : CollectionType, T>(
|
|
source: C, _ transform: (C.Generator.Element) -> [T]
|
|
) -> [T] {
|
|
// FIXME(prext): remove this function when protocol extensions land.
|
|
return source._prext_flatMap(transform)
|
|
}
|
|
|
|
//===--- Support for lazy(s) ----------------------------------------------===//
|
|
|
|
% traversals = ('Forward', 'Bidirectional', 'RandomAccess')
|
|
% for View, Self in [('MapSequenceView', 'LazySequence')] + [
|
|
% ('MapCollectionView', 'Lazy%sCollection' % t) for t in traversals
|
|
% ]:
|
|
|
|
extension ${Self} {
|
|
/// Return a `${View}` over this `${Self}`. The elements of
|
|
/// the result are computed lazily, each time they are read, by
|
|
/// calling `transform` function on a base element.
|
|
public
|
|
func map<U>(
|
|
transform: (S.Generator.Element) -> U
|
|
) -> ${Self}<${View}<S, U>> {
|
|
return ${Self}<${View}<S, U>>(
|
|
${View}(_base: self._base, _transform: transform)
|
|
)
|
|
}
|
|
}
|
|
|
|
% end
|
|
|
|
// ${'Local Variables'}:
|
|
// eval: (read-only-mode 1)
|
|
// End:
|