Quantcast

Does this function have a name?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Does this function have a name?

Paul Brauner-3
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does this function have a name?

Paul Brauner-3
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 <[hidden email]> 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does this function have a name?

Matt
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 <[hidden email]> 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 <[hidden email]> 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.


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does this function have a name?

Paul Brauner-3

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 <[hidden email]> 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 <[hidden email]> 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 <[hidden email]> 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.


_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does this function have a name?

John Wiegley-2
In reply to this post by Paul Brauner-3
>>>>> "PB" == Paul Brauner <[hidden email]> writes:

PB> Oops, there's a free variable in there, let me fix it:
PB> first f [] = Nothing
PB> first f (x:xs) = fmap (:xs) (f x) <|> fmap (x:) (first f xs)

If you break up the test from the modification (instead of fusing the
operation into 'f'), this sort of thing becomes quite easy to express
using lens:

  [1..10] & partsOf (traverse.filtered (>2))._head +~ 3

If the list has no elements >2, the operation is a no-op.

--
John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2
_______________________________________________
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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Does this function have a name?

Paul Brauner-3
Hi John,

this is a nice way of expressing it! I was hoping there would be some more fundamental function that I would have missed though. By fundamental I mean that I was hoping there was some function of a "core" typeclass like Alternative that I had missed that was a general case of my function. Thanks anyway, I learned about lens' partsOf :)

Paul

On Sat, Apr 8, 2017 at 11:20 PM John Wiegley <[hidden email]> wrote:
>>>>> "PB" == Paul Brauner <[hidden email]> writes:

PB> Oops, there's a free variable in there, let me fix it:
PB> first f [] = Nothing
PB> first f (x:xs) = fmap (:xs) (f x) <|> fmap (x:) (first f xs)

If you break up the test from the modification (instead of fusing the
operation into 'f'), this sort of thing becomes quite easy to express
using lens:

  [1..10] & partsOf (traverse.filtered (>2))._head +~ 3

If the list has no elements >2, the operation is a no-op.

--
John Wiegley                  GPG fingerprint = 4710 CF98 AF9B 327B B80F
http://newartisans.com                          60E1 46C4 BD1A 7AC1 4BA2

_______________________________________________
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.
Loading...