You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In many situations a std.typecons.Tuple has uniform types (this means all its types are the same). Such tuples are produced by cartesianProduct(), by std.range.zip(), and so on.
Later I want to process such tuples in various ways, like iterating on them, using transversal() on them, and so on (in Python code this kind of processing is common because thanks to dynamic typing tuples are like lists).
If you have one of such uniform tuples like tup:
import std.typecons, std.typetuple;
void main() {
auto tup = tuple(0, 5, 11, 22);
static assert(NoDuplicates!(tup.Types).length == 1);
}
You can convert it to a dynamic array like this:
[tup[]]
So now it's usable as a range:
[tup[]].map!(x => x * 2).writeln;
But that syntax is a little noisy, and currently it causes a heap allocation.
This avoids the heap allocation but it's wrong if the tuple is not uniform:
(cast(tup.Types[0]*)&tup[0])[0 .. tup.length]
So I suggest to add asRange/AsRange to std.typecons:
// - - - - - - - - - - - - - - - -
import std.typecons, std.typetuple, std.stdio, std.algorithm,
std.range;
struct AsRange(T)
if (isTuple!T && NoDuplicates!(T.Types).length == 1) {
alias Tfield = T.Types[0];
T inputTuple;
size_t idx; // This can also be of type Tfield*.
static this() { // Useless? foreach (i, _; T.Types) static assert(T.tupleof[i].offsetof == Tfield.sizeof * i);}@property bool empty() { return idx >= T.length; }@property Tfield front() { return (cast(Tfield*)&inputTuple[0])[idx];}void popFront() { idx++; }// This can also be a Random Access Range.
}
// helper function.
AsRange!T asRange(T)(T tup)
if (isTuple!T && NoDuplicates!(T.Types).length == 1) {
return tup.AsRange!T;
}
void main() { // demo
auto a = [0];
auto b = [5];
auto c = [11];
auto d = [22];
Tuple!(int,int,int,int) tup = zip(a, b, c, d).front;
auto result = tup.asRange.map!(x => x * 2).array;
assert(result == [0, 10, 22, 44]);
}
// - - - - - - - - - - - - - - - -
In theory simpler code is enough, but because of a current DMD bug (a limit of alias this), this can't be used with UFCS:
// - - - - - - - - - - - - - - - -
import std.typecons, std.typetuple, std.stdio, std.algorithm,
std.range;
struct AsRange(T)
if (isTuple!T && NoDuplicates!(T.Types).length == 1) {
alias Tfield = T.Types[0];
T inputTuple;
static this() { // Useless? foreach (i, _; T.Types) static assert(T.tupleof[i].offsetof == Tfield.sizeof * i);}@property Tfield[] asArray() { return (cast(Tfield*)&inputTuple[0])[0 .. T.length];}alias asArray this;
}
AsRange!T asRange(T)(T tup)
if (isTuple!T && NoDuplicates!(T.Types).length == 1) {
return tup.AsRange!T;
}
void main() { // demo
auto a = [0];
auto b = [5];
auto c = [11];
auto d = [22];
Tuple!(int,int,int,int) tup = zip(a, b, c, d).front;
// Currently this can't be used:// Error: no property 'map' for type 'int[]'// tup.asRange.map!(x => x * 2).writeln;int[] result = tup.asRange;int[] result2 = result.map!(x => x * 2).array;assert(result2 == [0, 10, 22, 44]);
}
// - - - - - - - - - - - - - - - -
The text was updated successfully, but these errors were encountered:
> A simpler implementation:
Well, it's just:
---
tup.expand.only;
---https://run.dlang.io/is/vJaBtM
I'm not so sure whether this will be added to Tuple, but as Tuples will hopefully soon be part of the language, they should be first-class range citizens too.
bearophile_hugs reported this on 2013-04-03T19:44:52Z
Transfered from https://issues.dlang.org/show_bug.cgi?id=9871
CC List
Description
In many situations a std.typecons.Tuple has uniform types (this means all its types are the same). Such tuples are produced by cartesianProduct(), by std.range.zip(), and so on. Later I want to process such tuples in various ways, like iterating on them, using transversal() on them, and so on (in Python code this kind of processing is common because thanks to dynamic typing tuples are like lists). If you have one of such uniform tuples like tup: import std.typecons, std.typetuple; void main() { auto tup = tuple(0, 5, 11, 22); static assert(NoDuplicates!(tup.Types).length == 1); } You can convert it to a dynamic array like this: [tup[]] So now it's usable as a range: [tup[]].map!(x => x * 2).writeln; But that syntax is a little noisy, and currently it causes a heap allocation. This avoids the heap allocation but it's wrong if the tuple is not uniform: (cast(tup.Types[0]*)&tup[0])[0 .. tup.length] So I suggest to add asRange/AsRange to std.typecons: // - - - - - - - - - - - - - - - - import std.typecons, std.typetuple, std.stdio, std.algorithm, std.range; struct AsRange(T) if (isTuple!T && NoDuplicates!(T.Types).length == 1) { alias Tfield = T.Types[0]; T inputTuple; size_t idx; // This can also be of type Tfield*. static this() { // Useless? foreach (i, _; T.Types) static assert(T.tupleof[i].offsetof == Tfield.sizeof * i); } @property bool empty() { return idx >= T.length; } @property Tfield front() { return (cast(Tfield*)&inputTuple[0])[idx]; } void popFront() { idx++; } // This can also be a Random Access Range. } // helper function. AsRange!T asRange(T)(T tup) if (isTuple!T && NoDuplicates!(T.Types).length == 1) { return tup.AsRange!T; } void main() { // demo auto a = [0]; auto b = [5]; auto c = [11]; auto d = [22]; Tuple!(int,int,int,int) tup = zip(a, b, c, d).front; auto result = tup.asRange.map!(x => x * 2).array; assert(result == [0, 10, 22, 44]); } // - - - - - - - - - - - - - - - - In theory simpler code is enough, but because of a current DMD bug (a limit of alias this), this can't be used with UFCS: // - - - - - - - - - - - - - - - - import std.typecons, std.typetuple, std.stdio, std.algorithm, std.range; struct AsRange(T) if (isTuple!T && NoDuplicates!(T.Types).length == 1) { alias Tfield = T.Types[0]; T inputTuple; static this() { // Useless? foreach (i, _; T.Types) static assert(T.tupleof[i].offsetof == Tfield.sizeof * i); } @property Tfield[] asArray() { return (cast(Tfield*)&inputTuple[0])[0 .. T.length]; } alias asArray this; } AsRange!T asRange(T)(T tup) if (isTuple!T && NoDuplicates!(T.Types).length == 1) { return tup.AsRange!T; } void main() { // demo auto a = [0]; auto b = [5]; auto c = [11]; auto d = [22]; Tuple!(int,int,int,int) tup = zip(a, b, c, d).front; // Currently this can't be used: // Error: no property 'map' for type 'int[]' // tup.asRange.map!(x => x * 2).writeln; int[] result = tup.asRange; int[] result2 = result.map!(x => x * 2).array; assert(result2 == [0, 10, 22, 44]); } // - - - - - - - - - - - - - - - -The text was updated successfully, but these errors were encountered: