Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ranges from associative arrays in D 2

I've just started implementing my first medium scale program in D 2.0 after reading Andrei's book The D Programming Language. One of the first problems I came to was using the std.algorithm library with a built-in associative array. For example:

#!/usr/bin/env rdmd

import std.stdio;
import std.algorithm;

void main()
{
   alias int[string] StringHashmap;

   StringHashmap map1;
   map1["one"] = 1;
   map1["two"] = 2;
   writefln("map1: %s", map1);

   StringHashmap map2;
   map2["two"] = 2;
   map2["three"] = 3;
   writefln("map2: %s", map2);

   auto inter = setIntersection(map1, map2);
}

It seemed a simple enough thing to me, expecting that iterating over the inter would produce the single "two" entry. However, I get this compiler error:

./test.d(20): Error: template std.algorithm.setIntersection(alias less = "a < b",Rs...) if (allSatisfy!(isInputRange,Rs)) does not match any function template declaration

./test.d(20): Error: template std.algorithm.setIntersection(alias less = "a < b",Rs...) if (allSatisfy!(isInputRange,Rs)) cannot deduce template function from argument types !()(int[string],int[string])

I can see that the built-in associative array doesn't seem to provide any version of the range to use with the std algorithms.

Am I missing something? Doing something wrong? If not, is this a glaring omission? Is there some reason why this is properly unavailable?

like image 258
aligature Avatar asked Aug 08 '10 19:08

aligature


People also ask

How do you find the data from an associative array?

Traversing the Associative Array: We can loop through the associative array in two ways. First by using for loop and secondly by using foreach. Example: Here array_keys() function is used to find indices names given to them and count() function is used to count number of indices in associative arrays.

Does In_array work for associative array?

in_array() function is utilized to determine if specific value exists in an array. It works fine for one dimensional numeric and associative arrays.

What is associative array with example?

For example, the following statement defines an associative array a with key signature [ int, string ] and stores the integer value 456 in a location named by the tuple [ 123, "hello" ]: a[123, "hello"] = 456; The type of each object contained in the array is also fixed for all elements in a given array.

Is an associative array a Hashmap?

A dictionary (also known as a map, hashmap or associative array) is a set of key/value pairs. OpenAPI lets you define dictionaries where the keys are strings. To define a dictionary, use type: object and use the additionalProperties keyword to specify the type of values in key/value pairs.


1 Answers

Use this:

auto inter = setIntersection(map1.keys, map2.keys);
like image 161
Andrei Alexandrescu Avatar answered Dec 31 '22 19:12

Andrei Alexandrescu