[Issue 4591] Concat of std.typecons.Tuples

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue May 14 09:49:41 PDT 2013


http://d.puremagic.com/issues/show_bug.cgi?id=4591


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         AssignedTo|andrei at erdani.com           |nobody at puremagic.com


--- Comment #2 from bearophile_hugs at eml.cc 2013-05-14 09:49:37 PDT ---
I suggest to add the support for Tuple concatenation and join:

In Python 2.6:

>>> t1 = (1, 2)
>>> t1 + t1
(1, 2, 1, 2)
>>> t1 + (3,)
(1, 2, 3)


Proposed D syntax:

void main() {
    import std.typecons;
    auto t1 = tuple(1, 2);
    auto t2 = t1 ~ t1;
    auto t3a = t1 ~ 3;
    auto t3b = t1 ~ tuple(3);
}


An use case, this computes the frequency of the first digit (Benford's Law):


import std.stdio, std.range, std.math, std.conv, std.bigint,
       std.algorithm;

auto benford(R)(R data) {
    auto heads = data.filter!q{a != 0}.map!q{ a.text[0] - '1' }.array;
    immutable double k = heads.length;
    return iota(1, 10)
           .zip(heads.sort().group.map!(p => p[1] / k))
           .map!q{ [a[]] ~ log10(1.0 + 1.0 / a[0]) };
}

void main() {
    auto fibs = recurrence!q{a[n - 1] + a[n - 2]}(1.BigInt, 1.BigInt);

    writefln("%9s %9s %9s", "Actual", "Expected", "Deviation");
    foreach (p; fibs.take(1000).benford)
        writefln("%1.0f: %5.2f%% | %5.2f%% | %5.4f%%",
                 p[0], p[1] * 100, p[2] * 100, abs(p[2] - p[1]) * 100);
}


Currently the benford() function returns a range of double[]:

.map!q{ [a[]] ~ log10(1.0 + 1.0 / a[0]) };

If I want to return a range of 3-tuples:

.map!q{ tuple(a[], log10(1.0 + 1.0 / a[0])) };

With the proposed syntax the code becomes:

.map!q{ a ~ log10(1.0 + 1.0 / a[0]) };

Or:

.map!q{ a ~ tuple(log10(1.0 + 1.0 / a[0])) };

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list