Skip to content

Commit a4635c8

Browse files
committed
Make groupBy stable
`groupBy tt ([x] <> xs) = [NonEmptyArray x xs]`
1 parent d81e971 commit a4635c8

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/Data/Array.purs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -866,8 +866,8 @@ groupBy op xs =
866866
iter <- STAI.iterator (xs !! _)
867867
STAI.iterate iter \x -> void do
868868
sub <- STA.empty
869-
STAI.pushWhile (op x) iter sub
870869
_ <- STA.push x sub
870+
STAI.pushWhile (op x) iter sub
871871
grp <- STA.unsafeFreeze sub
872872
STA.push ((unsafeCoerce :: Array ~> NonEmptyArray) grp) result
873873
STA.unsafeFreeze result

test/Test/Data/Array.purs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,9 @@ testArray = do
306306
log "groupBy should group consecutive equal elements into arrays based on an equivalence relation"
307307
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]]
308308

309+
log "groupBy should be stable"
310+
assert $ A.groupBy (\_ _ -> true) [1, 2, 3] == [nea [1, 2, 3]]
311+
309312
log "nub should remove duplicate elements from the list, keeping the first occurence"
310313
assert $ A.nub [1, 2, 2, 3, 4, 1] == [1, 2, 3, 4]
311314

0 commit comments

Comments
 (0)