Boost logo

Boost :

From: Aleksey Gurtovoy (agurtovoy_at_[hidden])
Date: 2003-11-03 22:39:57


Joel de Guzman wrote:
> Aleksey Gurtovoy <agurtovoy_at_[hidden]> wrote:
> > IMO the way the OP problem is formulated, it literally screams for
> > 'fold'/'accumulate':
> >
> >> vehicle* make(vehicle* v0)
> >> {
> >> vehicle* v1 = new car( v0 );
> >> vehicle* v2 = new bicycle( v1 );
> >> vehicle* v3 = new train( v2 );
> >> return v3;
> >> }
> >
> > And then the 'fold' version requires less code on the user side.
> >
> >> Is identity_view supposed to
> >> default-construct a vehicle,car, bicycle, and train?
> >
> > Nope, it constructs 'identity<vehicle>', 'identity<car>', etc.
>
> Very nice! And it is doable now. Apart from identity_view, we have
> all the necessary ingredients now. identity_view is quite simple.
> It is just mpl::transform<s, mpl::identity<_> >, I'm not sure if
> it's worthy to be included into the library.

I was thinking more of

    fusion::identity_view<s> ==
        fusion::type_sequence< mpl::transform_view<s, mpl::identity<_> > >

which is a little too verbose than I would be confortable to spell out
every time :) - but please see below.

> Here's the actual syntax:
> (untested):
>
> typedef mpl::vector<vehicle,car,bicycle,train> vec;
> typedef mpl::transform<s, mpl::identity<_> > ivec;
> fusion::type_sequence<ivec> view;
> vehicle* result = fusion::fold(view, static_cast<vehicle*>(0),
make_vehicle);
>
> I think it is possible to work directly on mpl sequences.

That would be really cool, although AFAICS for that to work reliably we
would need something like 'fusion::is_sequence'.

> So, the syntax
> might be simplified to:
>
> typedef mpl::vector<vehicle,car,bicycle,train> vec;
> typedef mpl::transform<s, mpl::identity<_> > ivec;
> vehicle* result = fusion::fold(ivec(), static_cast<vehicle*>(0),
make_vehicle);
>
> Or, if you rig make_vehicle as:
>
> struct make_vehicle
> {
> template< typename T > struct result
> {
> typedef vehicle* type;
> };
>
> template< typename T >
> vehicle* operator()(T*, vehicle* ptr) const
> {
> return new T(ptr);
> }
> };
>
> you don't need the identity, so you can further simplify:
>
> typedef mpl::vector<vehicle*,car*,bicycle*,train*> vec;
> vehicle* result = fusion::fold(vec(), static_cast<vehicle*>(0),
make_vehicle);

IMO, it's beautiful. Or at least as beautiful as it can get within the
current
language. I would love to see things work this way.

Aleksey


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk