Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Data.ByteString.Lazy.compareLength #300

Merged
merged 15 commits into from
Oct 16, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Data/ByteString/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ module Data.ByteString.Lazy (
all, -- :: (Word8 -> Bool) -> ByteString -> Bool
maximum, -- :: ByteString -> Word8
minimum, -- :: ByteString -> Word8
compareLength, -- :: ByteString -> Int -> Ordering

-- * Building ByteStrings
-- ** Scans
Expand Down Expand Up @@ -525,6 +526,18 @@ minimum (Chunk c cs) = foldlChunks (\n c' -> n `min` S.minimum c')
(S.minimum c) cs
{-# INLINE minimum #-}

-- | /O(n)/ 'compareLength' compares the length of a 'ByteString'
gutjuri marked this conversation as resolved.
Show resolved Hide resolved
-- to an 'Int'
compareLength :: ByteString -> Int -> Ordering
compareLength s toCmp = go 0 s
where
go currLen Empty = compare currLen toCmp
go currLen (Chunk c cs) = let nextLen = currLen + S.length c
in if nextLen > toCmp
then GT
else go nextLen cs
sjakobi marked this conversation as resolved.
Show resolved Hide resolved
{-# INLINE compareLength #-}

-- | 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
Expand Down