[Haskell-cafe] safe ways how to get head/last of Seq (or Foldable in general)
michael at snoyman.com
Thu Feb 26 14:26:08 UTC 2015
The mono-traversable package provides headMay which works for Seq.
On Thu Feb 26 2015 at 4:18:31 PM Petr Pudlák <petr.mvd at gmail.com> wrote:
> 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.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe