Files
swift-mirror/stdlib/public/core/FlatMap.swift
practicalswift 797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00

119 lines
4.2 KiB
Swift

//===--- FlatMap.swift ----------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
extension LazySequenceProtocol {
/// Returns the concatenated results of mapping `transform` over
/// `self`. Equivalent to
///
/// self.map(transform).joined()
///
/// - Complexity: O(1)
public func flatMap<SegmentOfResult : Sequence>(
_ transform: @escaping (Elements.Iterator.Element) -> SegmentOfResult
) -> LazySequence<
FlattenSequence<LazyMapSequence<Elements, SegmentOfResult>>> {
return self.map(transform).joined()
}
/// Returns a `LazyMapSequence` containing the concatenated non-nil
/// results of mapping transform over this `Sequence`.
///
/// Use this method to receive only nonoptional values when your
/// transformation produces an optional value.
///
/// - Parameter transform: A closure that accepts an element of this
/// sequence as its argument and returns an optional value.
public func flatMap<ElementOfResult>(
_ transform: @escaping (Elements.Iterator.Element) -> ElementOfResult?
) -> LazyMapSequence<
LazyFilterSequence<
LazyMapSequence<Elements, ElementOfResult?>>,
ElementOfResult
> {
return self.map(transform).filter { $0 != nil }.map { $0! }
}
}
extension LazyCollectionProtocol {
/// Returns the concatenated results of mapping `transform` over
/// `self`. Equivalent to
///
/// self.map(transform).joined()
///
/// - Complexity: O(1)
public func flatMap<SegmentOfResult : Collection>(
_ transform: @escaping (Elements.Iterator.Element) -> SegmentOfResult
) -> LazyCollection<
FlattenCollection<
LazyMapCollection<Elements, SegmentOfResult>>
> {
return self.map(transform).joined()
}
/// Returns a `LazyMapCollection` containing the concatenated non-nil
/// results of mapping transform over this collection.
///
/// Use this method to receive only nonoptional values when your
/// transformation produces an optional value.
///
/// - Parameter transform: A closure that accepts an element of this
/// collection as its argument and returns an optional value.
public func flatMap<ElementOfResult>(
_ transform: @escaping (Elements.Iterator.Element) -> ElementOfResult?
) -> LazyMapCollection<
LazyFilterCollection<
LazyMapCollection<Elements, ElementOfResult?>>,
ElementOfResult
> {
return self.map(transform).filter { $0 != nil }.map { $0! }
}
}
extension LazyCollectionProtocol
where
Self : BidirectionalCollection,
Elements : BidirectionalCollection
{
/// Returns the concatenated results of mapping `transform` over
/// `self`. Equivalent to
///
/// self.map(transform).joined()
///
/// - Complexity: O(1)
public func flatMap<SegmentOfResult : Collection>(
_ transform: @escaping (Elements.Iterator.Element) -> SegmentOfResult
) -> LazyCollection<
FlattenBidirectionalCollection<
LazyMapBidirectionalCollection<Elements, SegmentOfResult>>>
where SegmentOfResult : BidirectionalCollection {
return self.map(transform).joined()
}
/// Returns a `LazyMapBidirectionalCollection` containing the concatenated non-nil
/// results of mapping transform over this collection.
///
/// Use this method to receive only nonoptional values when your
/// transformation produces an optional value.
///
/// - Parameter transform: A closure that accepts an element of this
/// collection as its argument and returns an optional value.
public func flatMap<ElementOfResult>(
_ transform: @escaping (Elements.Iterator.Element) -> ElementOfResult?
) -> LazyMapBidirectionalCollection<
LazyFilterBidirectionalCollection<
LazyMapBidirectionalCollection<Elements, ElementOfResult?>>,
ElementOfResult
> {
return self.map(transform).filter { $0 != nil }.map { $0! }
}
}