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

Rename group' to groupAll and add groupAllBy; add docs #200

Merged
merged 6 commits into from
Dec 29, 2020

Conversation

JordanMartinez
Copy link
Contributor

Addresses the comments that @kl0tli made here

@JordanMartinez
Copy link
Contributor Author

CI passes. Ready for review.

@hdgarrood
Copy link
Contributor

This applies to #194 too but I wonder if it's worth considering dropping the Ord constraint in groupAllBy and instead having it take an a -> a -> Ordering to use for both the sorting and the grouping.

@JordanMartinez
Copy link
Contributor Author

Perhaps a variant called groupAllBy'?

groupAllBy' :: forall a. (a -> a -> Boolean) -> (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a)
groupAllBy' p c = groupBy p <<< sortBy c

Does this pass the Fairbairn threshold?

Copy link
Member

@kl0tl kl0tl left a comment

Choose a reason for hiding this comment

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

You’re right Harry, I think we should update Data.Array.groupAllBy to forall a. (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a) (ditto for the non empty variant).

src/Data/Array/NonEmpty.purs Outdated Show resolved Hide resolved
@JordanMartinez
Copy link
Contributor Author

forall a. (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a)

Meaning this?

groupAllBy :: forall a. (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a)
groupAllBy cmp = groupBy (eq Eq $ cmp) <<< sortBy cmp

@kl0tl
Copy link
Member

kl0tl commented Dec 28, 2020

Something like the following should work:

groupAllBy comp = groupBy (\x y -> comp x y == EQ) <<< sortBy comp

-- | groupAllBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3]
-- | = [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3, 3], NonEmptyArray [4]]
-- | groupAllBy (\a b -> compare (odd a) (odd b)) [1, 3, 2, 4, 3, 3]
-- | = [NonEmptyArray [2, 4], NonEmptyArray [1, 3, 3, 3]]
-- | ```
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I adapted the example to the one above, but I don't think it's a good example. Any ideas?

Copy link
Member

Choose a reason for hiding this comment

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

What about using comparing Down?

groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3]
  = [NonEmptyArray [4], NonEmptyArray [3, 3, 3], NonEmptyArray [2], NonEmptyArray [1]]

@@ -987,15 +987,15 @@ groupBy op xs =
STA.unsafeFreeze result

-- | Group equal elements of an array into arrays, using the specified
-- | equivalence relation to determine equality.
-- | comparison relation to determine equality.
-- |
Copy link
Contributor Author

Choose a reason for hiding this comment

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

How should docs be updated to better communicate what this does?

Copy link
Member

Choose a reason for hiding this comment

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

I think we could replace “comparison relation” by “partial ordering“, as in the documentation of sortBy, here.

Copy link
Contributor

Choose a reason for hiding this comment

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

Those docs are actually incorrect - it's expecting a total ordering, not a partial one. I think they must be an oversight left over from a short period in the distant past where we said Ord represented a partial ordering rather than a total ordering. We call this a "comparison function" in Data.Array.ST, and we say "where elements are compared according to the specified ordering" in Data.List. I think either of those are fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've reverted back to using comparison function

@@ -987,15 +987,15 @@ groupBy op xs =
STA.unsafeFreeze result

-- | Group equal elements of an array into arrays, using the specified
-- | equivalence relation to determine equality.
-- | comparison relation to determine equality.
-- |
Copy link
Member

Choose a reason for hiding this comment

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

I think we could replace “comparison relation” by “partial ordering“, as in the documentation of sortBy, here.

-- | groupAllBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3]
-- | = [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3, 3], NonEmptyArray [4]]
-- | groupAllBy (\a b -> compare (odd a) (odd b)) [1, 3, 2, 4, 3, 3]
-- | = [NonEmptyArray [2, 4], NonEmptyArray [1, 3, 3, 3]]
-- | ```
Copy link
Member

Choose a reason for hiding this comment

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

What about using comparing Down?

groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3]
  = [NonEmptyArray [4], NonEmptyArray [3, 3, 3], NonEmptyArray [2], NonEmptyArray [1]]

@@ -363,10 +363,10 @@ testArray = do
assert $ A.groupBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]

log "groupAllBy should group equal elements into arrays based on an equivalence relation"
Copy link
Member

Choose a reason for hiding this comment

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

What about “based on the result of a comparison function”, as in the test for sortBy?

@JordanMartinez
Copy link
Contributor Author

I've addresses all your comments.

@JordanMartinez
Copy link
Contributor Author

This is ready for another look over.

@thomashoneyman thomashoneyman merged commit 370b8f5 into purescript:master Dec 29, 2020
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.

None yet

4 participants