D with experimental features like tuple unpacking
bearophile
bearophileHUGS at lycos.com
Fri Mar 22 03:58:13 PDT 2013
Denis Shelomovskij:
> D already have undocumented[1] front tuple expansion in foreach
> over range:
> ---
> import std.algorithm;
> import std.stdio;
>
> void main() {
> enum s = "this is an example for huffman encoding"d;
> foreach (c, n; s.dup.sort().release().group())
> writefln("'%s' %s", c, n);
> }
I know, but it doesn't work in the case of my code I have shown,
with a SortedRange of 2-tuples:
void main() {
auto s = "this is an example for huffman encoding"d;
foreach (c, n; s.dup.sort().release.group.encode)
writefln("'%s' %s", c, n);
}
---------------------------
Jacob Carlborg:
>> auto (lof, loa) = heap.front;
>
> How is front suddenly returning two elements?
It is not returning two elements. In D you can't return two
elements. It is returning a 2-Tuple. You see the 2-tuples are
built by the map() and then used to create a heap:
auto heap = sf.map!(((c, f)) => tuple(f,
...)).array.heapify!q{...};
And this proposed syntax performs pattern matching on that
2-tuple, unpacking it into two variables (Hara has implemented
this already):
auto (lof, loa) = ...;
In Haskell, Scala, Python, F#, etc, the semantics and syntax are
similar.
Bye,
bearophile
More information about the Digitalmars-d
mailing list