//===--- 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( _ transform: @escaping (Elements.Iterator.Element) -> SegmentOfResult ) -> LazySequence< FlattenSequence>> { 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( _ transform: @escaping (Elements.Iterator.Element) -> ElementOfResult? ) -> LazyMapSequence< LazyFilterSequence< LazyMapSequence>, 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( _ transform: @escaping (Elements.Iterator.Element) -> SegmentOfResult ) -> LazyCollection< FlattenCollection< LazyMapCollection> > { 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( _ transform: @escaping (Elements.Iterator.Element) -> ElementOfResult? ) -> LazyMapCollection< LazyFilterCollection< LazyMapCollection>, 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( _ transform: @escaping (Elements.Iterator.Element) -> SegmentOfResult ) -> LazyCollection< FlattenBidirectionalCollection< LazyMapBidirectionalCollection>> 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( _ transform: @escaping (Elements.Iterator.Element) -> ElementOfResult? ) -> LazyMapBidirectionalCollection< LazyFilterBidirectionalCollection< LazyMapBidirectionalCollection>, ElementOfResult > { return self.map(transform).filter { $0 != nil }.map { $0! } } }