Thread overview
[Issue 5935] New: Non-tuple iteration with std.range.zip
May 06, 2011
Kenji Hara
May 06, 2011
kennytm@gmail.com
May 06, 2011
Kenji Hara
May 06, 2011
kennytm@gmail.com
May 09, 2011
Lutger
Aug 09, 2012
Denis Shelomovskij
May 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5935

           Summary: Non-tuple iteration with std.range.zip
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: patch
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody@puremagic.com
        ReportedBy: k.hara.pg@gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg@gmail.com> 2011-05-06 07:37:46 PDT ---
Zip sequence should provide non-tuple version foreach like this:
----
foreach (i, c; zip(sequence!"n"(), "str"))
{
         if (i==0) assert(c == 's');
    else if (i==1) assert(c == 't');
    else if (i==2) assert(c == 'r');
    else assert(0);
}
----

Patch:

 std/range.d |   15 +++++++++++++++
 1 files changed, 15 insertions(+), 0 deletions(-)

diff --git a/std/range.d b/std/range.d
index 7e7916c..0a24500 100644
--- a/std/range.d
+++ b/std/range.d
@@ -3209,6 +3209,21 @@ if(Ranges.length && allSatisfy!(isInputRange,
staticMap!(Unqual, Ranges)))
             }
         }
     }
+
+/**
+   Iterate zip elements with directry named heads of ranges.
+ */
+    int opApply(int delegate(ref ElementType.Types) dg)
+    {
+        auto r = this;
+        for (; !r.empty; r.popFront())
+        {
+            auto e = r.front;
+            if (auto result = dg(e.field))
+                return result;
+        }
+        return 0;
+    }
 }

 /// Ditto

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5935


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com


--- Comment #1 from kennytm@gmail.com 2011-05-06 09:24:10 PDT ---
What about std.range.lockstep?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5935



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2011-05-06 11:09:07 PDT ---
(In reply to comment #1)
> What about std.range.lockstep?

Oh, I have been overlooked. Thanks.
But..., why can't we merge Zip and Lockstep?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5935


Andrei Alexandrescu <andrei@metalanguage.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@metalanguage.com


--- Comment #3 from Andrei Alexandrescu <andrei@metalanguage.com> 2011-05-06 11:18:36 PDT ---
I'm worried about this development. Before long we could get to the point where a lot of ranges get bloated to support two iteration methods because opApply has capabilities that ranges don't.

To prevent that we should improve range-based iteration to provide good support for foreach, and leave opApply to entities that need internal iteration.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5935



--- Comment #4 from kennytm@gmail.com 2011-05-06 11:26:58 PDT ---
(In reply to comment #3)
> I'm worried about this development. Before long we could get to the point where a lot of ranges get bloated to support two iteration methods because opApply has capabilities that ranges don't.
> 
> To prevent that we should improve range-based iteration to provide good support for foreach, and leave opApply to entities that need internal iteration.

Implement (a subset of) issue 4579?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 09, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5935


Lutger <lutger.blijdestijn@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lutger.blijdestijn@gmail.co
                   |                            |m


--- Comment #5 from Lutger <lutger.blijdestijn@gmail.com> 2011-05-09 10:18:44 PDT ---
If no language changes for range iteration and tuple unpacking will be made, perhaps it's worth to consider a generic wrapper for this purpose as a compromise?

I couldn't think of a good name:

auto iterUntupled(R)(R input)
    if ( isInputRange!R &&
         is( typeof( { auto x = input.front.tupleof; }() ) ) )
{

    static struct IterUntupled(R)
    {
        this(R input)
        {
            _input = input;
        }

        int opApply( int delegate( ref typeof(_input.front.tupleof) ) dg )
        {
            while( !_input.empty)
            {
                auto front = _input.front;
                if ( auto result = dg(front.tupleof) )
                    return result;
                _input.popFront();
            }
            return 0;
        }

        R _input;
    }
    return IterUntupled!R(input);
}

This will also unpack arbitrary structs and classes, is that too loose?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 09, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5935


Denis Shelomovskij <verylonglogin.reg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |verylonglogin.reg@gmail.com
         Resolution|                            |FIXED


--- Comment #6 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2012-08-09 13:35:50 MSD ---
"front tuple expansion" by Kenji Hara fixed this issue.
But it still isn't documented.

See Issue 7361 - No documentation for front tuple expansion in foreach over range

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