nntpnews.net

Global Usenet Archiver


Register

[Haskell-cafe] a beginner question: decorate-op-undecorate

Reply

  #1  
Old 05-02-10, 05:34 PM
Aran Donohue
 
Posts: n/a
Default [Haskell-cafe] a beginner question: decorate-op-undecorate

Hi Haskell-Cafe,

Consider a data type such as

data Binding = Binding Var (Either Value [Value])

representing a variable bound either to a fixed value or that has a list of
possible values.

I'd like to perform an operation on say, the fixed-value members of a list
of bindings. Data.Either has "partitionEithers"---I'd essentially like to
use partitionEithers, but in a way that it "peeks" into the value field of
the binding. For the sake of argument, let's say I can't or can't modify
Binding to move the Either to the outside.

What would be an idiomatic Haskell way to accomplish this? Currently I've
got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which
is my own version of partitionEithers that calls a selector first. Another
option would be to map each Binding to a new datatype that has the Either on
the outside, use partitionEithers, and map back.

Thanks,
Aran

_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #2  
Old 05-02-10, 05:50 PM
Felipe Lessa
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

On Fri, Feb 05, 2010 at 12:34:01PM -0500, Aran Donohue wrote:
> Hi Haskell-Cafe,
>
> Consider a data type such as
>
> data Binding = Binding Var (Either Value [Value])
>
> representing a variable bound either to a fixed value or that has a list of
> possible values.
>
> I'd like to perform an operation on say, the fixed-value members of a list
> of bindings. Data.Either has "partitionEithers"---I'd essentially like to
> use partitionEithers, but in a way that it "peeks" into the value field of
> the binding. For the sake of argument, let's say I can't or can't modify
> Binding to move the Either to the outside.
>
> What would be an idiomatic Haskell way to accomplish this? Currently I've
> got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])" which
> is my own version of partitionEithers that calls a selector first. Another
> option would be to map each Binding to a new datatype that has the Either on
> the outside, use partitionEithers, and map back.
>
> Thanks,
> Aran


> _______________________________________________
> Haskell-Cafe mailing list
>
Code:
Content visible to registered users only.
>
Code:
Content visible to registered users only.


You could try using uniplate[1], something like

transformBi (either doWhatYouLike id)

I guess .

[1]
Code:
Content visible to registered users only.
--
Felipe.
_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #3  
Old 06-02-10, 12:14 AM
Brandon S. Allbery KF8NH
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

On Feb 5, 2010, at 12:34 , Aran Donohue wrote:
> data Binding = Binding Var (Either Value [Value])
>
> representing a variable bound either to a fixed value or that has a
> list of possible values.
>
> I'd like to perform an operation on say, the fixed-value members of
> a list of bindings. Data.Either has "partitionEithers"---I'd
> essentially like to use partitionEithers, but in a way that it
> "peeks" into the value field of the binding. For the sake of
> argument, let's say I can't or can't modify Binding to move the
> Either to the outside.
>
> What would be an idiomatic Haskell way to accomplish this? Currently
> I've got "liftedPartitionEithers :: [a] -> (a -> Either b c) ->
> ([a], [a])" which is my own version of partitionEithers that calls a
> selector first. Another option would be to map each Binding to a new
> datatype that has the Either on the outside, use partitionEithers,
> and map back.



Hm. Does it make sense to make this a Functor?

> instance Functor Binding where
> fmap f (Binding v e) = Binding v (f e)


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell]
Code:
Content visible to registered users only.
system administrator [openafs,heimdal,too many hats]
Code:
Content visible to registered users only.
electrical and computer engineering, carnegie mellon university KF8NH



_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iEYEARECAAYFAktstDIACgkQIn7hlCsL25WCYQCeORpOyA9w7K XBxDQRZK9vKNtE
VPUAoJJ1FeXi5c48utr4MV1dCpZK+POi
=JwsK
-----END PGP SIGNATURE-----

Reply With Quote
  #4  
Old 06-02-10, 12:25 AM
Luke Palmer
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

On Fri, Feb 5, 2010 at 10:34 AM, Aran Donohue <aran.donohue@gmail.com> wrote:
> What would be an idiomatic Haskell way to accomplish this? Currently I've
> got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a], [a])"*which
> is my own version of partitionEithers that calls a selector first.


