D foreach

Discussion in 'Python' started by bearophileHUGS@lycos.com, Nov 13, 2005.

  1. Guest

    The Digital Mars D compiler is a kind of "improved c++", it contains a
    "foreach" statement:
    http://www.digitalmars.com/d/statement.html#foreach

    Usage example:
    foreach(int i, inout int p; v1) p = i;

    Is equal to Python:
    for i in xrange(len(v)): v = i

    That is: v1 = range(len(v1))
    (Some people use something like this in Python to scan a list of lists,
    so p become a reference to a list, that can be modified in place, but
    it's not much explicit way of doing things.)


    Another example:
    foreach(int i, int p; v2) v1 = p;

    Is equal to Python:
    for i,p in enumerate(v2): v1 = p

    So the variable p contains (scans) the elements of the given iterable
    object, but if you assign p with a value, that value becomes copied
    inside the mutable iterable too. Those are little examples, but I think
    it can be quite useful in more complex code.

    Bye,
    bearophile
     
    , Nov 13, 2005
    #1
    1. Advertisements

  2. wrote:
    > The Digital Mars D compiler is a kind of "improved c++", it contains a
    > "foreach" statement:
    > http://www.digitalmars.com/d/statement.html#foreach
    >
    > Usage example:
    > foreach(int i, inout int p; v1) p = i;
    >
    > Is equal to Python:
    > for i in xrange(len(v)): v = i

    [...]
    > So the variable p contains (scans) the elements of the given iterable
    > object, but if you assign p with a value, that value becomes copied
    > inside the mutable iterable too. Those are little examples, but I think
    > it can be quite useful in more complex code.


    1. It would be difficult to implement. Python would require the concept
    of 'reference to variable', which has lots of repercussions for
    reference counting, garbage collection etc. Requiring iterators to
    return references would also break all existing iterators. It would also
    be required that the assignment operator is overloadable and this is
    another box of Pandora no one likes to open (well, no one except C++
    programmemrs).

    Or the compiler would have to detect 'assignment to iterator variable'
    and issue an 'update_current' to the iterator.

    2. It violates the Zen of Python 'Explicit is better than implicit'
    (although the definition of 'explict' varies wildly in the Python community)

    3. For specific collections like lists and dictionaries, you could write
    a wrapper so that it is possible to write

    for ref in wrapper (mycollection):
    print ref.value
    ref.value = newvalue

    Daniel
     
    Daniel Dittmar, Nov 14, 2005
    #2
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    Problem with foreach loop

    =?Utf-8?B?SnVzdGlu?=, Oct 22, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    437
    Hans Kesting
    Oct 22, 2004
  2. Islam Elkhayat

    loop througth Literials using foreach!!

    Islam Elkhayat, Feb 10, 2005, in forum: ASP .Net
    Replies:
    16
    Views:
    971
    Scott Allen
    Feb 12, 2005
  3. caldera
    Replies:
    1
    Views:
    4,585
    Scott Allen
    Feb 22, 2005
  4. ad
    Replies:
    2
    Views:
    15,374
    Peter Rilling
    Jun 6, 2005
  5. Alan Silver
    Replies:
    2
    Views:
    1,836
    Alan Silver
    Nov 7, 2005
  6. mike

    find index value of foreach

    mike, Nov 18, 2005, in forum: ASP .Net
    Replies:
    8
    Views:
    892
    Patrick.O.Ige
    Nov 18, 2005
  7. Robert Jones

    JSP Custom Tag and forEach variable

    Robert Jones, Jul 9, 2003, in forum: Java
    Replies:
    0
    Views:
    682
    Robert Jones
    Jul 9, 2003
  8. Robert Jones
    Replies:
    5
    Views:
    16,824
    Robert Jones
    Jul 10, 2003
Loading...