Proposal: Add First and Last wrappers around Maybe to
Data.Monoid
Jeffrey Yasskin
jyasskin at gmail.com
Tue Mar 6 12:42:43 EST 2007
On 3/5/07, Andriy Palamarchuk <apa3a at yahoo.com> wrote:
> Jeffrey, would it be difficult to add an example or
> two for the intended usage?
Not at all. Two reasonable examples are:
> findLast :: Foldable t => (a -> Bool) -> t a -> Maybe a
> findLast pred = getLast . foldMap (\x -> if pred x then Last (Just x) else Last Nothing)
And, assuming Data.Map were extended with
alterA :: (Applicative f, Ord k) => (Maybe a -> f (Maybe a)) -> k ->
Map k a -> f (Map k a)
> instance Monoid a => Applicative ((,) a) -- from Control.Applicative
>
> insertLookupWithKey :: Ord k => (k -> v -> v -> v) -> k -> v -> Map k v -> (Maybe v, Map k v)
> insertLookupWithKey combine key value =
> Arrow.first getFirst . alterA doChange key
> where
> doChange Nothing = (First Nothing, Just value)
> doChange (Just oldValue) =
> (First (Just oldValue),
> Just (combine key value oldValue))
I haven't run these yet, so they may need some tweaking.
Since the rest of Data.Monoid doesn't have example uses, where do you
want me to put these?
Jeffrey
More information about the Libraries
mailing list