New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[stdlib] SR-361: Implement SE008: Add Lazy flatMap for Seq of Optionals #2461
Changes from 4 commits
1afa93b
408e8c7
b7329df
9af775b
7dc71d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,25 @@ extension LazySequenceProtocol { | |
FlattenSequence<LazyMapSequence<Elements, SegmentOfResult>>> { | ||
return self.map(transform).flatten() | ||
} | ||
|
||
/// 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. | ||
@warn_unused_result | ||
public func flatMap<U>( | ||
_ transform: (Self.Elements.Iterator.Element) -> U? | ||
) -> LazyMapSequence< | ||
LazyFilterSequence< | ||
LazyMapSequence<Self.Elements, U?>>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you drop There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also no problem |
||
U | ||
> { | ||
return self.map(transform).filter { $0 != nil }.map { $0! } | ||
} | ||
} | ||
|
||
extension LazyCollectionProtocol { | ||
|
@@ -42,6 +61,25 @@ extension LazyCollectionProtocol { | |
> { | ||
return self.map(transform).flatten() | ||
} | ||
|
||
/// 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. | ||
@warn_unused_result | ||
public func flatMap<U>( | ||
_ transform: (Self.Elements.Iterator.Element) -> U? | ||
) -> LazyMapCollection< | ||
LazyFilterCollection< | ||
LazyMapCollection<Self.Elements, U?>>, | ||
U | ||
> { | ||
return self.map(transform).filter { $0 != nil }.map { $0! } | ||
} | ||
} | ||
|
||
extension LazyCollectionProtocol | ||
|
@@ -67,4 +105,22 @@ extension LazyCollectionProtocol | |
>> { | ||
return self.map(transform).flatten() | ||
} | ||
|
||
/// 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<U>( | ||
_ transform: (Self.Elements.Iterator.Element) -> U? | ||
) -> LazyMapBidirectionalCollection< | ||
LazyFilterBidirectionalCollection< | ||
LazyMapBidirectionalCollection<Self.Elements, U?>>, | ||
U | ||
> { | ||
return self.map(transform).filter { $0 != nil }.map { $0! } | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The overloads for Sequence, Collection and BidirectionalCollection look very similar -- do you want to try gyb'ing them? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you change
U
toElementOfResult
?If you feel like doing so, could you also change the names of generic type parameters in the eager
flatMap
overloads (stdlib/public/core/SequenceAlgorithms.swift.gyb
) toSegmentOfResult
andElementOfResult
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that's no problem