safe ways how to get head/last of Seq (or Foldable in general)

Hi, today I was a bit surprised that apparently there is no easy way how to safely get the head element of `Seq` in a point-free way. Of course there is `viewl`, but it seems the data type has no folding function (something like 'foldViewL :: b -> (a -> Seq a -> b) -> b`. Is there any existing function like `Seq a -> Maybe a` to safely retrieve the head (or last) element? If not, I'd suggest to add headMaybe :: (Foldable t) => t a -> Maybe a headMaybe = getFirst . foldMap (First . Just) and similarly lastMaybe to Data.Foldable. Thanks, Petr

The mono-traversable package provides headMay[1] which works for Seq.
[1]
http://www.stackage.org/haddock/nightly-2015-02-25/mono-traversable-0.9.0.1/...
On Thu Feb 26 2015 at 4:18:31 PM Petr Pudlák
Hi,
today I was a bit surprised that apparently there is no easy way how to safely get the head element of `Seq` in a point-free way. Of course there is `viewl`, but it seems the data type has no folding function (something like 'foldViewL :: b -> (a -> Seq a -> b) -> b`.
Is there any existing function like `Seq a -> Maybe a` to safely retrieve the head (or last) element?
If not, I'd suggest to add
headMaybe :: (Foldable t) => t a -> Maybe a headMaybe = getFirst . foldMap (First . Just)
and similarly lastMaybe to Data.Foldable.
Thanks, Petr _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

So are you suggesting that we add these to Data.Foldable?
headMay = foldr (\x _ -> Just x) Nothing
lastMay = foldl (\_ x -> Just x) Nothing
On Feb 26, 2015 9:18 AM, "Petr Pudlák"
Hi,
today I was a bit surprised that apparently there is no easy way how to safely get the head element of `Seq` in a point-free way. Of course there is `viewl`, but it seems the data type has no folding function (something like 'foldViewL :: b -> (a -> Seq a -> b) -> b`.
Is there any existing function like `Seq a -> Maybe a` to safely retrieve the head (or last) element?
If not, I'd suggest to add
headMaybe :: (Foldable t) => t a -> Maybe a headMaybe = getFirst . foldMap (First . Just)
and similarly lastMaybe to Data.Foldable.
Thanks, Petr
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe

Yes, that'd be my suggestion.
čt 26. 2. 2015 v 16:03 odesílatel David Feuer
So are you suggesting that we add these to Data.Foldable?
headMay = foldr (\x _ -> Just x) Nothing lastMay = foldl (\_ x -> Just x) Nothing On Feb 26, 2015 9:18 AM, "Petr Pudlák"
wrote: Hi,
today I was a bit surprised that apparently there is no easy way how to safely get the head element of `Seq` in a point-free way. Of course there is `viewl`, but it seems the data type has no folding function (something like 'foldViewL :: b -> (a -> Seq a -> b) -> b`.
Is there any existing function like `Seq a -> Maybe a` to safely retrieve the head (or last) element?
If not, I'd suggest to add
headMaybe :: (Foldable t) => t a -> Maybe a headMaybe = getFirst . foldMap (First . Just)
and similarly lastMaybe to Data.Foldable.
Thanks, Petr
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
participants (3)
-
David Feuer
-
Michael Snoyman
-
Petr Pudlák