On Wednesday, 4 November 2015 at 06:20:30 UTC, Jakob Ovrum wrote:
On Tuesday, 3 November 2015 at 23:41:10 UTC, maik klein wrote:
[...]

import std.algorithm.iteration : sum;
import std.meta : allSatisfy, Filter;
import std.traits;
import std.typecons : tuple;
import std.range : only;

// These two are necessary since the ones in std.traits
// don't accept non-types
enum isIntegral(alias i) = std.traits.isIntegral!(typeof(i));
enum isFloatingPoint(alias f) = std.traits.isFloatingPoint!(typeof(f));

auto separateSum(T...)(T args)
        if(allSatisfy!(isNumeric, T))
{
return tuple(only(Filter!(isIntegral, args)).sum(), only(Filter!(isFloatingPoint, args)).sum());
}

pure nothrow @safe unittest
{
        assert(separateSum(2, 2.0) == tuple(2, 2.0));
        assert(separateSum(3, 2.0, 5, 1.0, 1.0) == tuple(8, 4.0));
}

Thanks, that is exactly what I wanted to achieve. What is the performance implication of 'only' in this context? Will it copy all arguments?

Reply via email to