Skip to content

Commit

Permalink
Make groupBy stable
Browse files Browse the repository at this point in the history
`groupBy tt ([x] <> xs) = [NonEmptyArray x xs]`
  • Loading branch information
LiamGoodacre committed Sep 25, 2018
1 parent d81e971 commit a4635c8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -866,8 +866,8 @@ groupBy op xs =
iter <- STAI.iterator (xs !! _)
STAI.iterate iter \x -> void do
sub <- STA.empty
STAI.pushWhile (op x) iter sub
_ <- STA.push x sub
STAI.pushWhile (op x) iter sub
grp <- STA.unsafeFreeze sub
STA.push ((unsafeCoerce :: Array ~> NonEmptyArray) grp) result
STA.unsafeFreeze result
Expand Down
3 changes: 3 additions & 0 deletions test/Test/Data/Array.purs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,9 @@ testArray = do
log "groupBy should group consecutive equal elements into arrays based on an equivalence relation"
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]]

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

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

Expand Down

0 comments on commit a4635c8

Please sign in to comment.