Since you are not using b or c anywhere else, the only thing you care
about in that Either is whether it is Left or Right. Which makes it
seem much more like a Bool. After this conversion, I can hoogle for
your signature.

Code:
Content visible to registered users only.
[a]+-%3E+%28a+-%3E+Bool%29+-%3E+%28[a]%2C[a]%29

Which gives, among other things, Data.List.partition :: (a -> Bool) ->
[a] -> ([a],[a]).

Without more details about the precise thing you want to accomplish, I
don't know what else to say. Many idioms are about the details of the
problem, even down to argument order.

Luke
_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
  #5  
Old 06-02-10, 12:41 AM
Brandon S. Allbery KF8NH
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

On Feb 5, 2010, at 19:13 , Brandon S. Allbery KF8NH wrote:
> Hm. Does it make sense to make this a Functor?
>
> > instance Functor Binding where
> > fmap f (Binding v e) = Binding v (f e)



Inaccurate/incomplete as written, since Functor expects kind (*) and
Binding is (* -> *). You'd have to fix v to declare instances.

--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell]
Code:
Content visible to registered users only.
system administrator [openafs,heimdal,too many hats]
Code:
Content visible to registered users only.
electrical and computer engineering, carnegie mellon university KF8NH



_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)

iEYEARECAAYFAktsuoIACgkQIn7hlCsL25WuZQCdF7VnC7l4D7 FPpM1x0GpfpSmy
iZoAoI4UXBcuEykZMoBOgSSfEV6I91uw
=pXH8
-----END PGP SIGNATURE-----

Reply With Quote
  #6  
Old 06-02-10, 02:23 AM
Aran Donohue
 
Posts: n/a
Default Re: [Haskell-cafe] a beginner question: decorate-op-undecorate

Thanks for the helpful thoughts.

I guess I was just reaching for a Haskell version of a programming pattern
from other languages---dealing with baggage if you will.

Thanks,
Aran

On Fri, Feb 5, 2010 at 7:24 PM, Luke Palmer <lrpalmer@gmail.com> wrote:

> On Fri, Feb 5, 2010 at 10:34 AM, Aran Donohue <aran.donohue@gmail.com>
> wrote:
> > What would be an idiomatic Haskell way to accomplish this? Currently I've
> > got "liftedPartitionEithers :: [a] -> (a -> Either b c) -> ([a],

> [a])" which
> > is my own version of partitionEithers that calls a selector first.

>
> Since you are not using b or c anywhere else, the only thing you care
> about in that Either is whether it is Left or Right. Which makes it
> seem much more like a Bool. After this conversion, I can hoogle for
> your signature.
>
>
>
Code:
Content visible to registered users only.
[a]+-%3E+%28a+-%3E+Bool%29+-%3E+%28[a]%2C[a]%29
>
> Which gives, among other things, Data.List.partition :: (a -> Bool) ->
> [a] -> ([a],[a]).
>
> Without more details about the precise thing you want to accomplish, I
> don't know what else to say. Many idioms are about the details of the
> problem, even down to argument order.
>
> Luke
>


_______________________________________________
Haskell-Cafe mailing list
Code:
Content visible to registered users only.
Code:
Content visible to registered users only.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Haskell-cafe] Hayoo and Hoogle (beginner question) drostin77 fa.haskell 10 08-12-09 01:12 AM
[Haskell-beginners] decorate-sort-undecorate in haskell Ivan Uemlianin fa.haskell 7 11-07-09 11:28 AM
[Haskell-cafe] Beginner SOS Manu Gupta fa.haskell 7 28-05-09 07:30 AM
[Haskell-cafe] beginner question: assigning local variable to afunction Nico Rolle fa.haskell 6 07-05-09 09:45 PM
[Haskell-cafe] Beginner question Benjamin Bach fa.haskell 3 02-01-09 05:19 PM


All times are GMT +1. The time now is 09:11 AM. Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0



For ads on this site use independent advertising companies. These companies may use some data (which does not include your name, address, email address or telephone number) about your visits to this and other websites to create advertisements on products and services you might enjoy. If you'd like more information and to know the options available to prevent the use of such information by these companies, click here

Abuse Ticket System