Re: std.algorithm range violation

2014-05-28 Thread Wanderer via Digitalmars-d-learn

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?


Re: std.algorithm range violation

2014-05-28 Thread maarten van damme via Digitalmars-d-learn
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?



Re: std.algorithm range violation

2014-05-28 Thread bearophile via Digitalmars-d-learn

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


Re: std.algorithm range violation

2014-05-28 Thread Wanderer via Digitalmars-d-learn

Sorry about typo, I meant

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

above.


Re: std.algorithm range violation

2014-05-28 Thread Wanderer via Digitalmars-d-learn
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. :-)


Re: std.algorithm range violation

2014-05-28 Thread bearophile via Digitalmars-d-learn

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


Re: std.algorithm range violation

2014-05-28 Thread maarten van damme via Digitalmars-d-learn
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



Re: std.algorithm range violation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn

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.lengthy.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]);


Re: std.algorithm range violation

2014-05-28 Thread monarch_dodra via Digitalmars-d-learn

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.lengthy.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.