This module contains different functions acting on tuples (std.typecons.Tuple, not type-tuples): converting to and from arrays, inserting fields, rotating fields, inverting them, mapping/reducing/filtering them.

In many ways, it allows you to act on tuples as if they were polymorphic ranges.

The corresponding typetuples templates can be found in typetuple.d.

Boost License 1.0.

Philippe Sigaud

Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

R[0] first (R...)(Tuple!(R) tup);
R[1] second (R...)(Tuple!(R) tup);
R[2] third (R...)(Tuple!(R) tup);
Small helper functions to extract a field from a tuple. Mainly to make some expressions more readable.

Tuple!(TypeNuple!(T,n)) nuple (uint n, T)(T t);
n==0 -> tuple() n=1 -> tuple(t) n>1 -> tuple(t,t,...,t) n times

CommonType!(R)[R.length] tupleToStaticArray (R...)(Tuple!(R) t);
CommonType!(R)[] tupleToDynamicArray (R...)(Tuple!(R) t);
The first function transforms a tuple into a static array, the second into a dynamic array. For both functions, the resulting array's element type is the common type of the tuple's elements (it doesn't compile if there is no common type).

auto t = tuple(1, 2.0, 10000000000);
auto sa = tupleToStaticArray(t);
auto da = tupleToDynamicArray(t);
assert(is(typeof(sa) == double[3]));
assert(is(typeof(da) == double[]));

assert(sa == [1.0,2.0,10000000000.0]);
assert(da == [1.0,2.0,10000000000.0][]);

Tuple!(TypeNuple!(T,n)) staticArrayToTuple (size_t n, T)(T[n] arr);
Tuple!(TypeNuple!(T,n)) dynamicArrayToTuple (size_t n, T)(T[] arr);
The first function transforms a static array into a tuple, the second does the same for a dynamic array. For the dynamic case, you must give it the array's length at compile-time. There is no length-checking done, so be careful.

int[3] a = [1,2,3];
auto t1 = staticArrayToTuple(a);
assert(is(typeof(t1) == Tuple!(int,int,int)));
assert(t1 == tuple(1,2,3));

int[] b = [1,2,3];
auto t2 = dynamicArrayToTuple!3(b);
assert(is(typeof(t2) == Tuple!(int,int,int)));
assert(t2 == tuple(1,2,3));

Tuple!(TypeNuple!(ElementType!(R),upTo)) rangeToTuple (size_t upTo, R)(R range);
Same as the previous functions: takes an input range and a length upTo and converts the first upTo elements of the range into a tuple. The range must have at least upTo elements.

auto r = [0,1,2,3,4,5];
auto f = filter!isOdd(r);
auto t = rangeToTuple!3(f);
assert(t == tuple(1,3,5));
// With an infinite range.
auto c = cycle(r[0..3]); // 0,1,2, 0,1,2, 0,1,2,...
auto t2 = rangeToTuple!7(c);
assert(t2 == tuple(0,1,2,0,1,2,0));

R[0] front (R...)(Tuple!(R) tup);
Tuple!(R[1 .. __dollar]) tail (R...)(Tuple!(R) tup);
R[__dollar - 1] back (R...)(Tuple!(R) tup);
size_t length (R...)(Tuple!(R) tup);
bool empty (R...)(Tuple!(R) tup);
Small functions to imitate the range 'API' with tuples.

Tuple!(TypeTuple!(R[0 .. n],T,R[n .. __dollar])) insertAtTuple (size_t n, T, R...)(Tuple!(R) tup, T val);
Inserts a new component into a tuple, at position n. Position 0 is before all other components, position R.length is after all of them. n cannot be greater than R.length.

auto t = tuple(1,'a',3.14);
auto s0 = insertAtTuple!0(t, "qwer");
auto s1 = insertAtTuple!1(t, "qwer");
auto s2 = insertAtTuple!2(t, "qwer");
auto s3 = tupleInsertAt!3(t, "qwer");
assert(s0 == tuple("qwer",1,'a',3.14));
assert(s1 == tuple(1,"qwer",'a',3.14));
assert(s2 == tuple(1,'a',"qwer",3.14));
assert(s3 == tuple(1,'a',3.14, "qwer"));

ShredType!(array,R) shredTuple (alias array, R...)(Tuple!(R) tup);
Creates a new tuple by extracting the components of tup and reassembling them according to an indices array. [0,1,2] means 'take the first, second and third component' and so on. The indices can be in any order ([1,2,0]), can be repeated or omitted([0,2,2,0]) and the array can be as long as you wish.

See Also:
shred in dranges.range

auto t = tuple(1, 'a', 3.14);

auto s1 = shredTuple!([0,2])(t);
assert(s1 == tuple(1, 3.14));

auto s2 = shredTuple!([2,0,0,1,1])(t);
assert(s2 == tuple(3.14,1,1,'a','a'));

auto s3 = shredTuple!([0])(t);
assert(s3 == tuple(1));

Tuple!(RotateTypes!(n,R)) rotateTuple (int n, R...)(Tuple!(R) tup);
Takes a tuple and rotates its fields by n positions. If n>0, it rotates to the left (takes the first n fields and put them at the end). If n<0, it rotate to the right (takes the last n fields and put them at the beginning).

auto t = tuple(1, 'a', 3.14);

auto r1 = rotateTuple!1(t);
auto r5 = rotateTuple!5(t);
auto r0 = rotateTuple!0(t);
auto r_1 = rotateTuple!(-1)(t);
auto r_5 = rotateTuple!(-5)(t);

assert(r1 == tuple('a',3.14,1));
assert(r5 == tuple(3.14,1,'a'));
assert(r0 == t);
assert(r_1 == tuple(3.14,1,'a'));
assert(r_5 == tuple('a',3.14,1)); // equivalent to rotateTuple!(-2) and also to rotateTuple!1

assert(rotateTuple!1(tuple(1)) == tuple(1));

Tuple!(ReverseTypes!(R)) reverseTuple (R...)(Tuple!(R) tup);
Takes a tuple and reverse its fields.

auto t = tuple(1,'a',3.14);
auto r = reverseTuple(t);
assert(r == tuple(3.14,'a',1));

Tuple!(T1.Types,T2.Types) stitchTuples (T1, T2)(T1 tup1, T2 tup2);
Stitches (glues) two tuples together, creating a larger one.

auto t1 = tuple(1, 'a', 3.14);
auto t2 = tuple("abc", true);
auto t3 = stitchTuples(t1,t2);
assert(is(typeof(t3) == Tuple!(int,char,double,string,bool)));
assert(t3 == tuple(1, 'a', 3.14, "abc", true));

Tuple!(R,E) append (E, R...)(Tuple!(R) tup, E element);
a new tuple with tup's fields and a new field at the end, containing 'element'.

auto t = tuple(1, 'a', 3.14);
auto t2 = append(t, "abc");
assert(is(typeof(t2) == Tuple!(int, char, double, string)));
assert(t2 == tuple(1, 'a', 3.14, "abc"));

Tuple!(E,R) prepend (E, R...)(E element, Tuple!(R) tup);
a new tuple with element as first field and then tup's fields.

auto t = tuple(1, 'a', 3.14);
auto t2 = prepend("abc", t);
assert(is(typeof(t2) == Tuple!(string, int, char, double)));
assert(t2 == tuple("abc", 1, 'a', 3.14));

Tuple!(SwapTypes!(i1,i2,R)) swapTuple (size_t i1, size_t i2, R...)(Tuple!(R) tup);
Swaps the fields at index i1 and index i2 in a tuple.

auto t = tuple(1, 'a', 3.14, "abc");
auto ts1 = swapTuple!(1,3)(t);
assert(ts1 == tuple(1, "abc", 3.14, 'a'));
auto ts2 = swapTuple!(3,1)(t);
assert(ts2 == tuple(1, "abc", 3.14, 'a'));
auto ts3 = swapTuple!(1,1)(t);
assert(ts3 == t);

Tuple!(FlattenTuple!(T)) flattenTuple (T...)(T tup);
Flatten a tuple: any tuples inside the main tuple are opened and their content inserted, recursively.

auto t = tuple(1, tuple("a",3.14), tuple(tuple(0), 100));
auto f = flattenTuple(t);
assert(f == tuple(1, "a", 3.14, 0, 100));

typeof(fun(ifLeaf(T[0].init))) tupleReduce (alias fun, alias ifLeaf, T...)(T tup);

typeof(fun(CommonType!(Flatten!(T)).init)) tupleReduce0 (alias fun, alias ifLeaf, T...)(T tup);

Tuple!(StaticMap!(RT!(fun),T)) mapTuple (alias fun, T...)(Tuple!(T) tup);
Maps on a tuple, using a polymorphic function. Produces another tuple.

Tuple!(RetTypeTuples!(fun,T)) mapTuples (alias fun, T...)(T tuples);
Maps n tuples in parallel, using a polymorphing n-args function.

StaticScan!(RT2!(fun),T)[__dollar - 1] reduceTuple (alias fun, T...)(Tuple!(T) ts);
Folds a tuple using a polymorphic function.

Tuple!(StaticScan!(RT2!(fun),T)) scanTuple (alias fun, T...)(Tuple!(T) ts);
Scan on a tuple. See dranges.algorithm.scan.

Tuple!(StaticStride!(n,T)) strideTuple (size_t n, T...)(Tuple!(T) tup);
Returns a tuple whith only 1 element in n.

Tuple!(StaticIterate!(times,RT!(fun),S)) iterateTuple (size_t times, alias fun, S)(S seed);
Returns a tuple which has for elements the successive application (times times) of fun on the seed value.

Tuple!(StaticUnfold!(times,RTS2!(fun),State)) unfoldTuple (size_t times, alias fun, State...)(State state);

Tuple!(FilterTupleTypes!(pred,tup)) filterTuple (alias pred, alias tup)();
Filter a tuple on its values.

Tuple!(TakeWhileTupleTypes!(pred,tup)) takeWhileTuple (alias pred, alias tup)();

Tuple!(tup.Types[DropWhileTupleIndex!(pred,tup,0) .. __dollar]) dropWhileTuple (alias pred, alias tup)();

Tuple!(tup.Types[DropWhileTupleIndex!(pred,tup,0) .. __dollar],tup.Types[0 .. DropWhileTupleIndex!(pred,tup,0)]) rotateWhileTuple (alias pred, alias tup)();

bool contains (U, T...)(Tuple!(T) tup, U elem);

Page was generated with on Fri Nov 12 11:55:12 2010