Skip to content

Commit d00254b

Browse files
authored
Rename group' to groupAll and add groupAllBy (#182)
1 parent 6aa1eac commit d00254b

File tree

4 files changed

+59
-12
lines changed

4 files changed

+59
-12
lines changed

src/Data/List.purs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,10 @@ module Data.List
6767
, dropWhile
6868
, span
6969
, group
70+
, groupAll
7071
, group'
7172
, groupBy
73+
, groupAllBy
7274
, partition
7375

7476
, nub
@@ -115,6 +117,8 @@ import Data.Unfoldable (class Unfoldable, unfoldr)
115117
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, find, findMap, any, all) as Exports
116118
import Data.Traversable (scanl, scanr) as Exports
117119

120+
import Prim.TypeError (class Warn, Text)
121+
118122
-- | Convert a list into any unfoldable structure.
119123
-- |
120124
-- | Running time: `O(n)`
@@ -583,30 +587,57 @@ span _ xs = { init: Nil, rest: xs }
583587
-- | For example,
584588
-- |
585589
-- | ```purescript
586-
-- | group (1 : 1 : 2 : 2 : 1 : Nil) == (1 : 1 : Nil) : (2 : 2 : Nil) : (1 : Nil) : Nil
590+
-- | group (1 : 1 : 2 : 2 : 1 : Nil) ==
591+
-- | (NonEmptyList (NonEmpty 1 (1 : Nil))) : (NonEmptyList (NonEmpty 2 (2 : Nil))) : (NonEmptyList (NonEmpty 1 Nil)) : Nil
587592
-- | ```
588593
-- |
589594
-- | Running time: `O(n)`
590595
group :: forall a. Eq a => List a -> List (NEL.NonEmptyList a)
591596
group = groupBy (==)
592597

593-
-- | Sort and then group the elements of a list into lists.
598+
-- | Group equal elements of a list into lists.
599+
-- |
600+
-- | For example,
594601
-- |
595602
-- | ```purescript
596-
-- | group' [1,1,2,2,1] == [[1,1,1],[2,2]]
603+
-- | groupAll (1 : 1 : 2 : 2 : 1 : Nil) ==
604+
-- | (NonEmptyList (NonEmpty 1 (1 : 1 : Nil))) : (NonEmptyList (NonEmpty 2 (2 : Nil))) : Nil
597605
-- | ```
598-
group' :: forall a. Ord a => List a -> List (NEL.NonEmptyList a)
599-
group' = group <<< sort
606+
groupAll :: forall a. Ord a => List a -> List (NEL.NonEmptyList a)
607+
groupAll = group <<< sort
608+
609+
-- | Deprecated previous name of `groupAll`.
610+
group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => List a -> List (NEL.NonEmptyList a)
611+
group' = groupAll
600612

601613
-- | Group equal, consecutive elements of a list into lists, using the specified
602614
-- | equivalence relation to determine equality.
603615
-- |
616+
-- | For example,
617+
-- |
618+
-- | ```purescript
619+
-- | groupBy (\a b -> odd a && odd b) (1 : 3 : 2 : 4 : 3 : 3 : Nil) ==
620+
-- | (NonEmptyList (NonEmpty 1 (3 : Nil))) : (NonEmptyList (NonEmpty 2 Nil)) : (NonEmptyList (NonEmpty 4 Nil)) : (NonEmptyList (NonEmpty 3 (3 : Nil))) : Nil
621+
-- | ```
622+
-- |
604623
-- | Running time: `O(n)`
605624
groupBy :: forall a. (a -> a -> Boolean) -> List a -> List (NEL.NonEmptyList a)
606625
groupBy _ Nil = Nil
607626
groupBy eq (x : xs) = case span (eq x) xs of
608627
{ init: ys, rest: zs } -> NEL.NonEmptyList (x :| ys) : groupBy eq zs
609628

629+
-- | Group equal elements of a list into lists, using the specified
630+
-- | equivalence relation to determine equality.
631+
-- |
632+
-- | For example,
633+
-- |
634+
-- | ```purescript
635+
-- | groupAllBy (\a b -> odd a && odd b) (1 : 3 : 2 : 4 : 3 : 3 : Nil) ==
636+
-- | (NonEmptyList (NonEmpty 1 Nil)) : (NonEmptyList (NonEmpty 2 Nil)) : (NonEmptyList (NonEmpty 3 (3 : 3 : Nil))) : (NonEmptyList (NonEmpty 4 Nil)) : Nil
637+
-- | ```
638+
groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> List a -> List (NEL.NonEmptyList a)
639+
groupAllBy p = groupBy p <<< sort
640+
610641
-- | Returns a lists of elements which do and do not satisfy a predicate.
611642
-- |
612643
-- | Running time: `O(n)`

src/Data/List/NonEmpty.purs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ module Data.List.NonEmpty
4141
, dropWhile
4242
, span
4343
, group
44+
, groupAll
4445
, group'
4546
, groupBy
47+
, groupAllBy
4648
, partition
4749
, nub
4850
, nubBy
@@ -78,6 +80,8 @@ import Data.Semigroup.Foldable (fold1, foldMap1, for1_, sequence1_, traverse1_)
7880
import Data.Semigroup.Traversable (sequence1, traverse1, traverse1Default) as Exports
7981
import Data.Traversable (scanl, scanr) as Exports
8082

