digitalmars.D.learn - std.algorithm range violation
- maarten van damme via Digitalmars-d-learn (8/8) May 28 2014 an anyone explain me what I'm doing wrong here :
- Wanderer (8/16) May 28 2014 "dstring[][dstring]" declaration looks a bit obscure to me...
- maarten van damme via Digitalmars-d-learn (11/32) May 28 2014 I'm trying to analyze the usage of certain words in a large number of sp...
- Wanderer (9/9) May 28 2014 Aha, so you want to maintain "spam word" -> "set of senders"
- bearophile (5/6) May 28 2014 This lambda doesn't return a boolean. Also, add spaces around
- Wanderer (3/3) May 28 2014 Sorry about typo, I meant
- monarch_dodra (10/13) May 28 2014 providor_symbol_map is an Associative Array, so you can't sort
- monarch_dodra (4/19) May 28 2014 I case this was not clear "compare" is an function you should
- bearophile (6/8) May 28 2014 Try:
- maarten van damme via Digitalmars-d-learn (4/13) May 28 2014 wow.
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
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
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
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
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
Sorry about typo, I meant providor_symbol_map.sort!((x,y)=>{x.value.length>y.value.length}) above.
May 28 2014
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
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: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.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
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
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