| | [Haskell-cafe] a beginner question: decorate-op-undecorate  | 
05-02-10, 05:34 PM
| | | [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.
| 
05-02-10, 05:50 PM
| | | 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.
| 
06-02-10, 12:14 AM
| | | 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----- | 
06-02-10, 12:25 AM
| | | 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.
| 
06-02-10, 12:41 AM
| | | 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----- | 
06-02-10, 02:23 AM
| | | 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.
|  | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | 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 | |