www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - inplace_merge, nWayUnion

reply bearophile <bearophileHUGS lycos.com> writes:
How do you perform with Phobos2 the C++ STL algorithm inplace_merge()?
http://www.cplusplus.com/reference/algorithm/inplace_merge/
If it is not present yet in Phobos2, then I'd like it eventually to be added.

-------------------

Regarding std.algorithm.nWayUnion, how do you merge iterables of different type
that yield the same type?


import std.stdio, std.algorithm, std.range;
void main() {
    auto r1 = map!q{a * 2}(iota(10));
    auto r2 = map!q{a * 5}(iota(15));
    auto r12 = nWayUnion([r1, r2]); // can't work
    writeln(r12);
}


A differently designed nWayUnion() allows me to use ranges of different type:

nWayUnion(r1, r2, r3, ...)

Bye,
bearophile
Aug 29 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 08/29/2010 06:00 PM, bearophile wrote:
 How do you perform with Phobos2 the C++ STL algorithm inplace_merge()?
 http://www.cplusplus.com/reference/algorithm/inplace_merge/
 If it is not present yet in Phobos2, then I'd like it eventually to be added.

 -------------------

 Regarding std.algorithm.nWayUnion, how do you merge iterables of different
type that yield the same type?


 import std.stdio, std.algorithm, std.range;
 void main() {
      auto r1 = map!q{a * 2}(iota(10));
      auto r2 = map!q{a * 5}(iota(15));
      auto r12 = nWayUnion([r1, r2]); // can't work
      writeln(r12);
 }


 A differently designed nWayUnion() allows me to use ranges of different type:

 nWayUnion(r1, r2, r3, ...)

 Bye,
 bearophile

I haven't implemented inplace_merge yet. About nWayUnion - great idea, but don't ever come back with a bug report "nWayUnion is difficult to understand". Andrei
Aug 29 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrei:
 but don't ever come back with a bug report "nWayUnion is difficult to 
 understand".

Iff nWayUnion will be difficult to undertand I will write that nWayUnion is difficult to undertand. Bye, bearophile
Aug 30 2010
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/30/10 5:59 PDT, bearophile wrote:
 Andrei:
 but don't ever come back with a bug report "nWayUnion is difficult to
 understand".

Iff nWayUnion will be difficult to undertand I will write that nWayUnion is difficult to undertand. Bye, bearophile

My point is you need to understand that you are making contradictory requests. Andrei
Aug 30 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Andrei Alexandrescu:
 My point is you need to understand that you are making contradictory 
 requests.

I understand your point, but I think you are wrong. I think that the usage and semantics of a nWayUnion(r1, r2, ...) are simple to understand and use (regardless the complexity of its signature). You use it as: foreach (x; nWayUnion(r1, r2)) Or as: array(nWayUnion(r1, r2, r3)) etc. This is a natural enough. Bye, bearophile
Aug 30 2010
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 8/30/10 14:06 PDT, bearophile wrote:
 Andrei Alexandrescu:
 My point is you need to understand that you are making contradictory
 requests.

I understand your point, but I think you are wrong. I think that the usage and semantics of a nWayUnion(r1, r2, ...) are simple to understand and use (regardless the complexity of its signature). You use it as: foreach (x; nWayUnion(r1, r2)) Or as: array(nWayUnion(r1, r2, r3)) etc. This is a natural enough. Bye, bearophile

I'd be indeed wrong to claim that. What I was saying is that in a past bug report you complained that find() is too hard to understand. That was because find() supported things like heterogeneous ranges and finding more things than one in a single-pass manner. Making nWayUnion accept heterogeneous ranges would no doubt increase its implementation complexity - probably worth it given the increased leverage. All I'm saying is, please remember that solutions that are at the same time simpler and more general are in short supply. Andrei
Aug 30 2010