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

[Compacted] Adding compacted() method to remove all nils in a sequence or collection #112

Merged
merged 5 commits into from
Apr 5, 2021

Conversation

LucianoPAlmeida
Copy link
Contributor

Fixes #107

From the sketch from @kylemacomber on the issue, this just complement the implementation adding the tests and docs.
Let me know what you think =]

cc @kylemacomber @natecook1000 @timvermeulen

Replace this paragraph with a description of your changes and rationale. Provide links to an existing issue or external references/discussions, if appropriate.

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

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

Thanks for taking this one on, @LucianoPAlmeida! 👏 A few notes below about indexes and conformances.

Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
@LucianoPAlmeida
Copy link
Contributor Author

@natecook1000 @kylemacomber Thank you for the review :)

Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

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

Looking good, @LucianoPAlmeida! Some more notes for you below, then I think we just need docs on the methods themselves.

Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Sources/Algorithms/Compacted.swift Outdated Show resolved Hide resolved
Tests/SwiftAlgorithmsTests/CompactedTests.swift Outdated Show resolved Hide resolved
@LucianoPAlmeida
Copy link
Contributor Author

@natecook1000 Thanks again and sorry for those mistakes, I think all things were addressed =]

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please test

Copy link
Member

@natecook1000 natecook1000 left a comment

Choose a reason for hiding this comment

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

Almost there! Just a couple notes left.

Comment on lines 178 to 179
extension CompactedCollection: RandomAccessCollection
where Base: RandomAccessCollection {}
Copy link
Member

Choose a reason for hiding this comment

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

I missed this — because we don't know how far apart the non-nil elements are until we iterate, we can't make CompactedCollection conform to RandomAccessCollection.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense :)


func testCollectionTraversals() {
for array in self.tests {
validateIndexTraversals(array)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we need to call compacted() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for that

Comment on lines 45 to 67
func testCollectionEquatableConformances() {
for array in self.tests {
XCTAssertEqual(
array.eraseToAnyHashableSequence().compacted(),
array.compactMap({ $0 }).eraseToAnyHashableSequence().compacted()
)
XCTAssertEqual(
array.compacted(), array.compactMap({ $0 }).compacted()
)
}
}

func testCollectionHashableConformances() {
for array in self.tests {
let seq = array.eraseToAnyHashableSequence()
XCTAssertEqualHashValue(
seq.compacted(), seq.compactMap({ $0 }).compacted()
)
XCTAssertEqualHashValue(
array.compacted(), array.compactMap({ $0 }).compacted()
)
}
}
Copy link
Member

Choose a reason for hiding this comment

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

For these two tests it would be great to make sure that equality and hashing work for non-equal base sequences/collections that are equal when compacted. E.g. [1, 2, 3, nil, nil, 4].compacted() == [1, nil, 2, nil, 3, 4].compacted(). You're covering the correctness of compacting already, so I don't think you need to do the comparison with compactMap { $0 } any more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adjusted :)

@LucianoPAlmeida LucianoPAlmeida force-pushed the compact branch 3 times, most recently from 6aebc1f to 3db1df0 Compare March 31, 2021 21:30

@inlinable
public func index(after i: Index) -> Index {
precondition(i < endIndex, "Index out of bounds")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
precondition(i < endIndex, "Index out of bounds")
precondition(i != endIndex, "Index out of bounds")

We try to compare indices using != wherever possible to be consistent with the stdlib, also see #65 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah ok, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! :)

@natecook1000
Copy link
Member

@swift-ci Please test

@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please test

@natecook1000
Copy link
Member

:shipit:

@natecook1000 natecook1000 merged commit 198eee0 into apple:main Apr 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add compacted() as a convenience for lazy.compactMap { $0 }
4 participants