Skip to content

Commit 12441f8

Browse files
committed
Rename group' to groupAll and add groupAllBy
1 parent c5ae83f commit 12441f8

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/Data/Array.purs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ module Data.Array
9494
, dropWhile
9595
, span
9696
, group
97+
, groupAll
9798
, group'
9899
, groupBy
100+
, groupAllBy
99101

100102
, nub
101103
, nubEq
@@ -140,6 +142,7 @@ import Data.Traversable (sequence, traverse)
140142
import Data.Tuple (Tuple(..), fst, snd)
141143
import Data.Unfoldable (class Unfoldable, unfoldr)
142144
import Partial.Unsafe (unsafePartial)
145+
import Prim.TypeError (class Warn, Text)
143146

144147
-- | Convert an `Array` into an `Unfoldable` structure.
145148
toUnfoldable :: forall f. Unfoldable f => Array ~> f
@@ -944,25 +947,29 @@ span p arr =
944947
-- | Group equal, consecutive elements of an array into arrays.
945948
-- |
946949
-- | ```purescript
947-
-- | group [1,1,2,2,1] == [NonEmpty 1 [1], NonEmpty 2 [2], NonEmpty 1 []]
950+
-- | group [1, 1, 2, 2, 1] == [NonEmptyArray [1, 1], NonEmptyArray [2, 2], NonEmptyArray [1]]
948951
-- | ```
949952
group :: forall a. Eq a => Array a -> Array (NonEmptyArray a)
950953
group xs = groupBy eq xs
951954

952-
-- | Sort and then group the elements of an array into arrays.
955+
-- | Group equal elements of an array into arrays.
953956
-- |
954957
-- | ```purescript
955-
-- | group' [1,1,2,2,1] == [NonEmpty 1 [1,1],NonEmpty 2 [2]]
958+
-- | groupAll [1, 1, 2, 2, 1] == [NonEmptyArray [1, 1, 1], NonEmptyArray [2, 2]]
956959
-- | ```
957-
group' :: forall a. Ord a => Array a -> Array (NonEmptyArray a)
958-
group' = group <<< sort
960+
groupAll :: forall a. Ord a => Array a -> Array (NonEmptyArray a)
961+
groupAll = groupAllBy eq
962+
963+
-- | Deprecated previous name of `groupAll`.
964+
group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => Array a -> Array (NonEmptyArray a)
965+
group' = groupAll
959966

960967
-- | Group equal, consecutive elements of an array into arrays, using the
961-
-- | specified equivalence relation to detemine equality.
968+
-- | specified equivalence relation to determine equality.
962969
-- |
963970
-- | ```purescript
964971
-- | groupBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3]
965-
-- | = [NonEmpty 1 [3], NonEmpty 2 [] , NonEmpty 4 [], NonEmpty 3 [3]]
972+
-- | = [NonEmptyArray [1, 3], NonEmptyArray [2], NonEmptyArray [4], NonEmptyArray [3, 3]]
966973
-- | ```
967974
-- |
968975
groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a)
@@ -978,6 +985,17 @@ groupBy op xs =
978985
STA.push (NonEmptyArray grp) result
979986
STA.unsafeFreeze result
980987

988+
-- | Group equal elements of an array into arrays, using the specified
989+
-- | equivalence relation to determine equality.
990+
-- |
991+
-- | ```purescript
992+
-- | groupAllBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3]
993+
-- | = [NonEmptyArray [1], NonEmptyArray [2], NonEmptyArray [3, 3, 3], NonEmptyArray [4]]
994+
-- | ```
995+
-- |
996+
groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a)
997+
groupAllBy p = groupBy p <<< sort
998+
981999
-- | Remove the duplicates from an array, creating a new array.
9821000
-- |
9831001
-- | ```purescript

test/Test/Data/Array.purs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,17 +349,23 @@ testArray = do
349349
testBigSpan 100000
350350

351351
log "group should group consecutive equal elements into arrays"
352-
assert $ A.group [1, 2, 2, 3, 3, 3, 1] == [NEA.singleton 1, nea [2, 2], nea [3, 3, 3], NEA.singleton 1]
352+
assert $ A.group [1, 2, 2, 3, 3, 3, 1] == [nea [1], nea [2, 2], nea [3, 3, 3], nea [1]]
353353

354-
log "group' should sort then group consecutive equal elements into arrays"
355-
assert $ A.group' [1, 2, 2, 3, 3, 3, 1] == [nea [1, 1], nea [2, 2], nea [3, 3, 3]]
354+
log "groupAll should group equal elements into arrays"
355+
assert $ A.groupAll [1, 2, 2, 3, 3, 3, 1] == [nea [1, 1], nea [2, 2], nea [3, 3, 3]]
356356

357357
log "groupBy should group consecutive equal elements into arrays based on an equivalence relation"
358-
assert $ A.groupBy (\x y -> odd x && odd y) [1, 1, 2, 2, 3, 3] == [nea [1, 1], NEA.singleton 2, NEA.singleton 2, nea [3, 3]]
358+
assert $ A.groupBy (\x y -> odd x && odd y) [1, 1, 2, 2, 3, 3] == [nea [1, 1], nea [2], nea [2], nea [3, 3]]
359359

360360
log "groupBy should be stable"
361361
assert $ A.groupBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]
362362

363+
log "groupAllBy should group equal elements into arrays based on an equivalence relation"
364+
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]]
365+
366+
log "groupAllBy should be stable"
367+
assert $ A.groupAllBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]
368+
363369
log "nub should remove duplicate elements from the list, keeping the first occurence"
364370
assert $ A.nub [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4]
365371

0 commit comments

Comments
 (0)