On 20.02.2019 07:06, Erich Steinböck wrote:
>
>     number a do with  over a table where the indices are not numbers, or over 
> a set or bag
>
>  
> Use
> do with index i item j over c~allItems

This is *not* the same collection as "c", but an array of all of the items in 
the "c" MapCollection!
The important information of which index is associated with which item gets 
lost.

The "do with...over" is meant to iterate over a supplier which is especially 
important for
non-orderable collections. 

In the case of Map/SetCollections the index object is as important as the item 
object, such that it
cannot be forgone. (The supplier is the only means for Map/SetCollections to 
get all
index-item-pairs, cf. e.g. for .Relation collections.)

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

One may be inclined to think that "do with...over" is fine for Orderable 
collections as array
collections demonstrate:

    a=("a","z","b")
    say a~class":"
    do with index i item o over a
        say "#" i":" pp(o)
    end
    say

    l=.list~of("a","y","b")
    say l~class":"
    do with index i item o over l
        say "#" i":" pp(o)
    end
    say

    ::routine pp
      return "["arg(1)"]"
    ---------------------------------------------------------------------------

    G:\test\oorexx\201902doover>test_do_over.rex
    The Array class:
    # 1: [a]
    # 2: [z]
    # 3: [b]

    The List class:
    # 0: [a]
    # 1: [y]
    # 2: [b]

However the index value starting out with 0 in the List collection case! The 
expected output in the
case of the List class probably would have been:

    The List class:
    # 1: [a]
    # 2: [y]
    # 3: [b]

However, as one can see the List does not have a numeric index that start with 
1, like it is the
case with Array collections.

In the case of an Orderable collection only the "allitems" array will allow for 
using the index for
numbering, starting with one (because an array always starts with 1):

    l=.list~of("a","y","b")
    say l~class":"
    do with index i item o over l~allItems
        say "#" i":" pp(o)
    end
    say

    ::routine pp
      return "["arg(1)"]"
    ---------------------------------------------------------------------------

    G:\test\oorexx\201902doover>test_do_over.rex
    The List class:
    # 1: [a]
    # 2: [y]
    # 3: [b]

=====================================

Using allItems or allIndexes would not help in the case of Map/SetCollections, 
where the index
object is as important as the item object.

Example with a Table collection:

    t=.table~of(("adam","male"), ("berta","female"), ("caesar","male") )
    say t~class":"

    do with index i item o over t
        say "#" i":" pp(o)
    end
    say

    say "--- now with t~allItems:"
    do with index i item o over t~allItems
        say "#" i":" pp(o)
    end
    say

    say "--- now with t~allIndexes:"
    do with index i item o over t~allIndexes
        say "#" i":" pp(o)
    end
    say

    say "--- the desired output:"
    i=1
    do with index idx item o over t
        say "#" i":" pp(idx) "->" pp(o)
        i=i+1
    end
    say

    ::routine pp
      return "["arg(1)"]"

Yielding:

    The Table class:
    # berta: [female]
    # caesar: [male]
    # adam: [male]

    --- now with t~allItems:
    # 1: [female]
    # 2: [male]
    # 3: [male]

    --- now with t~allIndexes:
    # 1: [berta]
    # 2: [caesar]
    # 3: [adam]

    --- the desired output:
    # 1: [berta] -> [female]
    # 2: [caesar] -> [male]
    # 3: [adam] -> [male]

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

Example of a relation (index is a human, item a child of that human; a human 
may have more than one
child):

    r=.relation~of(("adam","henry"), ("adam","julia"), ("berta","julia"), 
("caesar","denise"), ("caesar","mark") )
    say r~class":"
    do with index i item o over r
        say "#" i":" pp(o)
    end
    say

    say "--- now with t~allItems:"
    do with index i item o over r~allItems
        say "#" i":" pp(o)
    end
    say

    say "--- now with t~allIndexes:"
    do with index i item o over r~allIndexes
        say "#" i":" pp(o)
    end
    say

    say "--- the desired output:"
    i=1
    do with index idx item o over r
        say "#" i":" pp(idx) "->" pp(o)
        i=i+1
    end
    say

    ::routine pp
      return "["arg(1)"]"

Yielding:

    The Relation class:
    # caesar: [mark]
    # caesar: [denise]
    # berta: [julia]
    # adam: [julia]
    # adam: [henry]

    --- now with t~allItems:
    # 1: [mark]
    # 2: [denise]
    # 3: [julia]
    # 4: [julia]
    # 5: [henry]

    --- now with t~allIndexes:
    # 1: [caesar]
    # 2: [caesar]
    # 3: [berta]
    # 4: [adam]
    # 5: [adam]

    --- the desired output:
    # 1: [caesar] -> [mark]
    # 2: [caesar] -> [denise]
    # 3: [berta] -> [julia]
    # 4: [adam] -> [julia]
    # 5: [adam] -> [henry]

Having a "counter" subkeyword would allow numbering without the need to know 
the kind of collection
that is being iterated. E.g.

List example:

    l=.list~of("a","y","b")
    say l~class":"
    do with index idx item o over l counter i
        say "#" i":" pp (idx) "->" pp(o)
    end

    --- yielding:
    # 1: [0] -> [a]
    # 2: [1] -> [y]
    # 3: [2] -> [b]

Table example:

    t=.table~of(("adam","male"), ("berta","female"), ("caesar","male") )
    say t~class":"
    do with index idx item o over t counter i
        say "#" i":" pp(idx) "->" pp(o)
        i=i+1
    end

    --- yielding:
    # 1: [berta] -> [female]
    # 2: [caesar] -> [male]
    # 3: [adam] -> [male] 

or

Relation example:

    r=.relation~of(("adam","henry"), ("adam","julia"), ("berta","julia"), 
("caesar","denise"), ("caesar","mark") )
    say r~class":"
    do with index idx item o over r counter i
        say "#" i":" pp(idx) "->" pp(o)
    end

    --- yielding:
    # 1: [caesar] -> [mark]
    # 2: [caesar] -> [denise]
    # 3: [berta] -> [julia]
    # 4: [adam] -> [julia]
    # 5: [adam] -> [henry]

---rony






_______________________________________________
Oorexx-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/oorexx-devel

Reply via email to