digitalmars.D.learn - compiler error when trying to get random key from AA
- mark (34/34) Jan 25 2020 I have this code:
- Basile B. (4/39) Jan 25 2020 rndGen is a range.
- mark (11/37) Jan 25 2020 I tried that. It doesn't solve the problem but does reduce the
- Basile B. (3/23) Jan 25 2020 yeah indeed. sorry, I didn't read and thought you needed the
- Basile B. (2/7) Jan 25 2020 no sorry, I didn't read and thought you need the index of rndGen.
- Basile B. (17/52) Jan 25 2020 So the problem is that byKey is not a ref parameter, so you can
- Basile B. (17/52) Jan 25 2020 So the problem is that byKey is not a ref parameter, so you can
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
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
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]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: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)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
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:yeah indeed. sorry, I didn't read and thought you needed the index of rndGen, e.g index % upperBound.On Saturday, 25 January 2020 at 08:35:18 UTC, mark wrote:[snip][...]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)[...]rndGen is a range. Use `auto word = aa.byKey.choice(rnd.front())` as index instead. Then `rndGen.popFront()` to advance.
Jan 25 2020
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:no sorry, I didn't read and thought you need the index of rndGen.[...]rndGen is a range. Use `auto word = aa.byKey.choice(rnd.front())` as index instead. Then `rndGen.popFront()` to advance.
Jan 25 2020
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
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
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: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 ...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:
Jan 25 2020
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