Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- data Count = One | Two | ThreeOrMore
- type Datum = { count :: Count, char :: Char }
- data Stat = Stat (Array Datum)
- instance semigroupCount :: Semigroup Count where
- append One One = Two
- append One _ = ThreeOrMore
- append Two _ = ThreeOrMore
- toStat :: Char -> Stat
- toStat = Stat [{ count: One, char: _ }]
- fromDatum :: Datum -> [Char]
- fromDatum {count, char} = case count of
- One -> [char]
- Two -> [char, char]
- ThreeOrMore -> []
- instance semigroupStat :: Semigroup Stat where
- append (Stat a) (Stat b) = case Array.unsnoc a of
- Nothing -> Stat b -- a is empty
- Just { init, last } -> case Array.uncons b of
- Nothing -> Stat a -- b is empty
- Just { head, tail }
- | last.char == head.char ->
- let count = append last.count head.count
- in if count == ThreeOrMore
- then append (Stat init) (Stat tail) -- recursive call
- else (Stat (init <> [{ count, char: head.char }] <> tail)
- | otherwise -> Stat (append a b)
- instance monoidStat :: Monoid Stat where
- mempty = Stat []
- compute :: [Char] -> [Char]
- compute = foldMap toDatum >>> un Stat >>> foldMap fromDatum
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement