Thread overview
Is there a way to map associative arrays
Aug 01, 2014
Freddy
Aug 01, 2014
bearophile
Aug 01, 2014
Freddy
Aug 02, 2014
Brian Schott
August 01, 2014
#!/usr/bin/rdmd
import std.algorithm;
import std.stdio;
uint[uint] test;

void main(){
	test=[0:2 ,1:3 ,2:4];
	writeln(test.map!(a=>a-2));
}

$ ./test.d
./test.d(8): Error: template std.algorithm.map cannot deduce
function from argument types !((a) => a - 2)(uint[uint]),
candidates are:
/usr/include/dmd/phobos/std/algorithm.d(375):
std.algorithm.map(fun...) if (fun.length >= 1)
Failed: ["dmd", "-v", "-o-", "./test.d", "-I."]
August 01, 2014
Freddy:

> uint[uint] test;
>
> void main(){
> 	test=[0:2 ,1:3 ,2:4];
> 	writeln(test.map!(a=>a-2));
> }

If you need keys or values you have .keys .values, .byKey, .byValue (the first two are eager). If you need both you are out of luck, and if you want to write safe code it's better to use a foreach loop. If you want to live dangerously you can use a test.byKey.zip(test.byValue).

Take a look in Rosettacode, there are examples for all of them.

Bye,
bearophile
August 01, 2014
On Friday, 1 August 2014 at 23:22:06 UTC, bearophile wrote:
> Freddy:
>
>> uint[uint] test;
>>
>> void main(){
>> 	test=[0:2 ,1:3 ,2:4];
>> 	writeln(test.map!(a=>a-2));
>> }
>
> If you need keys or values you have .keys .values, .byKey, .byValue (the first two are eager). If you need both you are out of luck, and if you want to write safe code it's better to use a foreach loop. If you want to live dangerously you can use a test.byKey.zip(test.byValue).
>
> Take a look in Rosettacode, there are examples for all of them.
>
> Bye,
> bearophile
Sorry, i wasn't very clear.
I wanted to know if there a way to lazily change the look up
operation
eg:
assert(test[1]==1);
assert(test[2]==2);
August 02, 2014
On Friday, 1 August 2014 at 23:33:22 UTC, Freddy wrote:
> On Friday, 1 August 2014 at 23:22:06 UTC, bearophile wrote:
>> Freddy:
>>
>>> uint[uint] test;
>>>
>>> void main(){
>>> 	test=[0:2 ,1:3 ,2:4];
>>> 	writeln(test.map!(a=>a-2));
>>> }
>>
>> If you need keys or values you have .keys .values, .byKey, .byValue (the first two are eager). If you need both you are out of luck, and if you want to write safe code it's better to use a foreach loop. If you want to live dangerously you can use a test.byKey.zip(test.byValue).
>>
>> Take a look in Rosettacode, there are examples for all of them.
>>
>> Bye,
>> bearophile
> Sorry, i wasn't very clear.
> I wanted to know if there a way to lazily change the look up
> operation
> eg:
> assert(test[1]==1);
> assert(test[2]==2);

Give your struct/class a method called "opIndex".

http://dlang.org/operatoroverloading.html#Array