Better idea for double list comprehension?

Stanislav Blinov stanislav.blinov at gmail.com
Sat Jan 18 23:37:44 PST 2014


On Sunday, 19 January 2014 at 06:57:37 UTC, CJS wrote:

> 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++.)

Look at the docs for Appender. It can either provide its own 
storage, or use an existing one. In the latter case you feed it a 
pointer to your array and it uses that.

>
>>
>> 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?

Yes. But in this case you'd have to allocate anyways, to fill the 
units[s] array.

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

Ah, I see.

>
> 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){
		peers[s] = units[s].join().sort().uniq().filter!(a => 
a!=s).array();
	}

That's std.array.join() there. However, I notice that the output 
(the order of elements) differs from the one on that page you 
linked earlier.


More information about the Digitalmars-d-learn mailing list