Hi Gil,

On 09.03.2025 14:45, Gilbert Barmwater wrote:

Many years ago, I saw the same need but in a more general context, converting an instance of an ordered collection to a different collection type.  And, yes, I know that stems are NOT ordered collections!

The .list, .queue, .circularqueue (and .array) all have a makearray method.  So the first thought was to create a makelist, makequeue and makecircularqueue methods for all the classes.  Then it occurred to me that by adding a class method to the orderedcollection class, which I called 'from', I could achieve the same thing.  So, for example, if you have an array named arr, then .list~from(arr) would create an instance of .list containing all the items in arr in the same order.  The advantage is that a single method needs to be added and, by inheritance, all four of the ordered collection classes get the new functionality.

Since one of the most frequent uses of stems is to implement a (stem) array, extending the idea to that class seemed logical. Then one could code .stem~from(arr) and conversely .array~from(.a).  This requires an addition to the stem class of a 'from' class method and changes to the 'from' ordered collection method to handle stem instances.  And, yes, the edge cases mentioned in this thread need to be handled as well.

Sounds interesting!

The edge case for .stem, keeping the current makeArray semantics and allowing for the "stemArray" semantics (maybe as a second argument?), would be a nice show case perhaps.

I implemented this both in a private build and via sub-classing and it has worked well for me for a long time.  I strongly recommend this approach to the problem and am happy to share my implementation if the development community decides to open an RFE.

Will do to get to see your implementation, thanks for the offer! :)

Cheers

---rony



On 3/9/2025 6:39 AM, Rony G. Flatscher wrote:

1) ad stem arrays
==============

Stem arrays have been conceived and used since the inception of Rexx.

The pattern is simple: the tail is an integer number from 1 on, the tail with the value 0 keeps the current number of total stem array elements. E.g.:

    a.1="one"
    a.2="two"
    a.0=2       
    say "there are" a.0 "items"

Turning such a stem array into an ooRexx array should yield an array that omits the element with the tail 0 and uses the tail's number as index in the ooRexx array. E.g. turning the above stem array into an ooRexx array:

    a[1]="one"
    a[2]="two"
    say "there are" a~itmes "items"

1a) In order to create an ooRexx array from a stem array the following method should be added to the stem class:

    makeArrayFromStemArray

This name makes it clear that a stem array is to be processed and that the resulting array should not have the stem value with the tail 0. (The existting stem method makeArray continues to create an array that represents all tails of a stem, including a tail with the value 0.)

1b) In order to create a stem from an ooRexx array the following method should be added to the array class:

    makeStemArray

This name makes it clear that a stem array is to be created from the array items (using the array index values as tails) and adding the 0 tail to indicate the number of stem elements.

---

2) ad using the array class for reading and writing files/streams directly
==========================================================

The ooRexx stream class has a method arrayin to allow reading the content of a stream into an ooRexx array, and arrayout to write the content of an array to a stream. In order to use these methods a stream instance must be created first before being able to use the arrayin and arrayout methods and closing the stream thereafter. As the purpose of these two methods is to simply get the content into and from an array, it may be cumbersome to explicitly have to open and close streams in order to just read into or write the content of an array.

In order to forego the need to create stream instances in order to read the content into an array or an array into a stream or file two class methods for the ooRexx array class are proposed:

2a) In order to read the content of a stream or a file with a single message into an array the following method should be added to the array class:

    arrayin(filename|stream|file[, Lines|chars)

This allows for supplying either a filename (string), an instance of .File or .Stream and optionally "Lines" (default, one array element per text line) or "chars" (one array element per character).

2b) In order to write the content of an array to a file or a stream the following method should be added to the array class:

    arrayOut(filename|stream|file[, Lines|chars)

This allows for supplying either a filename (string), an instance of .File or .Stream and optionally "Lines" (default, a text line per per array element) or "chars" (the characters of all array elements, without appending endoflines).

---

Any comments, thoughts?

---rony

_______________________________________________
Oorexx-devel mailing list
Oorexx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/oorexx-devel

Reply via email to