www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - compiler error when trying to get random key from AA

reply mark <mark qtrac.eu> writes:
I have this code:

import std.random;
import std.stdio;
void main()
{
     auto aa = ["one": 1, "two": 2, "three": 3];
     writeln(aa);
     auto rnd = rndGen;
     auto word = aa.byKey.choice(rnd);
     writeln(word);
}

And in the D playground it gives this error:

onlineapp.d(8): Error: template std.random.choice cannot deduce 
function from argument types !()(Result, 
MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 
2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 
4022730752u, 18LU, 1812433253u)), candidates are:
/dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599):       
  choice(Range, RandomGen = Random)(auto ref Range range, ref 
RandomGen urng)
   with Range = Result,
        RandomGen = MersenneTwisterEngine!(uint, 32LU, 624LU, 
397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 
15LU, 4022730752u, 18LU, 1812433253u)
   must satisfy the following constraint:
        isRandomAccessRange!Range
/dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609):       
  choice(Range)(auto ref Range range)

I am treating aa as a set and want to pick a random word from it.
What am I doing wrong?

I'm sorry I can't give a link to this code in the D playground 
but the URL in the web browser is just https://run.dlang.io/ and 
when I click Shorten to get a URL nothing seems to happen (using 
Firefox on Linux).
Jan 25 2020
next sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 I have this code:

 import std.random;
 import std.stdio;
 void main()
 {
     auto aa = ["one": 1, "two": 2, "three": 3];
     writeln(aa);
     auto rnd = rndGen;
     auto word = aa.byKey.choice(rnd);
     writeln(word);
 }

 And in the D playground it gives this error:

 onlineapp.d(8): Error: template std.random.choice cannot deduce 
 function from argument types !()(Result, 
 MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 
 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 
 4022730752u, 18LU, 1812433253u)), candidates are:
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599):
  choice(Range, RandomGen = Random)(auto ref Range range, ref 
 RandomGen urng)
   with Range = Result,
        RandomGen = MersenneTwisterEngine!(uint, 32LU, 624LU, 
 397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 
 15LU, 4022730752u, 18LU, 1812433253u)
   must satisfy the following constraint:
        isRandomAccessRange!Range
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609):
  choice(Range)(auto ref Range range)

 I am treating aa as a set and want to pick a random word from 
 it.
 What am I doing wrong?

 I'm sorry I can't give a link to this code in the D playground 
 but the URL in the web browser is just https://run.dlang.io/ 
 and when I click Shorten to get a URL nothing seems to happen 
 (using Firefox on Linux).
rndGen is a range. Use `auto word = aa.byKey.choice(rnd.front())` as index instead. Then `rndGen.popFront()` to advance.
Jan 25 2020
next sibling parent reply mark <mark qtrac.eu> writes:
On Saturday, 25 January 2020 at 08:59:23 UTC, Basile B. wrote:
 On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 I have this code:

 import std.random;
 import std.stdio;
 void main()
 {
     auto aa = ["one": 1, "two": 2, "three": 3];
     writeln(aa);
     auto rnd = rndGen;
     auto word = aa.byKey.choice(rnd);
     writeln(word);
 }

 And in the D playground it gives this error:
[snip]
 I am treating aa as a set and want to pick a random word from 
 it.
 What am I doing wrong?

 I'm sorry I can't give a link to this code in the D playground 
 but the URL in the web browser is just https://run.dlang.io/ 
 and when I click Shorten to get a URL nothing seems to happen 
 (using Firefox on Linux).
rndGen is a range. Use `auto word = aa.byKey.choice(rnd.front())` as index instead. Then `rndGen.popFront()` to advance.
I tried that. It doesn't solve the problem but does reduce the size of the error output to: onlineapp.d(9): Error: template std.random.choice cannot deduce function from argument types !()(Result, uint), candidates are: /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599): choice(Range, RandomGen = Random)(auto ref Range range, ref RandomGen urng) /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609): choice(Range)(auto ref Range range)
Jan 25 2020
parent Basile B. <b2.temp gmx.com> writes:
On Saturday, 25 January 2020 at 09:06:53 UTC, mark wrote:
 On Saturday, 25 January 2020 at 08:59:23 UTC, Basile B. wrote:
 On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 [...]
[snip]
 [...]
rndGen is a range. Use `auto word = aa.byKey.choice(rnd.front())` as index instead. Then `rndGen.popFront()` to advance.
I tried that. It doesn't solve the problem but does reduce the size of the error output to: onlineapp.d(9): Error: template std.random.choice cannot deduce function from argument types !()(Result, uint), candidates are: /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599): choice(Range, RandomGen = Random)(auto ref Range range, ref RandomGen urng) /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609): choice(Range)(auto ref Range range)
yeah indeed. sorry, I didn't read and thought you needed the index of rndGen, e.g index % upperBound.
Jan 25 2020
prev sibling parent Basile B. <b2.temp gmx.com> writes:
On Saturday, 25 January 2020 at 08:59:23 UTC, Basile B. wrote:
 On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 [...]
