Skip to content
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

Merged
merged 5 commits into from May 11, 2016
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
56 changes: 56 additions & 0 deletions stdlib/public/core/FlatMap.swift
Expand Up @@ -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>(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change U to ElementOfResult?

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) to SegmentOfResult and ElementOfResult?

Copy link
Contributor Author

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

_ transform: (Self.Elements.Iterator.Element) -> U?
) -> LazyMapSequence<
LazyFilterSequence<
LazyMapSequence<Self.Elements, U?>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you drop Self. qualifications? They are not necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 {
Expand All @@ -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
Expand All @@ -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! }
}
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

}
24 changes: 24 additions & 0 deletions test/1_stdlib/Inputs/flatMap.gyb
Expand Up @@ -64,5 +64,29 @@ ${Test}.test("flatMap/${Kind}/Lazy") {
+ "once per element")
% end
}

for test in flatMapToOptionalTests {
let s = ${Minimal}<OpaqueValue<Int>>(elements:
test.sequence.map(OpaqueValue.init))
let closureLifetimeTracker = LifetimeTracked(0)
var timesClosureWasCalled = 0

var result = s.lazy.flatMap {
(element: OpaqueValue<Int>) -> OpaqueValue<Int32>? in
_blackHole(closureLifetimeTracker)
timesClosureWasCalled += 1
return test.transform(element.value).map(OpaqueValue.init)
}
expectEqual(0, timesClosureWasCalled, "unexpected eagerness")

let expected = test.expected.map(OpaqueValue.init)
expectEqualSequence(
expected, result,
stackTrace: SourceLocStack().with(test.loc)
) { $0.value == $1.value }
expectEqual(
test.sequence.count, timesClosureWasCalled,
"iterating lazy flatMap() should call closure once per element")
}
}
% end