Better idea for double list comprehension?

CJS Prometheus85 at hotmail.com
Sat Jan 18 22:57:36 PST 2014


Thanks!

>
>       auto app = appender(&unitlist);
>

Ah, I'd been wondering if there was something like this. But why 
does it need unitlist's address? (Assume & has the same meaning 
as in C and C++.)

>
> This one seems like it should be unitlist.filter!(x => x.any!(y 
> => s==y)).array();
>

Oh. Duh. But what is the .array() at the end doing? Allocating a 
new array for the results of the filter, since it's basically 
just a lazily computed range rather than actual storage for the 
results?

>>    foreach(s; squares){
>>        peers[s] = remove(chain(units[s]), s); \\line 41
>>    }
>
> This one I don't understand. chain(units[s]) ? That's the same 
> as units[s]. remove() returns a range, in this case string[][].

I'm just trying to match what the python code was doing. In that 
case the line

peers = dict((s, set(sum(units[s],[]))-set([s]))
              for s in squares)

was using sum to concatenate a bunch of lists together and then 
remove the element. But I miscounted the parentheses and didn't 
notice the '-' was after the concatenated lists were tured into a 
set. The following code appears to do what I want, but the string 
temporary and appender are a bit ugly. Any suggestions on getting 
rid of them?

    foreach(s; squares){
         string[] tmp;
         auto app2 = appender(&tmp);
         foreach(t; units[s]) {app2.put(t);}
         peers[s] = tmp.sort().uniq().filter!(a => a!=s).array();
     }




More information about the Digitalmars-d-learn mailing list