[fusion] const and transform algorithm
Hi, What is the reason fusion::transform taskes a const Sequence? template< typename Sequence, typename F > typename result_of::transform<Sequence const, F>::type transform( Sequence const& seq, F f); Does this means that the transform do not change the sequence itself o also its elements? Why the following do not compiles struct get { template<typename Sig> struct result; template<typename T> struct result<get(T)> { typedef typename boost::remove_reference<T>::type ACT; typedef typename ACT::result_type type; }; template<typename ACT> typename ACT::result_type operator()(ACT& act) const { return act.get(); } }; boost::tuple<shared_future<int>,shared_future<char> > t; boost::fusion::result_of::transform< boost::tuple<shared_future<int>,shared_future<char> > , get>::type res= boost::fusion::transform(t, get()); transform call to get()(const shared_future<int>&act). How can I make a transformation changing also the sequence itself? The result of transform is a transform_view, so the function get is not really called. How can I assign this to a boost::tuple<int, char> = ??(boost::fusion::transform(t, get())); boost::as_vector? Is there a as_tuple function? Should this force the evaluation of the get function? Thanks, Vicente
vicente.botet wrote:
Hi,
What is the reason fusion::transform taskes a const Sequence? template< typename Sequence, typename F > typename result_of::transform<Sequence const, F>::type transform( Sequence const& seq, F f);
Does this means that the transform do not change the sequence itself o also its elements?
Yes. Fusion transform returns a lazy (transform) view. This view is non-mutating. It is not like STL's transform which mutates the target container in place. Fusion's is purely functional and lazily evaluated.
Why the following do not compiles struct get { template<typename Sig> struct result; template<typename T> struct result<get(T)> { typedef typename boost::remove_reference<T>::type ACT; typedef typename ACT::result_type type; }; template<typename ACT> typename ACT::result_type operator()(ACT& act) const { return act.get(); } }; boost::tuple<shared_future<int>,shared_future<char> > t; boost::fusion::result_of::transform< boost::tuple<shared_future<int>,shared_future<char> > , get>::type res= boost::fusion::transform(t, get());
transform call to get()(const shared_future<int>&act). How can I make a transformation changing also the sequence itself?
The result of transform is a transform_view, so the function get is not really called. How can I assign this to a boost::tuple<int, char> = ??(boost::fusion::transform(t, get())); boost::as_vector? Is there a as_tuple function? Should this force the evaluation of the get function?
As I said, Fusion transform is purely functional. The result of a transform is a view. The function will only be called when you iterate over the view. The transformation does not change/mutate the sequence itself. If that's what you want, you can assign the view back to the original container. For example: vector<int, char> v; v = transform(v, f); Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
----- Original Message ----- From: "Joel de Guzman" <joel@boost-consulting.com> To: <boost-users@lists.boost.org> Sent: Tuesday, January 06, 2009 11:26 PM Subject: Re: [Boost-users] [fusion] const and transform algorithm
vicente.botet wrote:
Hi,
What is the reason fusion::transform taskes a const Sequence? template< typename Sequence, typename F > typename result_of::transform<Sequence const, F>::type transform( Sequence const& seq, F f);
Does this means that the transform do not change the sequence itself o also its elements?
Yes. Fusion transform returns a lazy (transform) view. This view is non-mutating. It is not like STL's transform which mutates the target container in place. Fusion's is purely functional and lazily evaluated.
Can for_each mutate the sequence elements? if yes, would the following get a transformation I'm locking for? vector<shared_pair<shared_future<int>,int>,shared_pair<shared_future<char>,int> > t; boost::fusion::for_each(t, intrusive_get()); vector<int, char> res=unzip<1>(boost::fusion::for_each(t, intrusive_get())); where shared_pair would be a sequence something like a shared_ptr<pair<A,B> and unzip would be the reverse function of zip. BTW do you think that the counterpart function of zip has a place on Fusion?
boost::tuple<int, char> = ??(boost::fusion::transform(t, get())); boost::as_vector? Is there a as_tuple function? Should this force the evaluation of the get function?
As I said, Fusion transform is purely functional. The result of a transform is a view. The function will only be called when you iterate over the view. The transformation does not change/mutate the sequence itself. If that's what you want, you can assign the view back to the original container. For example:
vector<int, char> v; v = transform(v, f);
I was lock for in the documentation and I have not found this assignation. Where in the documentation can I found that I can do that? If for_each can change/mutate the sequence itself, why don't have a mutating_transform that takes a Sequence& and not a Sequence const& Just a last question. How move semantics maps with purely functional Sequences? Do you plan to add move semantics for the elements of the tuple? The concept of MovableSequence has a sens for you? Thanks, Vicente
vicente.botet wrote:
----- Original Message ----- From: "Joel de Guzman" <joel@boost-consulting.com> To: <boost-users@lists.boost.org> Sent: Tuesday, January 06, 2009 11:26 PM Subject: Re: [Boost-users] [fusion] const and transform algorithm
vicente.botet wrote:
Hi,
What is the reason fusion::transform taskes a const Sequence? template< typename Sequence, typename F
typename result_of::transform<Sequence const, F>::type transform( Sequence const& seq, F f);
Does this means that the transform do not change the sequence itself o also its elements? Yes. Fusion transform returns a lazy (transform) view. This view is non-mutating. It is not like STL's transform which mutates the target container in place. Fusion's is purely functional and lazily evaluated.
Can for_each mutate the sequence elements?
Yes. if yes, would the following get a
transformation I'm locking for?
I don't know. I'm not sure what you are looking for.
vector<shared_pair<shared_future<int>,int>,shared_pair<shared_future<char>,int>
t; boost::fusion::for_each(t, intrusive_get()); vector<int, char> res=unzip<1>(boost::fusion::for_each(t, intrusive_get()));
where shared_pair would be a sequence something like a shared_ptr<pair<A,B> and unzip would be the reverse function of zip. BTW do you think that the counterpart function of zip has a place on Fusion?
I am not sure what it is you want to do. Could you explain in english instead of non-functioning code?
boost::tuple<int, char> = ??(boost::fusion::transform(t, get())); boost::as_vector? Is there a as_tuple function? Should this force the evaluation of the get function? As I said, Fusion transform is purely functional. The result of a transform is a view. The function will only be called when you iterate over the view. The transformation does not change/mutate the sequence itself. If that's what you want, you can assign the view back to the original container. For example:
vector<int, char> v; v = transform(v, f);
I was lock for in the documentation and I have not found this assignation. Where in the documentation can I found that I can do that?
See the sequence/views concepts and the docs for the containers. For example (http://tinyurl.com/84vdvp): Notation: v Instance of vector s A Forward Sequence Expression Semantics: v = s Assigns to a vector, v, from a Forward Sequence, s.
If for_each can change/mutate the sequence itself, why don't have a mutating_transform that takes a Sequence& and not a Sequence const&
Because it is not needed.
Just a last question. How move semantics maps with purely functional Sequences? Do you plan to add move semantics for the elements of the tuple? The concept of MovableSequence has a sens for you?
Makes sense. Last BoostCon, we had a workshop about implementing Fusion for 0x incuding move. We'll have it when it is available. Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
----- Original Message ----- From: "Joel de Guzman" <joel@boost-consulting.com> To: <boost-users@lists.boost.org> Sent: Wednesday, January 07, 2009 1:58 AM Subject: Re: [Boost-users] [fusion] const and transform algorithm
vicente.botet wrote:
----- Original Message ----- From: "Joel de Guzman" <joel@boost-consulting.com> To: <boost-users@lists.boost.org> Sent: Tuesday, January 06, 2009 11:26 PM Subject: Re: [Boost-users] [fusion] const and transform algorithm
vicente.botet wrote:
Hi,
What is the reason fusion::transform taskes a const Sequence? template< typename Sequence, typename F
typename result_of::transform<Sequence const, F>::type transform( Sequence const& seq, F f);
Does this means that the transform do not change the sequence itself o also its elements? Yes. Fusion transform returns a lazy (transform) view. This view is non-mutating. It is not like STL's transform which mutates the target container in place. Fusion's is purely functional and lazily evaluated.
Can for_each mutate the sequence elements?
Yes.
if yes, would the following get a
transformation I'm locking for?
I don't know. I'm not sure what you are looking for.
I just wat to apply the unction get to a tuple of futures and reviver the stored values. T& future<T>::get() is non const
vector<int, char> v; v = transform(v, f);
I was lock for in the documentation and I have not found this assignation. Where in the documentation can I found that I can do that?
See the sequence/views concepts and the docs for the containers. For example (http://tinyurl.com/84vdvp):
Notation: v Instance of vector s A Forward Sequence
Expression Semantics: v = s Assigns to a vector, v, from a Forward Sequence, s.
Thanks for the reference. I was looking for a function prototype. BTW, the example is not in line with the functions. vector<int, float> v(12, 5.5f); std::cout << at_c<0>(v) << std::endl; std::cout << at_c<1>(v) << std::endl;
If for_each can change/mutate the sequence itself, why don't have a mutating_transform that takes a Sequence& and not a Sequence const&
Because it is not needed.
Just a last question. How move semantics maps with purely functional Sequences? Do you plan to add move semantics for the elements of the tuple? The concept of MovableSequence has a sens for you?
Makes sense. Last BoostCon, we had a workshop about implementing Fusion for 0x incuding move. We'll have it when it is available.
Great! Would you try with a Boost.Move library? Thanks, Vicente
vicente.botet wrote:
I don't know. I'm not sure what you are looking for.
I just wat to apply the unction get to a tuple of futures and reviver the stored values. T& future<T>::get() is non const
YOu can use a tie (tuple of references).
vector<int, char> v; v = transform(v, f); I was lock for in the documentation and I have not found this assignation. Where in the documentation can I found that I can do that? See the sequence/views concepts and the docs for the containers. For example (http://tinyurl.com/84vdvp):
Notation: v Instance of vector s A Forward Sequence
Expression Semantics: v = s Assigns to a vector, v, from a Forward Sequence, s.
Thanks for the reference. I was looking for a function prototype. BTW, the example is not in line with the functions. vector<int, float> v(12, 5.5f); std::cout << at_c<0>(v) << std::endl; std::cout << at_c<1>(v) << std::endl;
Why? "Semantics of an expression is defined only where it differs from, or is not defined in Random Access Sequence." You should read more carefully ;-)
If for_each can change/mutate the sequence itself, why don't have a mutating_transform that takes a Sequence& and not a Sequence const& Because it is not needed.
Just a last question. How move semantics maps with purely functional Sequences? Do you plan to add move semantics for the elements of the tuple? The concept of MovableSequence has a sens for you? Makes sense. Last BoostCon, we had a workshop about implementing Fusion for 0x incuding move. We'll have it when it is available.
Great! Would you try with a Boost.Move library?
Hah. Wish I had more time :-P Regards, -- Joel de Guzman http://www.boostpro.com http://spirit.sf.net
participants (2)
-
Joel de Guzman
-
vicente.botet