automate tuple creation
forkit
forkit at gmail.com
Wed Jan 19 23:22:17 UTC 2022
On Wednesday, 19 January 2022 at 22:35:58 UTC, Ali Çehreli wrote:
>
so I combined ideas from all responses:
// --
module test;
import std.stdio : writeln;
import std.range : iota, isForwardRange, hasSlicing, hasLength,
isInfinite, array;
import std.random : Random, unpredictableSeed, dice;
import std.algorithm : map;
@safe:
Random rnd;
static this()
{
rnd = Random(unpredictableSeed);
}
void main()
{
uint[][] mArrBool;
// e.g: create a matrix consisting of 5 tuples, with each
tuple containing 3 random bools (0 or 1)
createBoolMatrix(mArrBool,5, 2);
process(mArrBool);
}
void createBoolMatrix(ref uint[][] m, size_t numberOfTuples,
size_t numberOfBoolsInTuple)
{
m = iota(numberOfTuples)
.map!(i => iota(numberOfBoolsInTuple)
.map!(numberOfBoolsInTuple => cast(uint)
rnd.dice(0.6, 1.4))
.array).array;
}
void process(T)(const ref T t) if (isForwardRange!T &&
hasSlicing!T && hasLength!T && !isInfinite!T)
{
t.writeln;
}
//--
More information about the Digitalmars-d-learn
mailing list