Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ module Data.Array.NonEmpty
, dropEnd
, dropWhile
, span
, group
, group'
, groupBy

, nub
, nubBy
Expand Down Expand Up @@ -339,6 +342,15 @@ span
-> { init :: Array a, rest :: Array a }
span f = adaptAny $ A.span f

group :: forall a. Eq a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
group = unsafeAdapt $ A.group

group' :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
group' = unsafeAdapt $ A.group'

groupBy :: forall a. (a -> a -> Boolean) -> NonEmptyArray a -> NonEmptyArray (NonEmptyArray a)
groupBy op = unsafeAdapt $ A.groupBy op

nub :: forall a. Ord a => NonEmptyArray a -> NonEmptyArray a
nub = unsafeAdapt A.nub

Expand Down
12 changes: 12 additions & 0 deletions test/Test/Data/Array/NonEmpty.purs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,18 @@ testNonEmptyArray = do
let oneToSeven = fromArray [1, 2, 3, 4, 5, 6, 7]
testSpan { p: (_ < 4), input: oneToSeven, init_: [1, 2, 3], rest_: [4, 5, 6, 7] }

log "group should group consecutive equal elements into arrays"
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]

log "group' should sort then group consecutive equal elements into arrays"
assert $ NEA.group' (fromArray [1, 2, 2, 3, 3, 3, 1]) == fromArray [fromArray [1, 1], fromArray [2, 2], fromArray [3, 3, 3]]

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

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

log "nub should remove duplicate elements from the list, keeping the first occurence"
assert $ NEA.nub (fromArray [1, 2, 2, 3, 4, 1]) == fromArray [1, 2, 3, 4]

Expand Down