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);
}
}
auto scaleAll(int[] xs, int m) {
import std.range: repeat, zip;
import std.algorithm: map;
return repeat(m).zip(xs).map!(unpack!((m, x) => m * x));
}
void main()
{
import std.algorithm: each;
import std.stdio: writeln;
[1, 2, 3, 4, 5].scaleAll(6).each!writeln;
}