[Haskell-cafe] Does this function have a name?
Paul Brauner
polux2001 at gmail.com
Fri Apr 7 19:24:12 UTC 2017
Hi Matt,
this isn't exactly the same thing I think. My function replaces the first
element in the list for which f returns (Just x) by x, and leaves the rest
of the list untouched.
Paul
On Fri, Apr 7, 2017, 20:53 Matt <parsonsmatt at gmail.com> wrote:
> I think you're looking at the Monoid instance for First:
>
> λ> import Data.Monoid
> λ> let first f = getFirst . foldMap (First . f)
> λ> first (\x -> guard (x > 2) *> pure x) [1..10]
> Just 3
>
>
>
> Matt Parsons
>
> On Fri, Apr 7, 2017 at 2:57 AM, Paul Brauner <polux2001 at gmail.com> wrote:
>
> Oops, there's a free variable in there, let me fix it:
>
> first f [] = Nothing
> first f (x:xs) = fmap (:xs) (f x) <|> fmap (x:) (first f xs)
>
> Paul
>
> On Fri, Apr 7, 2017 at 10:42 AM Paul Brauner <polux2001 at gmail.com> wrote:
>
> Hello Haskell Cafe,
>
> I had to write the following function and I was wondering if it (or its
> generalization to Alternative or Traversable) was exposed by some library
> under a different name:
>
> first [] = Nothing
> first (t:ts) = fmap (:ts) (s t) <|> fmap (t:) (first ts)
>
> Somehow it is to "traverse" as "any" is to "all".
>
> Cheers,
> Paul
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170407/261a0e37/attachment.html>
More information about the Haskell-Cafe
mailing list