rndGen is a range. Use `auto word = aa.byKey.choice(rnd.front())` as index instead. Then `rndGen.popFront()` to advance.
no sorry, I didn't read and thought you need the index of rndGen.
Jan 25 2020
prev sibling next sibling parent Basile B. <b2.temp gmx.com> writes:
On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 I have this code:

 import std.random;
 import std.stdio;
 void main()
 {
     auto aa = ["one": 1, "two": 2, "three": 3];
     writeln(aa);
     auto rnd = rndGen;
     auto word = aa.byKey.choice(rnd);
     writeln(word);
 }

 And in the D playground it gives this error:

 onlineapp.d(8): Error: template std.random.choice cannot deduce 
 function from argument types !()(Result, 
 MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 
 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 
 4022730752u, 18LU, 1812433253u)), candidates are:
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599):
  choice(Range, RandomGen = Random)(auto ref Range range, ref 
 RandomGen urng)
   with Range = Result,
        RandomGen = MersenneTwisterEngine!(uint, 32LU, 624LU, 
 397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 
 15LU, 4022730752u, 18LU, 1812433253u)
   must satisfy the following constraint:
        isRandomAccessRange!Range
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609):
  choice(Range)(auto ref Range range)

 I am treating aa as a set and want to pick a random word from 
 it.
 What am I doing wrong?

 I'm sorry I can't give a link to this code in the D playground 
 but the URL in the web browser is just https://run.dlang.io/ 
 and when I click Shorten to get a URL nothing seems to happen 
 (using Firefox on Linux).
So the problem is that byKey is not a ref parameter, so you can use array on it: --- import std.random; import std.stdio; import std.array; void main() { auto aa = ["one": 1, "two": 2, "three": 3]; writeln(aa); Random rnd; auto word = choice(aa.byKey.array, rnd); writeln(word); } --- sorry for the previous noise.
Jan 25 2020
prev sibling parent reply Basile B. <b2.temp gmx.com> writes:
On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 I have this code:

 import std.random;
 import std.stdio;
 void main()
 {
     auto aa = ["one": 1, "two": 2, "three": 3];
     writeln(aa);
     auto rnd = rndGen;
     auto word = aa.byKey.choice(rnd);
     writeln(word);
 }

 And in the D playground it gives this error:

 onlineapp.d(8): Error: template std.random.choice cannot deduce 
 function from argument types !()(Result, 
 MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 
 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 
 4022730752u, 18LU, 1812433253u)), candidates are:
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599):
  choice(Range, RandomGen = Random)(auto ref Range range, ref 
 RandomGen urng)
   with Range = Result,
        RandomGen = MersenneTwisterEngine!(uint, 32LU, 624LU, 
 397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 
 15LU, 4022730752u, 18LU, 1812433253u)
   must satisfy the following constraint:
        isRandomAccessRange!Range
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609):
  choice(Range)(auto ref Range range)

 I am treating aa as a set and want to pick a random word from 
 it.
 What am I doing wrong?

 I'm sorry I can't give a link to this code in the D playground 
 but the URL in the web browser is just https://run.dlang.io/ 
 and when I click Shorten to get a URL nothing seems to happen 
 (using Firefox on Linux).
So the problem is that byKey is not a ref parameter, so you can use array on it: --- import std.random; import std.stdio; import std.array; void main() { auto aa = ["one": 1, "two": 2, "three": 3]; writeln(aa); Random rnd; auto word = choice(aa.byKey.array, rnd); writeln(word); } --- sorry for the previous noise.
Jan 25 2020
parent reply Basile B. <b2.temp gmx.com> writes:
On Saturday, 25 January 2020 at 09:18:01 UTC, Basile B. wrote:
 On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:
 I have this code:

 import std.random;
 import std.stdio;
 void main()
 {
     auto aa = ["one": 1, "two": 2, "three": 3];
     writeln(aa);
     auto rnd = rndGen;
     auto word = aa.byKey.choice(rnd);
     writeln(word);
 }

 And in the D playground it gives this error:

 onlineapp.d(8): Error: template std.random.choice cannot 
 deduce function from argument types !()(Result, 
 MersenneTwisterEngine!(uint, 32LU, 624LU, 397LU, 31LU, 
 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 15LU, 
 4022730752u, 18LU, 1812433253u)), candidates are:
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2599):
  choice(Range, RandomGen = Random)(auto ref Range range, ref 
 RandomGen urng)
   with Range = Result,
        RandomGen = MersenneTwisterEngine!(uint, 32LU, 624LU, 
 397LU, 31LU, 2567483615u, 11LU, 4294967295u, 7LU, 2636928640u, 
 15LU, 4022730752u, 18LU, 1812433253u)
   must satisfy the following constraint:
        isRandomAccessRange!Range
 /dlang/dmd/linux/bin64/../../src/phobos/std/random.d(2609):
  choice(Range)(auto ref Range range)

 I am treating aa as a set and want to pick a random word from 
 it.
 What am I doing wrong?

 I'm sorry I can't give a link to this code in the D playground 
 but the URL in the web browser is just https://run.dlang.io/ 
 and when I click Shorten to get a URL nothing seems to happen 
 (using Firefox on Linux).
So the problem is that byKey is not a ref parameter, so you can use array on it:
Well the explanation for your error is rather that byKey did not verify isRandomAccesRange, i.e indexable by an index so .array on it solve this. pfff finally ...
Jan 25 2020
parent mark <mark qtrac.eu> writes:
In the end I used this line since I'm not fussy about the rnd for 
this:

auto word = compatibles.byKey.array.choice;

Thank you!
Jan 25 2020