www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6383] New: Unpacking from dynamic array, lazy ranges

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6383

           Summary: Unpacking from dynamic array, lazy ranges
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



This is a spinoff of Issue 6365  (AutoTupleDeclaration).

Issue 6365 is about syntax sugar to unpack tuples, but unpacking dynamic arrays
or lazy ranges too is handy and commonly useful, because in D most arrays are
not fixed-sized, and many functions return a lazy range, like lazy splitting,
regular expression matches, etc.

The 'lengths don't match' exception is already present in D and I think it's
acceptable (when the lengths are statically known there is no need to test
lengths at run time, so this exception throwing is not present, so it's nothrow
code):

int[] array = [1, 2, 3];
int[2] a2 = array[];

-------------------

Some usage examples, assign from a dynamic array, in D2:

import std.string;
void main() {
    auto s = " foo bar ";
    auto ss = s.split();
    auto sa = ss[0];
    auto sb = ss[1];
}


In Python2.6:

 sa, sb = " foo bar ".split()
 sa
'foo'
 sb
'bar' Proposed: import std.string; void main() { auto s = " foo bar "; auto (sa, sb) = s.split(); } ------------------- From lazy range, D2: import std.algorithm, std.conv, std.string; void main() { string s = " 15 27"; // splitter doesn't work yet for this // (here it doesn't strip the leading space) //auto xy = map!(to!int)(splitter(s)); auto xy = map!(to!int)(split(s)); int x = xy[0]; int y = xy[1]; } In Python:
 x, y = map(int, " 15 27".split())
 x
15
 y
27
 from itertools import imap
 x, y = imap(int, " 15 27".split())
 x
15
 y
27 Proposed: import std.algorithm, std.conv, std.string; void main() { string s = " 15 27"; (int x, int y) = map!(to!int)(splitter(s)); } ------------------- From a lazy range, another example. See: http://rosettacode.org/wiki/Range_expansion#D D2 code: import std.stdio, std.regex, std.string, std.conv, std.range; int[] rangeExpand(string txt) { int[] result; foreach (r; std.string.split(txt, ",")) { auto m = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures); if (m[2].empty) result ~= to!int(m[1]); else result ~= array(iota(to!int(m[1]), to!int(m[3])+1)); } return result; } Python: import re def rangeexpand(txt): lst = [] for rng in txt.split(','): start,end = re.match('^(-?\d+)(?:-(-?\d+))?$', rng).groups() if end: lst.extend(xrange(int(start),int(end)+1)) else: lst.append(int(start)) return lst print(rangeexpand('-6,-3--1,3-5,7-11,14,15,17-20')) Proposed (just the important line), from dynamic array: auto (start,end) = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures); Or better, from the range of regex captures: auto (start,end) = match(r, r"^(-?\d+)(-?(-?\d+))?$").captures; Similar unpacking from Python list (that is a dynamic array) or from lazy iterable is quite common in scripting-style Python code. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 26 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6383




Sorry, this is not correct:

auto (start,end) = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures);
auto (start,end) = match(r, r"^(-?\d+)(-?(-?\d+))?$").captures;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 26 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6383


Dmitry Olshansky <dmitry.olsh gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmitry.olsh gmail.com



08:36:58 PDT ---

 Sorry, this is not correct:
 
 auto (start,end) = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures);
 auto (start,end) = match(r, r"^(-?\d+)(-?(-?\d+))?$").captures;
I'm puzzled, in this case captures contain way more then 2 strings: 0 - whole match 1 - (-?\d+) 2 - (-?(-?\d+))? 3 - (-?\d+) -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 30 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6383






 Sorry, this is not correct:
 
 auto (start,end) = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures);
 auto (start,end) = match(r, r"^(-?\d+)(-?(-?\d+))?$").captures;
I'm puzzled, in this case captures contain way more then 2 strings: 0 - whole match 1 - (-?\d+) 2 - (-?(-?\d+))? 3 - (-?\d+)
What are you puzzled about? I have already said that code I have written is not correct. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 30 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6383




08:43:44 PDT ---



 Sorry, this is not correct:
 
 auto (start,end) = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures);
 auto (start,end) = match(r, r"^(-?\d+)(-?(-?\d+))?$").captures;
I'm puzzled, in this case captures contain way more then 2 strings: 0 - whole match 1 - (-?\d+) 2 - (-?(-?\d+))? 3 - (-?\d+)
What are you puzzled about? I have already said that code I have written is not correct.
Ah, don't worry, I supposed you pasted a correction, sily me. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jul 30 2011
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6383


pompei2 gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pompei2 gmail.com





 Sorry, this is not correct:
 
 auto (start,end) = array(match(r, r"^(-?\d+)(-?(-?\d+))?$").captures);
 auto (start,end) = match(r, r"^(-?\d+)(-?(-?\d+))?$").captures;
I'm puzzled, in this case captures contain way more then 2 strings: 0 - whole match 1 - (-?\d+) 2 - (-?(-?\d+))? 3 - (-?\d+)
So why couldn't "start" and "end" unpack the whole match and (-?\d+) and ignore the last two? I totally want to see unpacking in D2. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 21 2011