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 01:24:38 PST 2016
On Monday, 19 December 2016 at 00:11:49 UTC, Nicholas Wilson
wrote:
> On Sunday, 18 December 2016 at 22:26:50 UTC, Ali wrote:
>>
>> 1. The first line with the splitting, I need to use .array
>> three times. The last one I understand is because on "line 2"
>> I alias T as the type of the data, and if I don't call .array
>> then it's a MapResult type which has no opIndex. Yes?
>>
>> 2. On "line 1" I have to call .array.sort(). Why can't I just
>> call .sort() on the transposed rows?
>>
>
> Because sort requires a random access range.
Ok so laziness stops as soon as sort is required on a range then?
Ahh, because in place algorithms? 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?
> Unfortunately arrays have a builtin property sort that for some
> reason takes precedence of std.algorithm.sort when called
> without (). The compiler error is probably because .sort is a
> mutating operation and you're working with immutable data.
Ah ok. I think it's just that array.sort doesn't have CTFE
capabilities then: "Error: _adSort cannot be interpreted at
compile time, because it has no available source code"
> Try
> })(ImmutableOf!T.init);
> or use the seedless version of fold.
> may need to import std.traits(?)
Same error message :(
>
> does `data.map!fold!"a[1] > b[1] ? a : b".map!"a[0]".array;`
> work?
Actually I guess what's stopping this is my problem above maybe?
Since I can't jut return b because of that error. I can't use the
seedless version of fold either because Another version of the
problem requires the seed be T.init(dchar.max, uint.max).
Thanks for input so far!
More information about the Digitalmars-d-learn
mailing list