Topic: Suggestion for lib function: apply<>


Author: AlbertoBarbati@libero.it (Alberto Barbati)
Date: Sun, 4 Jan 2004 04:14:34 +0000 (UTC)
Raw View
Jeff Schwab wrote:
> red floyd wrote:
>
>>
>> I have a suggestion for the standard library....
>>
>> This is sort of a combination of std::transform() and std::for_each().
>> It applies the binary function to each iterator in [start1, end1), to the
>> corresponding member of the container beginning with start2.  Like
>> for_each(), it returns the functor.
>>
>> [snip]
>
> How is this different from std::transform, aside from the lack of an
> output iterator?

transform() requires that "binary_op shall not have any side effects"
(25.2.3/2). If I understand correctly the proposal, the real usefulness
of apply(), like for_each(), is precisely in the side effects of the
functor.

I like the idea, but I disagree on the name "apply". Why don't you call
it simply for_each()? As it performs essentially the same algorithm as
for_each(), I believe it's better to overload the name.

Alberto

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: no.spam@here.dude (red floyd)
Date: Mon, 5 Jan 2004 02:08:29 +0000 (UTC)
Raw View
Jeff Schwab wrote:
> red floyd wrote:
>
>>
>> I have a suggestion for the standard library....
>>
>> This is sort of a combination of std::transform() and std::for_each().
>> It applies the binary function to each iterator in [start1, end1), to the
>> corresponding member of the container beginning with start2.  Like
>> for_each(), it returns the functor.
>>
>> ---
>> template<typename Iter1, typename InIter2, typename BinaryFunc>
>> BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc
>> func)
>> {
>>     while (start1 != end1)
>>     {
>>         func(*start1, *start2);
>>         ++start1;
>>         ++start2;
>>     }
>>     return func;
>> }
>> ---
>> You could dummy up something like this with for_each() and a clever
>> functor, but
>> you begin to lose readability.   Any comments, suggestions?
>
>
> How is this different from std::transform, aside from the lack of an
> output iterator?
>

transform performs the operation and copies it to the output.  the posted
function does the operation "in place", without the overhead of operator=().

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: no.spam@here.dude (red floyd)
Date: Thu, 1 Jan 2004 08:31:43 +0000 (UTC)
Raw View
I have a suggestion for the standard library....

This is sort of a combination of std::transform() and std::for_each().
It applies the binary function to each iterator in [start1, end1), to the
corresponding member of the container beginning with start2.  Like for_each(),
it returns the functor.

---
template<typename Iter1, typename InIter2, typename BinaryFunc>
BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc func)
{
     while (start1 != end1)
     {
         func(*start1, *start2);
         ++start1;
         ++start2;
     }
     return func;
}
---
You could dummy up something like this with for_each() and a clever functor, but
you begin to lose readability.   Any comments, suggestions?


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: technews@kangaroologic.com (Jonathan Turkanis)
Date: Thu, 1 Jan 2004 09:46:03 +0000 (UTC)
Raw View
"red floyd" <no.spam@here.dude> wrote in message
news:c5KIb.4369$R85.1407@newssvr29.news.prodigy.com...
>
> I have a suggestion for the standard library....
>
> This is sort of a combination of std::transform() and std::for_each().
> It applies the binary function to each iterator in [start1, end1), to the
> corresponding member of the container beginning with start2.  Like
for_each(),
> it returns the functor.
>
> ---
> template<typename Iter1, typename InIter2, typename BinaryFunc>
> BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc
func)
> {
>      while (start1 != end1)
>      {
>          func(*start1, *start2);
>          ++start1;
>          ++start2;
>      }
>      return func;
> }
> ---
> You could dummy up something like this with for_each() and a clever
functor, but
> you begin to lose readability.   Any comments, suggestions?
>
>

A number of algorithms in the standard library could be generalized in this
way, to operate on two sequences instead of one, using binary rather than
unary predicates or functions. For instance, one could have a version of
find_if which takes two sequences and a binary predicate.

Instead of introducing double versions of these algorithms, a more general
solution might be to define iterator adaptors which turn pairs of iterators
into iterators over pairs.

Jonathan


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jeffplus@comcast.net (Jeff Schwab)
Date: Fri, 2 Jan 2004 01:18:12 +0000 (UTC)
Raw View
red floyd wrote:
>
> I have a suggestion for the standard library....
>
> This is sort of a combination of std::transform() and std::for_each().
> It applies the binary function to each iterator in [start1, end1), to the
> corresponding member of the container beginning with start2.  Like
> for_each(), it returns the functor.
>
> ---
> template<typename Iter1, typename InIter2, typename BinaryFunc>
> BinaryFunc apply(Iter1 start1, Iter1 end1, InIter2 start2, BinaryFunc func)
> {
>     while (start1 != end1)
>     {
>         func(*start1, *start2);
>         ++start1;
>         ++start2;
>     }
>     return func;
> }
> ---
> You could dummy up something like this with for_each() and a clever
> functor, but
> you begin to lose readability.   Any comments, suggestions?

How is this different from std::transform, aside from the lack of an
output iterator?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]