Skip to content

Commit

Permalink
Polished source
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabriella439 committed Sep 13, 2014
1 parent c8573fe commit 9b56684
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/Control/Foldl.hs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ instance Functor (Fold a) where
instance Applicative (Fold a) where
pure b = Fold (\() _ -> ()) () (\() -> b)
{-# INLINABLE pure #-}

(Fold stepL beginL doneL) <*> (Fold stepR beginR doneR) =
let step (Pair xL xR) a = Pair (stepL xL a) (stepR xR a)
begin = Pair beginL beginR
Expand All @@ -148,6 +149,7 @@ instance Applicative (Fold a) where
instance Monoid b => Monoid (Fold a b) where
mempty = pure mempty
{-# INLINABLE mempty #-}

mappend = liftA2 mappend
{-# INLINABLE mappend #-}

Expand Down Expand Up @@ -252,6 +254,7 @@ instance Monad m => Functor (FoldM m a) where
instance Monad m => Applicative (FoldM m a) where
pure b = FoldM (\() _ -> return ()) (return ()) (\() -> return b)
{-# INLINABLE pure #-}

(FoldM stepL beginL doneL) <*> (FoldM stepR beginR doneR) =
let step (Pair xL xR) a = do
xL' <- stepL xL a
Expand All @@ -271,6 +274,7 @@ instance Monad m => Applicative (FoldM m a) where
instance (Monoid b, Monad m) => Monoid (FoldM m a b) where
mempty = pure mempty
{-# INLINABLE mempty #-}

mappend = liftA2 mappend
{-# INLINABLE mappend #-}

Expand Down Expand Up @@ -589,7 +593,7 @@ eqNub = Fold step (Pair [] id) fin
{-# INLINABLE eqNub #-}

-- | Fold values into a set
set :: (Ord a) => Fold a (Set.Set a)
set :: Ord a => Fold a (Set.Set a)
set = Fold (flip Set.insert) Set.empty id
{-# INLINABLE set #-}

Expand Down
8 changes: 4 additions & 4 deletions src/Control/Foldl/ByteString.hs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ null = L.Fold step True id
{-# INLINABLE null #-}

-- | Return the length of the byte stream in bytes
length :: (Num n) => Fold ByteString n
length :: Num n => Fold ByteString n
length = L.Fold (\n bs -> n + fromIntegral (B.length bs)) 0 id
{-# INLINABLE length #-}

Expand Down Expand Up @@ -149,7 +149,7 @@ find predicate = L.Fold step Nothing' lazy
{-| @(index n)@ returns the @n@th byte of the byte stream, or 'Nothing' if the
stream has an insufficient number of bytes
-}
index :: (Integral n) => n -> Fold ByteString (Maybe Word8)
index :: Integral n => n -> Fold ByteString (Maybe Word8)
index i = L.Fold step (Left' (fromIntegral i)) hush
where
step x bs = case x of
Expand All @@ -164,14 +164,14 @@ index i = L.Fold step (Left' (fromIntegral i)) hush
{-| @(elemIndex w8)@ returns the index of the first byte that equals @w8@, or
'Nothing' if no byte matches
-}
elemIndex :: (Num n) => Word8 -> Fold ByteString (Maybe n)
elemIndex :: Num n => Word8 -> Fold ByteString (Maybe n)
elemIndex w8 = findIndex (w8 ==)
{-# INLINABLE elemIndex #-}

{-| @(findIndex predicate)@ returns the index of the first byte that satisfies
the predicate, or 'Nothing' if no byte satisfies the predicate
-}
findIndex :: (Num n) => (Word8 -> Bool) -> Fold ByteString (Maybe n)
findIndex :: Num n => (Word8 -> Bool) -> Fold ByteString (Maybe n)
findIndex predicate = L.Fold step (Left' 0) hush
where
step x bs = case x of
Expand Down
8 changes: 4 additions & 4 deletions src/Control/Foldl/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ null = L.Fold step True id
{-# INLINABLE null #-}

-- | Return the length of the text stream in characters
length :: (Num n) => Fold Text n
length :: Num n => Fold Text n
length = L.Fold (\n txt -> n + fromIntegral (T.length txt)) 0 id
{-# INLINABLE length #-}

Expand Down Expand Up @@ -146,7 +146,7 @@ find predicate = L.Fold step Nothing' lazy
{-| @(index n)@ returns the @n@th character of the text stream, or 'Nothing' if
the stream has an insufficient number of characters
-}
index :: (Integral n) => n -> Fold Text (Maybe Char)
index :: Integral n => n -> Fold Text (Maybe Char)
index i = L.Fold step (Left' (fromIntegral i)) hush
where
step x txt = case x of
Expand All @@ -161,15 +161,15 @@ index i = L.Fold step (Left' (fromIntegral i)) hush
{-| @(elemIndex c)@ returns the index of the first character that equals @c@,
or 'Nothing' if no character matches
-}
elemIndex :: (Num n) => Char -> Fold Text (Maybe n)
elemIndex :: Num n => Char -> Fold Text (Maybe n)
elemIndex c = findIndex (c ==)
{-# INLINABLE elemIndex #-}

{-| @(findIndex predicate)@ returns the index of the first character that
satisfies the predicate, or 'Nothing' if no character satisfies the
predicate
-}
findIndex :: (Num n) => (Char -> Bool) -> Fold Text (Maybe n)
findIndex :: Num n => (Char -> Bool) -> Fold Text (Maybe n)
findIndex predicate = L.Fold step (Left' 0) hush
where
step x txt = case x of
Expand Down

0 comments on commit 9b56684

Please sign in to comment.