www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm range violation

reply maarten van damme via Digitalmars-d-learn writes:
an anyone explain me what I'm doing wrong here :

[code]
dstring[][dstring] providor_symbol_map;
...

writeln(providor_symbol_map.keys.sort!((x,y)=>providor_symbol_map[x].length>=providor_symbol_map[y].length));
[/code]

output:
core.exception.RangeError std.algorithm(9429): Range violation
May 28 2014
next sibling parent reply "Wanderer" <no-reply no-reply.org> writes:
On Wednesday, 28 May 2014 at 10:10:41 UTC, maarten van damme via
Digitalmars-d-learn wrote:
 an anyone explain me what I'm doing wrong here :

 [code]
 dstring[][dstring] providor_symbol_map;
 ...

 writeln(providor_symbol_map.keys.sort!((x,y)=>providor_symbol_map[x].length>=providor_symbol_map[y].length));
 [/code]

 output:
 core.exception.RangeError std.algorithm(9429): Range violation
"dstring[][dstring]" declaration looks a bit obscure to me... what do you intent with it, "array of maps" or "map of arrays"? Also, it looks unnatural that inside sorting lambda, you refer outside from it (back to providor_symbol_map variable). Ideally, you should only use variables x and y. Does D support sorting by map entries instead of by keys only or by values only?
May 28 2014
parent reply maarten van damme via Digitalmars-d-learn writes:
I'm trying to analyze the usage of certain words in a large number of spam
emails, and I want for every interesting word a list of 'providors', that
mentioned that word. with associative arrays I hoped to get it by using
array["interestingworde].

And I have to refer outside from sort(x,y) as associative arrays have no
order, I'll always have to sort a key-array according to values obtained
through the associative array.
It's supposed to be a delegate anyway, otherwise the least it could do is
throw a compilation error.


2014-05-28 12:53 GMT+02:00 Wanderer via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com>:

 On Wednesday, 28 May 2014 at 10:10:41 UTC, maarten van damme via

 Digitalmars-d-learn wrote:

 an anyone explain me what I'm doing wrong here :

 [code]
 dstring[][dstring] providor_symbol_map;
 ...

 writeln(providor_symbol_map.keys.sort!((x,y)=>providor_
 symbol_map[x].length>=providor_symbol_map[y].length));
 [/code]

 output:
 core.exception.RangeError std.algorithm(9429): Range violation
"dstring[][dstring]" declaration looks a bit obscure to me... what do you intent with it, "array of maps" or "map of arrays"? Also, it looks unnatural that inside sorting lambda, you refer outside from it (back to providor_symbol_map variable). Ideally, you should only use variables x and y. Does D support sorting by map entries instead of by keys only or by values only?
May 28 2014
parent reply "Wanderer" <no-reply no-reply.org> writes:
Aha, so you want to maintain "spam word" -> "set of senders" 
relationship, so it's actually "map of sets" and your declaration 
is correct. You only need to sort map's entries (key and value 
pairs together).

If D supports that, it should be something like 
providor_symbol_map.sort!((x,y)=>{x.value.length=>y.value.length}), 
but I'm not sure it would work...

But I'm just learning D (and very much like it so far!), 
hopefully someone more skilled will reply as well. :-)
May 28 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Wanderer:

 providor_symbol_map.sort!((x,y)=>{x.value.length=>y.value.length}),
This lambda doesn't return a boolean. Also, add spaces around operators and after commas. Bye, bearophile
May 28 2014
prev sibling parent reply "Wanderer" <no-reply no-reply.org> writes:
Sorry about typo, I meant

providor_symbol_map.sort!((x,y)=>{x.value.length>y.value.length})

above.
May 28 2014
parent reply "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 28 May 2014 at 11:40:05 UTC, Wanderer wrote:
 Sorry about typo, I meant

 providor_symbol_map.sort!((x,y)=>{x.value.length>y.value.length})

 above.
providor_symbol_map is an Associative Array, so you can't sort that. *Usually*, you want to do what the OP did, which is to get the keys, and sort them, but leave the AA unchanged. EG: Val[Key] myAA; Key[] mySortedKeys = myAA.keys.sort!((x, y)=> compare(myAA[x], myAA[y]))() //Print values in incremented order: foreach(key; mySortedKeys) writefln("%s: %s", key, myAA[key]);
May 28 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Wednesday, 28 May 2014 at 17:39:15 UTC, monarch_dodra wrote:
 On Wednesday, 28 May 2014 at 11:40:05 UTC, Wanderer wrote:
 Sorry about typo, I meant

 providor_symbol_map.sort!((x,y)=>{x.value.length>y.value.length})

 above.
providor_symbol_map is an Associative Array, so you can't sort that. *Usually*, you want to do what the OP did, which is to get the keys, and sort them, but leave the AA unchanged. EG: Val[Key] myAA; Key[] mySortedKeys = myAA.keys.sort!((x, y)=> compare(myAA[x], myAA[y]))() //Print values in incremented order: foreach(key; mySortedKeys) writefln("%s: %s", key, myAA[key]);
I case this was not clear "compare" is an function you should replace with your own. It should simply define strict ordering of x and y. "<" is one such function.
May 28 2014
prev sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
maarten van damme:

 writeln(providor_symbol_map.keys.sort!((x,y)=>providor_symbol_map[x].length>=providor_symbol_map[y].length));
 [/code]
Try: ((x, y) => providor_symbol_map[x].length > providor_symbol_map[y].length) Bye, bearophile
May 28 2014
parent maarten van damme via Digitalmars-d-learn writes:
wow.
senpai, teach me what I did wrong...


2014-05-28 13:21 GMT+02:00 bearophile via Digitalmars-d-learn <
digitalmars-d-learn puremagic.com>:

 maarten van damme:


  writeln(providor_symbol_map.keys.sort!((x,y)=>providor_
 symbol_map[x].length>=providor_symbol_map[y].length));
 [/code]
Try: ((x, y) => providor_symbol_map[x].length > providor_symbol_map[y].length) Bye, bearophile
May 28 2014