Skip to content

Commit

Permalink
Throw away mapAccum[LR]Chunks.
Browse files Browse the repository at this point in the history
Turns out we do not really need it. We thought we need it to implement `scan[lr]`, but actually `mapAccum[LR]` is enough.
  • Loading branch information
kindaro committed Aug 19, 2021
1 parent af6c605 commit 93df278
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions Data/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -588,25 +588,29 @@ compareLength (Chunk c cs) toCmp = compareLength cs (toCmp - fromIntegral (S.le
n <= length t = compareLength t n /= LT
#-}

mapAccumLChunks :: (acc -> S.ByteString -> (acc, S.ByteString)) -> acc -> ByteString -> (acc, ByteString)
mapAccumLChunks function accumulator = fmap (List.foldr Chunk Empty) . List.mapAccumL function accumulator . foldrChunks (:) [ ]

mapAccumRChunks :: (acc -> S.ByteString -> (acc, S.ByteString)) -> acc -> ByteString -> (acc, ByteString)
mapAccumRChunks function accumulator = fmap (List.foldr Chunk Empty) . List.mapAccumR function accumulator . foldrChunks (:) [ ]

-- | The 'mapAccumL' function behaves like a combination of 'map' and
-- 'foldl'; it applies a function to each element of a ByteString,
-- passing an accumulating parameter from left to right, and returning a
-- final value of this accumulator together with the new ByteString.
mapAccumL :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString)
mapAccumL = mapAccumLChunks . S.mapAccumL
mapAccumL f = go
where
go s Empty = (s, Empty)
go s (Chunk c cs) = (s'', Chunk c' cs')
where (s', c') = S.mapAccumL f s c
(s'', cs') = go s' cs

-- | The 'mapAccumR' function behaves like a combination of 'map' and
-- 'foldr'; it applies a function to each element of a ByteString,
-- passing an accumulating parameter from right to left, and returning a
-- final value of this accumulator together with the new ByteString.
mapAccumR :: (acc -> Word8 -> (acc, Word8)) -> acc -> ByteString -> (acc, ByteString)
mapAccumR = mapAccumRChunks . S.mapAccumR
mapAccumR f = go
where
go s Empty = (s, Empty)
go s (Chunk c cs) = (s'', Chunk c' cs')
where (s'', c') = S.mapAccumR f s' c
(s', cs') = go s cs

-- ---------------------------------------------------------------------
-- Building ByteStrings
Expand Down

0 comments on commit 93df278

Please sign in to comment.