Skip to content

Added Extra functions #5

Open
wmwang52 wants to merge 53 commits intomainfrom
InsertSorted
Open

Added Extra functions #5
wmwang52 wants to merge 53 commits intomainfrom
InsertSorted

Conversation

@wmwang52
Copy link
Copy Markdown

Added Extra Functions to the Toolbox Pacagek for more flexible data structure features

@wmwang52 wmwang52 requested a review from myattj March 4, 2024 22:16
Copy link
Copy Markdown

@myattj myattj left a comment

Choose a reason for hiding this comment

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

This looks generally good to me, no syntax stuff or big logical issues. But this feels a little above my pay grade to approve, since more teams use appteamtoolbox, so maybe request @maxnabokow or @samrshi

@myattj myattj requested a review from maxnabokow March 18, 2024 20:55
@maxnabokow
Copy link
Copy Markdown
Member

Okay, I'll take a look. Which ticket are we making this change for? @wmwang52 @myattj

Copy link
Copy Markdown
Member

@samrshi samrshi left a comment

Choose a reason for hiding this comment

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

Left a few comments. Let me know what you think

Comment on lines +47 to +51
if index >= count {
items.append(element)
} else {
items.insert(element, at: index)
}
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.

what happens if index is < 0? we should probably handle that case, since we currently allow/support passing -1 as a param from other places

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

When we use the insertAt function within Centible, it's only passed positive values since it's using existing indexes, so that's a good point I didn't think about. I suppose if it's less than zero we can just instantly return it just as a precaution.

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.

you could also maybe prepend it to the list

Comment on lines +63 to +64
// If the index is equal to 0, it indicates that the elements passed through as an argument is a larger array, and items is the subset of elements.
if index == -1 {
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.

Is this comment accurate? Seeing "if this index is equal to 0" immediately followed by if index == -1 is kind of funky. What's going on here?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

That's a typo, I'll fix that comment

Comment on lines +61 to +64
// Given a certain sorted array, this function determines whether to sort it based on a superset of a larger array, or an array simply in a different order.
public mutating func reorderItems(to elements: [Element], index: Int) {
// If the index is equal to 0, it indicates that the elements passed through as an argument is a larger array, and items is the subset of elements.
if index == -1 {
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'm not a fan of passing an index of -1 to change the behavior of this method. Other than changing how the sorting is done, index is only used to update currIndex, right?

I also don't quite understand the intended behavior of this method in the two cases. Is this right?

  • If index == -1, then we resort the internal items array, preserving the relative order of items in the elements parameter?
  • If index != -1, then we filter the internal items array based on whether each element is in elements?

If that is correct, then these two behaviors seem pretty unrelated to each other. And passing -1 to index definitely doesn't make sense to switch between the two to me.

Maybe they should be two separate methods? One to rearrange elements based on another array, and one to filter? Or at least we should come up with a better mechanism to switch between the behavior and pick a better name. You could also add a method to change currIndex if needed.

Regardless, let's add some good doc (///) comments to whatever methods we add to this repo.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

You bring up good points, yeah I'll separate the two conditions within the function to be their own independent function. Passing -1 to index was just a way to distinguish two different cases that occurred when the function occurred, but in retrospect it probably would've been better to use enums and make separate functions. I'll fix this and redo the documentation.

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.

Cool. I might prefer to provide separate methods and let the caller call the associated method

Comment on lines +82 to +89
// Maps each element in 'elements' to its 'id', creating an array of IDs.
let unsortedArrayID = elements.map { $0.id }

// Filters 'items' to include only those whose 'id' is found in 'unsortedArrayID'.
let sortedArray = items.filter { unsortedArrayID.contains($0.id) }

// Updates 'items' to be the filtered list, effectively removing any items not present in 'elements'.
items = sortedArray
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.

this doesn't seem to reorder the elements in the list, which is pretty confusing to me given the name of the method

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I'll fix this in the new separate function.

@myattj
Copy link
Copy Markdown

myattj commented Mar 19, 2024

Okay, I'll take a look. Which ticket are we making this change for? @wmwang52 @myattj
Related to this one:
https://www.notion.so/appteamcarolina/Filtering-Transactions-242f9d6ff4cb4bd4b7d914f8e127fd10?pvs=4
Here is William's PR that uses these changes in Centible: https://github.com/appteamcarolina/Centible/pull/66

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.

4 participants