sort, .array and folding on immutable data (finding most common character in column of matrix)

Ali via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 19 03:42:55 PST 2016


On Monday, 19 December 2016 at 10:03:34 UTC, Nicholas Wilson 
wrote:
> On Monday, 19 December 2016 at 09:24:38 UTC, Ali wrote:
>> On Monday, 19 December 2016 at 00:11:49 UTC, Nicholas Wilson 
>> wrote:
>>> [...]
>>
>> Ok so laziness stops as soon as sort is required on a range 
>> then?
>
> No. Because a lazy range is not random access, and therefore 
> does not meet sorts requirement.
>
>> Ahh, because in place algorithms?
>
> Yes
>
>>Are there any plans in
>> D to make is to that you can output copies to collections so 
>> that you could do something like filter.transpose.sort and 
>> just have it output a modified copy?
>
> None that I know.
>
>>
>> [...]
>
> Hmm, for the other problem you could do
>
> chain(only(T(dchar.max, uint.max)),range)
>
> and still use the seedless version.
>
>> Thanks for input so far!

The seedless version without the typeof(a)(b[0], b[1]) hack (with 
default inited T) seems to crap out with:

"Unable to deduce an acceptable seed type for __lambda2 with 
element type immutable(Tuple!(dchar, uint))."


BTW: Your chain fix does indeed make the T(dchar.init, uint.init) 
seeded fold work on a seedless version of fold :D. Only problem 
is the need for the typeof hack again though.


Some more info: The following alternate version of this program 
works great.

// Swapping the tuple elements that are produced by "group" 
around is necessary for reduce!max to work.
auto data = 
import("data_06.txt").split("\n").transposed.map!`a.array.sort().group.map!"tuple(a[1], a[0])".array`.array;
void main() {
     auto word = data.map!(reduce!max).array.map!"a[1]".array;
     word.writeln;
}

But this stops working as soon as you take the "word" variable 
outside the scope of main and in to global scope. If you make 
everything static immutable then you get:

"Error: static assert  "Unable to deduce an acceptable seed type 
for std.algorithm.comparison.max with element type 
immutable(Tuple!(uint, dchar))."


More information about the Digitalmars-d-learn mailing list