Skip to content

Commit 738f1b4

Browse files
committed
Use NonEmpty for group operations
1 parent a8e463b commit 738f1b4

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

bower.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
],
1818
"dependencies": {
1919
"purescript-foldable-traversable": "^2.0.0",
20+
"purescript-nonempty": "^3.0.0",
2021
"purescript-partial": "^1.1.0",
2122
"purescript-st": "^2.0.0",
2223
"purescript-tailrec": "^2.0.0",

src/Data/Array.purs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import Control.Monad.Rec.Class (class MonadRec, Step(..), tailRecM2)
118118
import Data.Foldable (class Foldable, foldl, foldr)
119119
import Data.Foldable (foldl, foldr, foldMap, fold, intercalate, elem, notElem, find, findMap, any, all) as Exports
120120
import Data.Maybe (Maybe(..), maybe, isJust, fromJust)
121+
import Data.NonEmpty (NonEmpty, (:|))
121122
import Data.Traversable (scanl, scanr) as Exports
122123
import Data.Traversable (sequence)
123124
import Data.Tuple (Tuple(..))
@@ -505,27 +506,27 @@ span p = go []
505506
-- | ```purescript
506507
-- | group [1,1,2,2,1] == [[1,1],[2,2],[1]]
507508
-- | ```
508-
group :: forall a. Eq a => Array a -> Array (Array a)
509+
group :: forall a. Eq a => Array a -> Array (NonEmpty Array a)
509510
group xs = groupBy eq xs
510511

511512
-- | Sort and then group the elements of an array into arrays.
512513
-- |
513514
-- | ```purescript
514515
-- | group' [1,1,2,2,1] == [[1,1,1],[2,2]]
515516
-- | ```
516-
group' :: forall a. Ord a => Array a -> Array (Array a)
517+
group' :: forall a. Ord a => Array a -> Array (NonEmpty Array a)
517518
group' = group <<< sort
518519

519520
-- | Group equal, consecutive elements of an array into arrays, using the
520521
-- | specified equivalence relation to detemine equality.
521-
groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (Array a)
522+
groupBy :: forall a. (a -> a -> Boolean) -> Array a -> Array (NonEmpty Array a)
522523
groupBy op = go []
523524
where
524-
go :: Array (Array a) -> Array a -> Array (Array a)
525+
go :: Array (NonEmpty Array a) -> Array a -> Array (NonEmpty Array a)
525526
go acc xs = case uncons xs of
526527
Just o ->
527528
let sp = span (op o.head) o.tail
528-
in go ((o.head : sp.init) : acc) sp.rest
529+
in go ((o.head :| sp.init) : acc) sp.rest
529530
Nothing -> reverse acc
530531

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

test/Test/Data/Array.purs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import Control.Monad.Eff.Console (log, CONSOLE)
88
import Data.Array (range, replicate, foldM, unzip, zip, zipWithA, zipWith, intersectBy, intersect, (\\), deleteBy, delete, unionBy, union, nubBy, nub, groupBy, group', group, span, dropWhile, drop, takeWhile, take, sortBy, sort, catMaybes, mapMaybe, mapWithIndex, filterM, filter, concat, concatMap, reverse, alterAt, modifyAt, updateAt, deleteAt, insertAt, findLastIndex, findIndex, elemLastIndex, elemIndex, (!!), uncons, init, tail, last, head, insertBy, insert, snoc, (:), length, null, singleton, fromFoldable)
99
import Data.Foldable (for_, foldMapDefaultR, class Foldable, all)
1010
import Data.Maybe (Maybe(..), isNothing, fromJust)
11+
import Data.NonEmpty ((:|))
12+
import Data.NonEmpty as NE
1113
import Data.Tuple (Tuple(..))
1214
import Data.Unfoldable (replicateA)
1315

@@ -240,13 +242,13 @@ testArray = do
240242
assert $ spanResult.rest == [4, 5, 6, 7]
241243

242244
log "group should group consecutive equal elements into arrays"
243-
assert $ group [1, 2, 2, 3, 3, 3, 1] == [[1], [2, 2], [3, 3, 3], [1]]
245+
assert $ group [1, 2, 2, 3, 3, 3, 1] == [NE.singleton 1, 2 :| [2], 3:| [3, 3], NE.singleton 1]
244246

245247
log "group' should sort then group consecutive equal elements into arrays"
246-
assert $ group' [1, 2, 2, 3, 3, 3, 1] == [[1, 1], [2, 2], [3, 3, 3]]
248+
assert $ group' [1, 2, 2, 3, 3, 3, 1] == [1 :| [1], 2 :| [2], 3 :| [3, 3]]
247249

248250
log "groupBy should group consecutive equal elements into arrays based on an equivalence relation"
249-
assert $ groupBy (\x y -> odd x && odd y) [1, 1, 2, 2, 3, 3] == [[1, 1], [2], [2], [3, 3]]
251+
assert $ groupBy (\x y -> odd x && odd y) [1, 1, 2, 2, 3, 3] == [1 :| [1], NE.singleton 2, NE.singleton 2, 3 :| [3]]
250252

251253
log "nub should remove duplicate elements from the list, keeping the first occurence"
252254
assert $ nub [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4]

0 commit comments

Comments
 (0)