tuple update

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

tuple update

Ovidiu Deac
Does anybody know if there are any functions like these in the Haskell
libray?

updateSnd f (x,y) -> (x, f y)

updateFst f (x,y) -> (f x, y)

Thanks,
ovidiu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120309/6a3011ff/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

tuple update

Brent Yorgey-2
On Fri, Mar 09, 2012 at 09:07:24PM +0200, Ovidiu Deac wrote:
> Does anybody know if there are any functions like these in the Haskell
> libray?
>
> updateSnd f (x,y) -> (x, f y)
>
> updateFst f (x,y) -> (f x, y)

Yes: 'first' and 'second' from Control.Arrow.

-Brent


Reply | Threaded
Open this post in threaded view
|

tuple update

Thomas Davie
first ofc also being known as fmap.

Bob
if (*ra4 != 0xffc78948) { return false; }

On 9 Mar 2012, at 19:09, Brent Yorgey wrote:

> On Fri, Mar 09, 2012 at 09:07:24PM +0200, Ovidiu Deac wrote:
>> Does anybody know if there are any functions like these in the Haskell
>> libray?
>>
>> updateSnd f (x,y) -> (x, f y)
>>
>> updateFst f (x,y) -> (f x, y)
>
> Yes: 'first' and 'second' from Control.Arrow.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120309/2b4386f7/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

tuple update

Ovidiu Deac
@Bert first& second are exactly what I was looking for. Thanks.

@Thomas: I'm not sure what you mean by fmap
What I wanted is to do this:
> let f = map $ first (*2)
> f [(1,1),(2,2),(3,3),(4,4)]
[(2,1),(4,2),(6,3),(8,4)]

On Fri, Mar 9, 2012 at 9:23 PM, Thomas Davie <tom.davie at gmail.com> wrote:

> first ofc also being known as fmap.
>
> Bob
>
> if (*ra4 != 0xffc78948) { return false; }
>
>
> On 9 Mar 2012, at 19:09, Brent Yorgey wrote:
>
> On Fri, Mar 09, 2012 at 09:07:24PM +0200, Ovidiu Deac wrote:
>
> Does anybody know if there are any functions like these in the Haskell
>
> libray?
>
>
> updateSnd f (x,y) -> (x, f y)
>
>
> updateFst f (x,y) -> (f x, y)
>
>
> Yes: 'first' and 'second' from Control.Arrow.
>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120309/f6c172a8/attachment.htm>

Reply | Threaded
Open this post in threaded view
|

tuple update

Brent Yorgey-2
In reply to this post by Thomas Davie
Actually it's second that is fmap.

Prelude> :m +Control.Monad.Instances Control.Arrow
Prelude Control.Monad.Instances Control.Arrow> fmap (*2) (1,3)
(1,6)
Prelude Control.Monad.Instances Control.Arrow> second (*2) (1,3)
(1,6)

-Brent

On Fri, Mar 09, 2012 at 07:23:50PM +0000, Thomas Davie wrote:

> first ofc also being known as fmap.
>
> Bob
> if (*ra4 != 0xffc78948) { return false; }
>
> On 9 Mar 2012, at 19:09, Brent Yorgey wrote:
>
> > On Fri, Mar 09, 2012 at 09:07:24PM +0200, Ovidiu Deac wrote:
> >> Does anybody know if there are any functions like these in the Haskell
> >> libray?
> >>
> >> updateSnd f (x,y) -> (x, f y)
> >>
> >> updateFst f (x,y) -> (f x, y)
> >
> > Yes: 'first' and 'second' from Control.Arrow.
> >
> > -Brent
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
>


Reply | Threaded
Open this post in threaded view
|

tuple update

Ertugrul Söylemez
In reply to this post by Ovidiu Deac
Ovidiu Deac <ovidiudeac at gmail.com> wrote:

> Does anybody know if there are any functions like these in the Haskell
> libray?
>
> updateSnd f (x,y) -> (x, f y)
>
> updateFst f (x,y) -> (f x, y)

While the function arrow mentioned by Brent is enough for many purposes,
I just want to have mentioned Edward Kmett's amazing data-lens library,
which provides prefab lenses for tuples:

    (3, 5) ^. fstLens = 3
    (3, 5) ^. sndLens = 5

    (fstLens ^= 4) (3, 5) = (4, 5)
    (sndLens ^= 4) (3, 5) = (3, 4)

    (fstLens ^%= f) (3, 5) = (f 3, 5)

This is especially useful when your tuple is part of the implicit
argument of a state monad (data-lens-fd package):

    access fstLens :: State (a, b) a

                       -- starting state (3, 5)
    fstLens ~= 10      -- (10, 5)
    fstLens += 1       -- (11, 5)
    fstLens %= negate  -- (-11, 5)

    focus fstLens $ do
        -- Do something with only the fst value here.


Greets,
Ertugrul

--
nightmare = unsafePerformIO (getWrongWife >>= sex)
http://ertes.de/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120310/4a6589cd/attachment.pgp>