Skip to content

Added documentation and where clause for API uniqued() #32

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

Merged
merged 22 commits into from
Oct 26, 2020

Conversation

Roshankumar350
Copy link
Contributor

Problem:
* A quick look up for the API named uniqued() with example associated with it was missing.
* API named uniqued() while enumerating each element was checking an if condition.

Solution:
* API documentation added to address the same.
* where clause added for the same

Checklist

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

@Roshankumar350 Roshankumar350 changed the title Readability Added documentation and where clause for API uniqued() Oct 22, 2020
@xwu
Copy link
Contributor

xwu commented Oct 22, 2020

Good catch on the typo!

I've followed the code style of the rest of the project

Note that the code style involves indentations with two spaces, and also wrapping to 80 columns on each line. Sentences end with a period.

/// order of the first occurrence of each unique element.
///
/// In this example, `uniqued()` is used to include only names which are
/// unique
Copy link
Contributor

@xwu xwu Oct 23, 2020

Choose a reason for hiding this comment

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

Thanks, the formatting is now mostly consistent. Make sure to include periods at the end of sentences, like here (also elsewhere). There is still another closing brace that is incorrectly indented. There are also several places where you have two consecutive spaces in the comment or the code; please make sure to remove them. There are also places where there are missing spaces after punctuation: please make sure to add them.

A more general comment here: I don’t think it adds anything to describe the example here by saying that uniqued makes the array unique. I think this line could be removed.

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 filling out this documentation, @Roshankumar350! A few notes below for you.

/// unique element.
///
/// let animals = ["dog", "pig", "cat", "ox", "dog","cat"]
/// let uniqued = animals(on: { $0.first })
Copy link
Contributor

Choose a reason for hiding this comment

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

This does not compile.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Addressed and tested the same example on compiler.

/// let animals = ["dog", "pig", "cat", "ox", "dog","cat"]
/// let uniqued = animals(on: { $0.first })
/// print(uniqued)
/// // Prints "["dog", "pig","cat", "ox"]"
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
/// // Prints "["dog", "pig","cat", "ox"]"
/// // Prints '["dog", "pig", "cat", "ox"]'

Although the output depends on what the projection chosen for this example is.

///
/// - Parameter projection: A projecting closure. `projection` accepts an
/// element of this sequence as its parameter and returns a projected
/// value of the same type.
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
/// value of the same type.
/// value of the same type.

This description is true, but it’s also just describing the type signature in words. But what are the semantics expected of this closure? That should be documented 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.

Added constraint of "Hashable" element

@@ -42,14 +42,14 @@ extension Sequence {
/// by the given projection), in the order of the first occurrence of each
/// unique element.
///
/// let animals = ["dog", "pig", "cat", "ox", "dog","cat"]
/// let uniqued = animals(on: { $0.first })
/// let animals = ["dog", "pig", "cat", "ox", "dog", "cat"]
Copy link
Contributor

Choose a reason for hiding this comment

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

This example doesn't really demonstrate the role of the 'projection' closure. If you want to stick to the projection $0.first, I'd suggest something like:

Suggested change
/// let animals = ["dog", "pig", "cat", "ox", "dog", "cat"]
/// let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]

///
/// - Parameter projection: A projecting closure. `projection` accepts an
/// element of this sequence as its parameter and returns a projected
/// value of the same type.
/// value of the same type having constraint `Hashable`.
Copy link
Contributor

@xwu xwu Oct 24, 2020

Choose a reason for hiding this comment

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

The addition about 'Hashable' is useful, but the first part of this line is not correct: the projected value does not need to be of the same type.

But moreover, even when corrected, this statement is still describing the type of the parameter in words, which is not necessary and doesn't add to a user's understanding, assuming that they know how to read Swift type signatures. The challenge here is to teach the user why and how to use a projection function to determine uniqueness.

/// - Parameter projection: A projecting closure. `projection` accepts an
/// element of this sequence as its parameter which is having the type of
/// projecting element and returns a projected value that may have the same
/// type having constraint `Hashable`.
Copy link
Contributor

Choose a reason for hiding this comment

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

@natecook1000 Do you have suggestions of how to approach describing this parameter?

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.

Getting close, @Roshankumar350!

Comment on lines 50 to 53
/// - Parameter projection: A projecting closure. `projection` accepts an
/// element of this sequence as its parameter which is having the type of
/// projecting element and returns a projected value that may have the same
/// type having constraint `Hashable`.
Copy link
Member

Choose a reason for hiding this comment

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

How about this?

Suggested change
/// - Parameter projection: A projecting closure. `projection` accepts an
/// element of this sequence as its parameter which is having the type of
/// projecting element and returns a projected value that may have the same
/// type having constraint `Hashable`.
/// - Parameter projection: A closure that transforms an element into the
/// value to use for uniqueness. If `projection` returns the same value
/// for two different elements, the second element will be excluded
/// from the resulting array.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@natecook1000 Thanks for forward thinking.

Comment on lines +45 to +46
/// let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]
/// let uniqued = animals.uniqued(on: {$0.first})
Copy link
Member

Choose a reason for hiding this comment

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

Let's give an introduction to this example, as well:

Suggested change
/// let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]
/// let uniqued = animals.uniqued(on: {$0.first})
/// This example finds the elements of the `animals` array with unique
/// first characters:
///
/// let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]
/// let uniqued = animals.uniqued(on: { $0.first })

@xwu
Copy link
Contributor

xwu commented Oct 25, 2020

This looks great!

Final step: looks like there's a conflict between this change and the main branch, and the revisions here are best as one commit instead of 18. So please squash and rebase this PR. Here's an introductory guide to doing that from the folks at Servo:

https://github.com/servo/servo/wiki/Beginner%27s-guide-to-rebasing-and-squashing

*Added documentation
*Added documentation
*consistant Indentation
*1. Indentations with two spaces.
*2. Wrapping to 80 columns on each line.
*Indentations with two spaces for functions.
*Example that demonstrates the uniqued(on:) method.
*Addressing comment
*Reverting lateral change.
*Reverting lateral change
*Addressing comment.
*minor correction.
*Addressing feedback.
*Addressing feedback.
*Addressing feedback.
*Spacing
*spacing
*Addressing feedback.
/// unique element.
@inlinable
Copy link
Contributor

Choose a reason for hiding this comment

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

I don’t think this was rebased correctly. These annotations shouldn’t be deleted.

@natecook1000
Copy link
Member

Looks great, @Roshankumar350 — thanks for sticking with this! 🎉

@natecook1000 natecook1000 merged commit 66ec968 into apple:main Oct 26, 2020
@Roshankumar350 Roshankumar350 deleted the Readability branch October 27, 2020 05:14
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.

3 participants