Skip to content

Commit 370b8f5

Browse files
Rename group' to groupAll and add groupAllBy; add docs (#200)
* Rename group' to groupAll and add groupAllBy; add docs * Replace implementation's deprecated `group'` with `groupAll` * Quote `groupAll` in deprecation warning * Update `groupAllBy` to use `compare` for both sorting and grouping * Address feedback: update docs and use `comparing Down` as example * Revert back to 'comparison function' phrasing for docs
1 parent bdd5bd7 commit 370b8f5

File tree

4 files changed

+63
-15
lines changed

4 files changed

+63
-15
lines changed

src/Data/Array.purs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -959,10 +959,10 @@ group xs = groupBy eq xs
959959
-- | groupAll [1, 1, 2, 2, 1] == [NonEmptyArray [1, 1, 1], NonEmptyArray [2, 2]]
960960
-- | ```
961961
groupAll :: forall a. Ord a => Array a -> Array (NonEmptyArray a)
962-
groupAll = groupAllBy eq
962+
groupAll = groupAllBy compare
963963

964964
-- | Deprecated previous name of `groupAll`.
965-
group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => Array a -> Array (NonEmptyArray a)
965+
group' :: forall a. Warn (Text "'group\'' is deprecated, use 'groupAll' instead") => Ord a => Array a -> Array (NonEmptyArray a)
966966
group' = groupAll
967967

968968
-- | Group equal, consecutive elements of an array into arrays, using the
@@ -987,15 +987,15 @@ groupBy op xs =
987987
STA.unsafeFreeze result
988988

989989
-- | Group equal elements of an array into arrays, using the specified
990-
-- | equivalence relation to determine equality.
990+
-- | comparison function to determine equality.
991991
-- |
992992
-- | ```purescript
993-
-- | groupAllBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3]
994-
-- | = [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3, 3], NonEmptyArray [4]]
993+
-- | groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3]
994+
-- | = [NonEmptyArray [4], NonEmptyArray [3, 3, 3], NonEmptyArray [2], NonEmptyArray [1]]
995995
-- | ```
996996
-- |
997-
groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a)
998-
groupAllBy p = groupBy p <<< sort
997+
groupAllBy :: forall a. (a -> a -> Ordering) -> Array a -> Array (NonEmptyArray a)
998+
groupAllBy cmp = groupBy (\x y -> cmp x y == EQ) <<< sortBy cmp
999999

10001000
-- | Remove the duplicates from an array, creating a new array.
10011001
-- |

src/Data/Array/NonEmpty.purs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ module Data.Array.NonEmpty
6666
, dropWhile
6767
, span
6868
, group
69+
, groupAll
6970
, group'
7071
, groupBy
72+
, groupAllBy
7173

7274
, nub
7375
, nubBy
@@ -118,6 +120,7 @@ import Data.Tuple (Tuple(..))
118120
import Data.Unfoldable (class Unfoldable)
119121
import Data.Unfoldable1 (class Unfoldable1, unfoldr1)
120122
import Partial.Unsafe (unsafePartial)
123+
import Prim.TypeError (class Warn, Text)
121124
import Unsafe.Coerce (unsafeCoerce)
122125

123126
-- | Internal - adapt an Array transform to NonEmptyArray
@@ -350,15 +353,49 @@ span
350353
-> { init :: Array a, rest :: Array a }
351354
span f = adaptAny $ A.span f
352355

356+
-- | Group equal, consecutive elements of an array into arrays.
357+
-- |
358+
-- | ```purescript
359+
-- | group (NonEmptyArray [1, 1, 2, 2, 1]) ==
360+
-- | NonEmptyArray [NonEmptyArray [1, 1], NonEmptyArray [2, 2], NonEmptyArray [1]]
361+
-- | ```
353362
group :: forall a. Eq a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
354363
group = unsafeAdapt $ A.group
355364

356-
group' :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
357-
group' = unsafeAdapt $ A.group'
358-
365+
-- | Group equal elements of an array into arrays.
366+
-- |
367+
-- | ```purescript
368+
-- | groupAll (NonEmptyArray [1, 1, 2, 2, 1]) ==
369+
-- | NonEmptyArray [NonEmptyArray [1, 1, 1], NonEmptyArray [2, 2]]
370+
-- | `
371+
groupAll :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
372+
groupAll = groupAllBy compare
373+
374+
-- | Deprecated previous name of `groupAll`.
375+
group' :: forall a. Warn (Text "'group\'' is deprecated, use 'groupAll' instead") => Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
376+
group' = unsafeAdapt $ A.groupAll
377+
378+
-- | Group equal, consecutive elements of an array into arrays, using the
379+
-- | specified equivalence relation to determine equality.
380+
-- |
381+
-- | ```purescript
382+
-- | groupBy (\a b -> odd a && odd b) (NonEmptyArray [1, 3, 2, 4, 3, 3])
383+
-- | = NonEmptyArray [NonEmptyArray [1, 3], NonEmptyArray [2], NonEmptyArray [4], NonEmptyArray [3, 3]]
384+
-- | ```
385+
-- |
359386
groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
360387
groupBy op = unsafeAdapt $ A.groupBy op
361388

389+
-- | Group equal elements of an array into arrays, using the specified
390+
-- | comparison function to determine equality.
391+
-- |
392+
-- | ```purescript
393+
-- | groupAllBy (comparing Down) (NonEmptyArray [1, 3, 2, 4, 3, 3])
394+
-- | = NonEmptyArray [NonEmptyArray [4], NonEmptyArray [3, 3, 3], NonEmptyArray [2], NonEmptyArray [1]]
395+
-- | ```
396+
groupAllBy :: forall a. (a -> a -> Ordering) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
397+
groupAllBy op = unsafeAdapt $ A.groupAllBy op
398+
362399
nub :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray a
363400
nub = unsafeAdapt A.nub
364401

test/Test/Data/Array.purs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Data.Const (Const(..))
99
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all, traverse_)
1010
import Data.Traversable (scanl, scanr)
1111
import Data.Maybe (Maybe(..), isNothing, fromJust)
12+
import Data.Ord.Down (Down(..))
1213
import Data.Tuple (Tuple(..))
1314
import Data.Unfoldable (replicateA)
1415
import Effect (Effect)
@@ -362,11 +363,11 @@ testArray = do
362363
log "groupBy should be stable"
363364
assert $ A.groupBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]
364365

365-
log "groupAllBy should group equal elements into arrays based on an equivalence relation"
366-
assert $ A.groupAllBy (\x y -> odd x && odd y) [1, 3, 2, 4, 3, 3] == [nea [1], nea [2], nea [3, 3, 3], nea [4]]
366+
log "groupAllBy should group equal elements into arrays based on the result of a comparison function"
367+
assert $ A.groupAllBy (comparing Down) [1, 3, 2, 4, 3, 3] == [nea [4], nea [3, 3, 3], nea [2], nea [1]]
367368

368369
log "groupAllBy should be stable"
369-
assert $ A.groupAllBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]
370+
assert $ A.groupAllBy (\_ _ -> EQ) [1, 2, 3] == [nea [1, 2, 3]]
370371

371372
log "nub should remove duplicate elements from the list, keeping the first occurence"
372373
assert $ A.nub [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4]

test/Test/Data/Array/NonEmpty.purs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import Data.FunctorWithIndex (mapWithIndex)
1010
import Data.Maybe (Maybe(..), fromJust)
1111
import Data.Monoid.Additive (Additive(..))
1212
import Data.NonEmpty ((:|))
13+
import Data.Ord.Down (Down(..))
1314
import Data.Semigroup.Foldable (foldMap1, foldr1, foldl1)
1415
import Data.Semigroup.Traversable (traverse1)
1516
import Data.Tuple (Tuple(..))
@@ -25,6 +26,9 @@ testNonEmptyArray = do
2526
let fromArray :: forall a. Array a -> NEA.NonEmptyArray a
2627
fromArray = unsafePartial fromJust <<< NEA.fromArray
2728

29+
nea :: forall a. Array a -> NEA.NonEmptyArray a
30+
nea = fromArray
31+
2832
log "singleton should construct an array with a single value"
2933
assert $ NEA.toArray (NEA.singleton 1) == [1]
3034
assert $ NEA.toArray (NEA.singleton "foo") == ["foo"]
@@ -247,15 +251,21 @@ testNonEmptyArray = do
247251
log "group should group consecutive equal elements into arrays"
248252
assert $ NEA.group (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [NEA.singleton 1, fromArray [2, 2], fromArray [3, 3, 3], NEA.singleton 1]
249253

250-
log "group' should sort then group consecutive equal elements into arrays"
251-
assert $ NEA.group' (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [fromArray [1, 1], fromArray [2, 2], fromArray [3, 3, 3]]
254+
log "groupAll should group equal elements into arrays"
255+
assert $ NEA.groupAll (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [fromArray [1, 1], fromArray [2, 2], fromArray [3, 3, 3]]
252256

253257
log "groupBy should group consecutive equal elements into arrays based on an equivalence relation"
254258
assert $ NEA.groupBy (\x y -> odd x && odd y) (fromArray [1, 1, 2, 2, 3, 3]) == fromArray [fromArray [1, 1], NEA.singleton 2, NEA.singleton 2, fromArray [3, 3]]
255259

256260
log "groupBy should be stable"
257261
assert $ NEA.groupBy (\_ _ -> true) (fromArray [1, 2, 3]) == fromArray [fromArray [1, 2, 3]]
258262

263+
log "groupAllBy should group equal elements into arrays based on the result of a comparison function"
264+
assert $ NEA.groupAllBy (comparing Down) (fromArray [1, 3, 2, 4, 3, 3]) == fromArray [nea [4], nea [3, 3, 3], nea [2], nea [1]]
265+
266+
log "groupAllBy should be stable"
267+
assert $ NEA.groupAllBy (\_ _ -> EQ) (fromArray [1, 2, 3]) == fromArray [nea [1, 2, 3]]
268+
259269
log "nub should remove duplicate elements from the list, keeping the first occurence"
260270
assert $ NEA.nub (fromArray [1, 2, 2, 3, 4, 1]) == fromArray [1, 2, 3, 4]
261271

0 commit comments

Comments
 (0)