@@ -115,11 +115,16 @@ import Prelude
115
115
import Control.Alt ((<|>))
116
116
import Control.Alternative (class Alternative )
117
117
import Control.Lazy (class Lazy , defer )
118
+ import Control.Monad.Eff (Eff , runPure )
118
119
import Control.Monad.Rec.Class (class MonadRec , Step (..), tailRecM2 )
120
+ import Control.Monad.ST (ST )
119
121
122
+ import Data.Array.ST (STArray , emptySTArray , pushSTArray , runSTArray' )
123
+ import Data.Array.ST.Iterator (iterate , iterator , pushWhile )
120
124
import Data.Foldable (class Foldable , foldl , foldr )
121
125
import Data.Foldable (foldl , foldr , foldMap , fold , intercalate , elem , notElem , find , findMap , any , all ) as Exports
122
126
import Data.Maybe (Maybe (..), maybe , isJust , fromJust )
127
+ import Data.Newtype (class Newtype , unwrap )
123
128
import Data.NonEmpty (NonEmpty , (:|))
124
129
import Data.Traversable (scanl , scanr ) as Exports
125
130
import Data.Traversable (sequence , traverse )
@@ -548,14 +553,25 @@ group' = group <<< sort
548
553
-- | Group equal, consecutive elements of an array into arrays, using the
549
554
-- | specified equivalence relation to detemine equality.
550
555
groupBy :: forall a . (a -> a -> Boolean ) -> Array a -> Array (NonEmpty Array a )
551
- groupBy op = go []
556
+ groupBy op xs =
557
+ runPure do
558
+ runGroupedSTArray do
559
+ result <- emptySTArray
560
+ iter <- iterator (xs !! _)
561
+ iterate iter \x -> do
562
+ sub <- emptySTArray
563
+ pushSTArray result (x :| sub)
564
+ pushWhile (op x) iter sub
565
+ pure result
552
566
where
553
- go :: Array (NonEmpty Array a ) -> Array a -> Array (NonEmpty Array a )
554
- go acc xs = case uncons xs of
555
- Just o ->
556
- let sp = span (op o.head) o.tail
557
- in go ((o.head :| sp.init) : acc) sp.rest
558
- Nothing -> reverse acc
567
+ runGroupedSTArray
568
+ :: forall b r
569
+ . (forall h . Eff (st :: ST h | r ) (STArray h (NonEmpty (STArray h ) b )))
570
+ -> Eff r (Array (NonEmpty Array b ))
571
+ runGroupedSTArray a = map unwrap (runSTArray' (map Grouped a ))
572
+
573
+ newtype Grouped a arr = Grouped (arr (NonEmpty arr a ))
574
+ derive instance newtypeGrouped :: Newtype (Grouped a arr ) _
559
575
560
576
-- | Remove the duplicates from an array, creating a new array.
561
577
nub :: forall a . Eq a => Array a -> Array a
0 commit comments