xxxxxxxxxx
/** Tuple unpacker
*
* Usage: myTuple.unpack!((x, y) => f(x, y));
*
* Arguments are bound by order; names are irrelevant.
*
* Based on 'tupArg' by Dukc:
* https://forum.dlang.org/thread/rkfezigmrvuzkztxqqxy@forum.dlang.org
*/
template unpack(alias func)
{
import std.typecons: isTuple;
auto unpack(TupleType)(TupleType tup)
if (isTuple!TupleType)
{
return func(tup.expand);
}
}
void main() {
import std.experimental.all;
const int j = 2;
int i = 0;
const int[3] tmp = [1, 2, 3];
tmp[]
.zip(repeat(j))
.filter!(unpack!((x, j) => x == j))
.map!(unpack!((x, j) => x))
.each!((x) scope => i = x);
assert(i == 2);
}