module HaskellWorks.Data.Foldable
  ( foldFirst
  , foldLast
  ) where

foldFirst :: Foldable t => t a -> Maybe a
foldFirst :: t a -> Maybe a
foldFirst = (a -> Maybe a -> Maybe a) -> Maybe a -> t a -> Maybe a
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr (Maybe a -> Maybe a -> Maybe a
forall a b. a -> b -> a
const (Maybe a -> Maybe a -> Maybe a)
-> (a -> Maybe a) -> a -> Maybe a -> Maybe a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Maybe a
forall a. a -> Maybe a
Just) Maybe a
forall a. Maybe a
Nothing
{-# INLINE foldFirst #-}

foldLast :: Foldable t => t a -> Maybe a
foldLast :: t a -> Maybe a
foldLast = (Maybe a -> a -> Maybe a) -> Maybe a -> t a -> Maybe a
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl Maybe a -> a -> Maybe a
forall a. Maybe a -> a -> Maybe a
go Maybe a
forall a. Maybe a
Nothing
  where go :: Maybe a -> a -> Maybe a
        go :: Maybe a -> a -> Maybe a
go Maybe a
_ = a -> Maybe a
forall a. a -> Maybe a
Just
{-# INLINE foldLast #-}