83+
import Prim.TypeError (class Warn, Text)
84+
8185
-- | Internal function: any operation on a list that is guaranteed not to delete
8286
-- | all elements also applies to a NEL, this function is a helper for defining
8387
-- | those cases.
@@ -259,12 +263,18 @@ span = lift <<< L.span
259263
group :: forall a. Eq a => NonEmptyList a -> NonEmptyList (NonEmptyList a)
260264
group = wrappedOperation "group" L.group
261265

262-
group' :: forall a. Ord a => NonEmptyList a -> NonEmptyList (NonEmptyList a)
263-
group' = wrappedOperation "group'" L.group'
266+
groupAll :: forall a. Ord a => NonEmptyList a -> NonEmptyList (NonEmptyList a)
267+
groupAll = wrappedOperation "groupAll" L.groupAll
268+
269+
group' :: forall a. Warn (Text "'group\'' is deprecated, use groupAll instead") => Ord a => NonEmptyList a -> NonEmptyList (NonEmptyList a)
270+
group' = groupAll
264271

265272
groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList (NonEmptyList a)
266273
groupBy = wrappedOperation "groupBy" <<< L.groupBy
267274

275+
groupAllBy :: forall a. Ord a => (a -> a -> Boolean) -> NonEmptyList a -> NonEmptyList (NonEmptyList a)
276+
groupAllBy = wrappedOperation "groupAllBy" <<< L.groupAllBy
277+
268278
partition :: forall a. (a -> Boolean) -> NonEmptyList a -> { yes :: L.List a, no :: L.List a }
269279
partition = lift <<< L.partition
270280

test/Test/Data/List.purs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Prelude
44

55
import Data.Foldable (foldMap, foldl)
66
import Data.FoldableWithIndex (foldMapWithIndex, foldlWithIndex, foldrWithIndex)
7-
import Data.List (List(..), (..), stripPrefix, Pattern(..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, partition, span, dropWhile, drop, dropEnd, takeWhile, take, takeEnd, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
7+
import Data.List (List(..), (..), stripPrefix, Pattern(..), length, range, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, group, groupAll, groupBy, groupAllBy, partition, span, dropWhile, drop, dropEnd, takeWhile, take, takeEnd, sortBy, sort, catMaybes, mapMaybe, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, unsnoc, init, tail, last, head, insertBy, insert, snoc, null, singleton, fromFoldable, transpose, mapWithIndex, (:))
88
import Data.List.NonEmpty as NEL
99
import Data.Maybe (Maybe(..), isNothing, fromJust)
1010
import Data.Monoid.Additive (Additive(..))
@@ -267,12 +267,15 @@ testList = do
267267
log "group should group consecutive equal elements into lists"
268268
assert $ group (l [1, 2, 2, 3, 3, 3, 1]) == l [NEL.singleton 1, NEL.NonEmptyList (2 :| l [2]), NEL.NonEmptyList (3 :| l [3, 3]), NEL.singleton 1]
269269

270-
log "group' should sort then group consecutive equal elements into lists"
271-
assert $ group' (l [1, 2, 2, 3, 3, 3, 1]) == l [NEL.NonEmptyList (1 :| l [1]), NEL.NonEmptyList (2 :| l [2]), NEL.NonEmptyList (3 :| l [3, 3])]
270+
log "groupAll should group equal elements into lists"
271+
assert $ groupAll (l [1, 2, 2, 3, 3, 3, 1]) == l [NEL.NonEmptyList (1 :| l [1]), NEL.NonEmptyList (2 :| l [2]), NEL.NonEmptyList (3 :| l [3, 3])]
272272

273273
log "groupBy should group consecutive equal elements into lists based on an equivalence relation"
274274
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])]
275275

276+
log "groupAllBy should group equal elements into lists based on an equivalence relation"
277+
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]
278+
276279
log "partition should separate a list into a tuple of lists that do and do not satisfy a predicate"
277280
let partitioned = partition (_ > 2) (l [1, 5, 3, 2, 4])
278281
assert $ partitioned.yes == l [5, 3, 4]

test/Test/Data/List/NonEmpty.purs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,15 @@ testNonEmptyList = do
168168
log "group should group consecutive equal elements into lists"
169169
assert $ NEL.group (nel 1 [2, 2, 3, 3, 3, 1]) == nel (nel 1 []) [nel 2 [2], nel 3 [3, 3], nel 1 []]
170170

171-
log "group' should sort then group consecutive equal elements into lists"
172-
assert $ NEL.group' (nel 1 [2, 2, 3, 3, 3, 1]) == nel (nel 1 [1]) [nel 2 [2], nel 3 [3, 3]]
171+
log "groupAll should group equal elements into lists"
172+
assert $ NEL.groupAll (nel 1 [2, 2, 3, 3, 3, 1]) == nel (nel 1 [1]) [nel 2 [2], nel 3 [3, 3]]
173173

174174
log "groupBy should group consecutive equal elements into lists based on an equivalence relation"
175175
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]]
176176

177+
log "groupAllBy should group equal elements into lists based on an equivalence relation"
178+
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 []]
179+
177180
log "partition should separate a list into a tuple of lists that do and do not satisfy a predicate"
178181
let partitioned = NEL.partition (_ > 2) (nel 1 [5, 3, 2, 4])
179182
assert $ partitioned.yes == l [5, 3, 4]

0 commit comments

Comments
 (0)