DIP32: Uniform tuple syntax

bearophile bearophileHUGS at lycos.com
Fri Mar 29 09:56:05 PDT 2013


kenji hara:

> If tup[0..1] makes closed tuple implicitly, you cannot make new 
> flattened
> tuple from other tuples.
>
> auto x = {1,"hi"};
> auto y = {[1,2], S(1)};
> auto tup1 = {x[], y[]};   // creates {1,"hi", [1,2], S(1)}
> auto tup2 = {x, y};   // creates {{1,"hi"}, {[1,2], S(1)}}
>
> Under your semantics, it is impossible.

I (and probably Timon) am just asking for another syntax to do 
that. Not to make it impossible.

A clean syntax to concatenate tuples:

auto tup3 = x ~ y; // creates {1,"hi", [1,2], S(1)}


In general I agree we need a syntax to apply the tuple items to a 
function. But I think the slice syntax is the wrong syntax for it.


In Python you use a star, this syntax can't be used in D:

>>> def foo(x, y): pass
...
>>>
>>> t = (1, 2)
>>> foo(*t)


In the Python itertools there is also a map that performs a star 
too:

>>> from itertools import starmap
>>> pow(2, 3)
8
>>> t = (2, 3)
>>> list(starmap(pow, [t]))
[8]


In Haskell there is more than one way to do that, like using 
uncurry that creates a new function:

Prelude> mod 10 4
2
Prelude> let t = (10, 4)
Prelude> let mod2 = uncurry mod
Prelude> mod2 t
2
Prelude> uncurry mod (10, 4)
2


Bye,
bearophile


More information about the Digitalmars-d mailing list