Boost logo

Boost :

From: Joel de Guzman (joel_at_[hidden])
Date: 2003-11-04 01:45:53


Aleksey Gurtovoy <agurtovoy_at_[hidden]> wrote:

>> 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<_> > >

I see.

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

Yes.

>> 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'.

Although I wish to have is_sequence<S>, it might not be necessary. Fusion
uses MPL style tag mechanism. I can just use MPL's sequence tags to
specialize fusion::begin(s) and fusion::end(s) inside fusion's namespace.
In the same manner, I can also specialize mpl::begin<s> and mpl::end<t>
for fusion sequences, thus giving us full round-trip interoperability. Very
cool, IMO :-)

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

How about putting identity_view inside MPL instead? Example:

namespace mpl
{
    template <class s>
    struct identity_view : transform_view<s, mpl::identity<_> > {};
};

Or how about pointer_view:

namespace mpl
{
    template <class s>
    struct pointer_view : transform_view<s, add_pointer<_> > {};
};

Views are one honking great idea -- let's do more of those! :-)

Then we can write:

    typedef mpl::pointer_view<mpl::vector<vehicle, car, bicycle, train> > vec;
    vehicle* result = fusion::fold(vec(), static_cast<vehicle*>(0), make_vehicle);

I'm not sure, however, if that's any better than:

    mpl::transform_view<
        mpl::vector<vehicle, car, bicycle, train>
      , add_pointer<_>
> vec;

    vehicle* result = fusion::fold(vec, static_cast<vehicle*>(0), make_vehicle);

Or simply:

    mpl::vector<vehicle*,car*,bicycle*,train*> vec;
    vehicle* result = fusion::fold(vec, static_cast<vehicle*>(0), make_vehicle);

Jeez, it's all connected somehow! I can imagine an Phoenix/LL extension
that will allow:

    mpl::identity_view<vehicle, car, bicycle, train> vec;
    vehicle* result = fusion::fold(
        vec
      , static_cast<vehicle*>(0)
      , new_<identity_of<mpl::_1> >(ll::_2)
    );

Exciting!

Cheers,

-- 
Joel de Guzman
http://www.boost-consulting.com
http://spirit.sf.net

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