Boost logo

Boost :

From: Fletcher, John P (j.p.fletcher_at_[hidden])
Date: 2020-09-29 21:37:16


Peter

It would be nice if it also supported

for_each(a.begin(), a.end(), std::cout << _1 << ' ');

which is the starting example given for boost Lambda

https://www.boost.org/doc/libs/1_74_0/doc/html/lambda.html

As was remarked before, there does not seem to be a function object in std to support operator <<.

I am exploring implementing something locally in a copy of lambda2.hpp, so far without success.

John

________________________________
From: Boost <boost-bounces_at_[hidden]> on behalf of Fletcher, John P via Boost <boost_at_[hidden]>
Sent: 29 September 2020 14:02
To: Peter Dimov <pdimov_at_[hidden]>; boost_at_[hidden] <boost_at_[hidden]>
Cc: Fletcher, John P <j.p.fletcher_at_[hidden]>
Subject: Re: [boost] Review Manager needed for Boost.Lambda2

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 );

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost


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