Skip to content

Revise groupAllBy to just use an Ordering function #191

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 10 commits into from
Mar 18, 2022
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Breaking changes:
- Update project and deps to PureScript v0.15.0 (#203 by @JordanMartinez)
- Drop deprecated `MonadZero` instance (#205 by @JordanMartinez)
- Drop deprecated `group'` and `mapWithIndex` (#206 by @JordanMartinez)
- Change `groupAllBy` to use a comparison function (#191)

New features:

Expand Down Expand Up @@ -43,7 +44,7 @@ Breaking changes:

New features:
- Added `nubEq`/`nubByEq` (#179)
- Added `groupAllBy` (#182, #191)
- Added `groupAllBy` (#182)
Comment on lines -46 to +47
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 prematurely added this PR to the changelog in #195. I was hoping this PR would have been included in the batch of breaking changes, so the release history would show groupAllBy as being added correctly the first time.

In hindsight, it would have been better to add the #191 tag here in this PR. That could have achieved the same result (assuming it got merged before the release), and would have avoided recent confusion where rebasing to master eliminated all changelog edits for this PR.

- Added `Eq1` and `Ord1` instances to `NonEmptyList` and `LazyNonEmptyList` (#188)

Bugfixes:
Expand Down
15 changes: 7 additions & 8 deletions src/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -611,17 +611,16 @@ groupBy _ Nil = Nil
groupBy eq (x : xs) = case span (eq x) xs of
{ init: ys, rest: zs } -> NEL.NonEmptyList (x :| ys) : groupBy eq zs

-- | Group equal elements of a list into lists, using the specified
-- | equivalence relation to determine equality.
-- |
-- | For example,
-- | Sort, then group equal elements of a list into lists, using the provided comparison function.
-- |
-- | ```purescript
-- | groupAllBy (\a b -> odd a && odd b) (1 : 3 : 2 : 4 : 3 : 3 : Nil) ==
-- | (NonEmptyList (NonEmpty 1 Nil)) : (NonEmptyList (NonEmpty 2 Nil)) : (NonEmptyList (NonEmpty 3 (3 : 3 : Nil))) : (NonEmptyList (NonEmpty 4 Nil)) : Nil
-- | groupAllBy (compare `on` (_ `div` 10)) (32 : 31 : 21 : 22 : 11 : 33 : Nil) ==
-- | NonEmptyList (11 :| Nil) : NonEmptyList (21 :| 22 : Nil) : NonEmptyList (32 :| 31 : 33) : Nil
-- | ```
groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> List a -> List (NEL.NonEmptyList a)
groupAllBy p = groupBy p <<< sort
-- |
-- | Running time: `O(n log n)`
groupAllBy :: forall a. (a -> a -> Ordering) -> List a -> List (NEL.NonEmptyList a)
groupAllBy p = groupBy (\x y -> p x y == EQ) <<< sortBy p

-- | Returns a lists of elements which do and do not satisfy a predicate.
-- |
Expand Down
2 changes: 1 addition & 1 deletion src/Data/List/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ groupAll = wrappedOperation "groupAll" L.groupAll
groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList (NonEmptyList a)
groupBy = wrappedOperation "groupBy" <<< L.groupBy

groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList (NonEmptyList a)
groupAllBy :: forall a. (a -> a -> Ordering) -> NonEmptyList a -> NonEmptyList (NonEmptyList a)
groupAllBy = wrappedOperation "groupAllBy" <<< L.groupAllBy

partition :: forall a. (a -> Boolean) -> NonEmptyList a -> { yes :: L.List a, no :: L.List a }
Expand Down
12 changes: 8 additions & 4 deletions test/Test/Data/List.purs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Test.Data.List (testList) where
import Prelude

import Data.Array as Array
import Data.Foldable (foldMap, foldl)
import Data.Foldable (class Foldable, foldMap, foldl)
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
import Data.Function (on)
import Data.List (List(..), Pattern(..), alterAt, catMaybes, concat, concatMap, delete, deleteAt, deleteBy, drop, dropEnd, dropWhile, elemIndex, elemLastIndex, filter, filterM, findIndex, findLastIndex, foldM, fromFoldable, group, groupAll, groupAllBy, groupBy, head, init, insert, insertAt, insertBy, intersect, intersectBy, last, length, mapMaybe, modifyAt, nub, nubBy, nubByEq, nubEq, null, partition, range, reverse, singleton, snoc, sort, sortBy, span, stripPrefix, tail, take, takeEnd, takeWhile, transpose, uncons, union, unionBy, unsnoc, unzip, updateAt, zip, zipWith, zipWithA, (!!), (..), (:), (\\))
Expand All @@ -23,7 +23,11 @@ import Test.Assert (assert)

testList :: Effect Unit
testList = do
let l = fromFoldable
let
l = fromFoldable

nel :: forall f a. Foldable f => a -> f a -> NEL.NonEmptyList a
nel x xs = NEL.NonEmptyList $ x :| fromFoldable xs

log "strip prefix"
assert $ stripPrefix (Pattern (1:Nil)) (1:2:Nil) == Just (2:Nil)
Expand Down Expand Up @@ -272,8 +276,8 @@ testList = do
log "groupBy should group consecutive equal elements into lists based on an equivalence relation"
assert $ groupBy (\x y -> odd x && odd y) (l [1, 1, 2, 2, 3, 3]) == l [NEL.NonEmptyList (1 :| l [1]), NEL.singleton 2, NEL.singleton 2, NEL.NonEmptyList (3 :| l [3])]

log "groupAllBy should group equal elements into lists based on an equivalence relation"
assert $ groupAllBy (\x y -> odd x && odd y) (l [1, 3, 2, 4, 3, 3]) == l [NEL.singleton 1, NEL.singleton 2, NEL.NonEmptyList (3 :| l [3, 3]), NEL.singleton 4]
log "groupAllBy should sort then group equal elements into lists based on a comparison function"
assert $ groupAllBy (compare `on` (_ `div` 10)) (l [32, 31, 21, 22, 11, 33]) == l [nel 11 [], nel 21 [22], nel 32 [31, 33]]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

A test case like this is necessary to demonstrate the degree of ordering preserved.
I wanted this to be consistent with the docs and then I wanted to make sure this was easily relatable to the other group* tests and examples.


log "partition should separate a list into a tuple of lists that do and do not satisfy a predicate"
let partitioned = partition (_ > 2) (l [1, 5, 3, 2, 4])
Expand Down
8 changes: 4 additions & 4 deletions test/Test/Data/List/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import Test.Assert (assert)
testNonEmptyList :: Effect Unit
testNonEmptyList = do
let
nel :: f a. Foldable f => a -> f a -> NEL.NonEmptyList a
nel :: forall f a. Foldable f => a -> f a -> NEL.NonEmptyList a
nel x xs = NEL.NonEmptyList $ x :| L.fromFoldable xs
l :: f a. Foldable f => f a -> L.List a
l :: forall f a. Foldable f => f a -> L.List a
l = L.fromFoldable

log "singleton should construct a non-empty list with a single value"
Expand Down Expand Up @@ -173,8 +173,8 @@ testNonEmptyList = do
log "groupBy should group consecutive equal elements into lists based on an equivalence relation"
assert $ NEL.groupBy (\x y -> odd x && odd y) (nel 1 [1, 2, 2, 3, 3]) == nel (nel 1 [1]) [nel 2 [], nel 2 [], nel 3 [3]]

log "groupAllBy should group equal elements into lists based on an equivalence relation"
assert $ NEL.groupAllBy (\x y -> odd x && odd y) (nel 1 [3, 2, 4, 3, 3]) == nel (nel 1 []) [nel 2 [], nel 3 [3, 3], nel 4 []]
log "groupAllBy should sort then group equal elements into lists based on a comparison function"
assert $ NEL.groupAllBy (compare `on` (_ `div` 10)) (nel 32 [31, 21, 22, 11, 33]) == nel (nel 11 []) [nel 21 [22], nel 32 [31, 33]]

log "partition should separate a list into a tuple of lists that do and do not satisfy a predicate"
let partitioned = NEL.partition (_ > 2) (nel 1 [5, 3, 2, 4])
Expand Down