Boost logo

Boost :

From: Fletcher, John P (j.p.fletcher_at_[hidden])
Date: 2020-09-29 13:02:58


Peter

> There's also
> std::transform( nums.begin(),nums.end(), nums.begin(), _1 + 1 );

Thank you, that is the answer which is better from the point of view of functional behaviour. I have also been able to do a binary operation as well:

    std::transform(nums.begin(),nums.end(),nums.begin(),nums.begin(), (_1 + _2) );

I was ignorant of std::transform although I now see it has a reference on the page for std::for_each!

Thanks again

John

P.S. My email system does not make it easy to do quoting.

________________________________
From: Peter Dimov <pdimov_at_[hidden]>
Sent: 29 September 2020 12:35
To: Fletcher, John P <j.p.fletcher_at_[hidden]>; boost_at_[hidden] <boost_at_[hidden]>
Subject: Re: [boost] Review Manager needed for Boost.Lambda2

Fletcher, John P wrote:
> Hi
>
> I have been playing around with lambda2 to see what I can do.
>
> I have an example from
> http://www.enseignement.polytechnique.fr/informatique/INF478/docs/Cpp/en/cpp/algorithm/for_each.html
> where the task is to increment an array of integers:
>
> std::vector<int> nums{3, 4, 2, 9, 15, 267};
> std::for_each(nums.begin(), nums.end(), [](int &n){ n++; }); The nearest I
> can get with lambda2 is this:
>
> std::for_each( nums.begin(),nums.end(), (_1 + 1) );
>
> This compiles although I have found no way to store back the result.

With for_each, there are three possible ways to write it, none of which is
supported by Lambda2:

    std::for_each( nums.begin(),nums.end(), _1 = _1 + 1 );
    std::for_each( nums.begin(),nums.end(), _1 += 1 );
    std::for_each( nums.begin(),nums.end(), ++_1 );

Of those, the first one is impossible because op= must be a member, and the
latter two are possible in principle, but fall outside the initial scope of
the library.

There's also

    std::transform( nums.begin(),nums.end(), nums.begin(), _1 + 1 );


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