Skip to content

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

Merged
natecook1000 merged 5 commits into
apple:mainfrom
LucianoPAlmeida:compact
Apr 5, 2021
Merged

[Compacted] Adding compacted() method to remove all nils in a sequence or collection#112
natecook1000 merged 5 commits into
apple:mainfrom
LucianoPAlmeida:compact

Conversation

@LucianoPAlmeida
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Tests/SwiftAlgorithmsTests/CompactedTests.swift Outdated
@LucianoPAlmeida
Copy link
Copy Markdown
Contributor Author

@natecook1000 @kylemacomber Thank you for the review :)

Comment thread Sources/Algorithms/Compacted.swift Outdated
Copy link
Copy Markdown
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.

Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Sources/Algorithms/Compacted.swift Outdated
Comment thread Tests/SwiftAlgorithmsTests/CompactedTests.swift Outdated
@LucianoPAlmeida
Copy link
Copy Markdown
Contributor Author

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

@LucianoPAlmeida
Copy link
Copy Markdown
Contributor Author

@swift-ci Please test

Copy link
Copy Markdown
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 thread Sources/Algorithms/Compacted.swift Outdated
Comment on lines +178 to +179
extension CompactedCollection: RandomAccessCollection
where Base: RandomAccessCollection {}
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Comment thread Sources/Algorithms/Compacted.swift Outdated

@inlinable
public func index(after i: Index) -> Index {
precondition(i < endIndex, "Index out of bounds")
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed! :)

Comment thread Sources/Algorithms/Compacted.swift Outdated
@natecook1000
Copy link
Copy Markdown
Member

@swift-ci Please test

@LucianoPAlmeida
Copy link
Copy Markdown
Contributor Author

@swift-ci Please test

@natecook1000
Copy link
Copy Markdown
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