C# to D

bearophile via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Mar 25 13:09:52 PDT 2015


Dennis Ritchie:

> A more effective solution for C ++:
>
> #include <iostream>
> #include <vector>
> #include <range/v3/all.hpp>
>
> int main() {
>   using namespace ranges;
>
>   auto rng = istream<int>( std::cin )
>            | to_vector
>            | action::sort
>            | view::group_by( std::equal_to<int>() )
>            | copy
>            | action::stable_sort( []( const auto& e1, const 
> auto& e2 ) { return distance( e1 ) < distance( e2 ); } );
>   std::cout << ( rng );
> }


This is still not very efficient (perhaps the last sorting has to 
be stable):

void main() {
     import std.stdio, std.algorithm, std.typecons, std.array;

     [7, 5, 7, 3, 3, 5, 3, 3, 0, 3, 1, 1, 5, 1, 1, 1, 2, 2, 8, 5, 
8, 8]
     .sort()
     .groupBy!((a, b) => a == b)
     .map!array
     .array
     .sort!q{a.length > b.length}
     .joiner
     .writeln;
}


Bye,
bearophile


More information about the Digitalmars-d-learn mailing list