Skip to content

Commit e13c01f

Browse files
authored
Merge pull request #194 from kl0tl/groupAll
Rename group' to groupAll and add groupAllBy
2 parents 120e283 + 12441f8 commit e13c01f

File tree

2 files changed

+35
-11
lines changed

2 files changed

+35
-11
lines changed

src/Data/Array.purs

+25-7
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
@@ -942,25 +945,29 @@ span p arr =
942945
-- | Group equal, consecutive elements of an array into arrays.
943946
-- |
944947
-- | ```purescript
945-
-- | group [1,1,2,2,1] == [NonEmpty 1 [1], NonEmpty 2 [2], NonEmpty 1 []]
948+
-- | group [1, 1, 2, 2, 1] == [NonEmptyArray [1, 1], NonEmptyArray [2, 2], NonEmptyArray [1]]
946949
-- | ```
947950
group :: forall a. Eq a => Array a -> Array (NonEmptyArray a)
948951
group xs = groupBy eq xs
949952

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

958965
-- | Group equal, consecutive elements of an array into arrays, using the
959-
-- | specified equivalence relation to detemine equality.
966+
-- | specified equivalence relation to determine equality.
960967
-- |
961968
-- | ```purescript
962969
-- | groupBy (\a b -> odd a && odd b) [1, 3, 2, 4, 3, 3]
963-
-- | = [NonEmpty 1 [3], NonEmpty 2 [] , NonEmpty 4 [], NonEmpty 3 [3]]
970+
-- | = [NonEmptyArray [1, 3], NonEmptyArray [2], NonEmptyArray [4], NonEmptyArray [3, 3]]
964971
-- | ```
965972
-- |
966973
groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (NonEmptyArray a)
@@ -976,6 +983,17 @@ groupBy op xs =
976983
STA.push (NonEmptyArray grp) result
977984
STA.unsafeFreeze result
978985

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

test/Test/Data/Array.purs

+10-4
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,23 @@ testArray = do
351351
testBigSpan 100000
352352

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

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

359359
log "groupBy should group consecutive equal elements into arrays based on an equivalence relation"
360-
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]]
360+
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]]
361361

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

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]]
367+
368+
log "groupAllBy should be stable"
369+
assert $ A.groupAllBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]
370+
365371
log "nub should remove duplicate elements from the list, keeping the first occurence"
366372
assert $ A.nub [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4]
367373

0 commit comments

Comments
 (0)