[chuck-users] Ragged object array

Dana Batali dana.batali at gmail.com
Fri Jan 7 13:00:17 EST 2022


Michael - nice!  The trick is to hide an array within a class/object!
thanks mucho!
Dana

On Fri, Jan 7, 2022 at 9:44 AM Michael Heuer <heuermh at gmail.com> wrote:

> Hello Dana,
>
> If you don't mind using LicK and its early-java-ish syntax with lots of
> casting, you could do it with ArrayList
>
> ---
>
> // list elements
> class Element {
>   string name;
>   int value;
>
>   fun static Element create(string name, int value)
>   {
>     Element e;
>     name => e.name;
>     value => e.value;
>     return e;
>   }
> }
>
> Element.create("foo", 1) @=> Element foo;
> Element.create("bar", 2) @=> Element bar;
> Element.create("baz", 3) @=> Element baz;
> Element.create("qux", 4) @=> Element qux;
>
> // inner lists
> ArrayList list0;
> ArrayList list1;
> ArrayList list2;
>
> list0.add(foo);
> list0.add(bar);
> list1.add(baz);
> list2.add(qux);
>
> // outer list
> ArrayList lists;
> lists.add(list0);
> lists.add(list1);
> lists.add(list2);
>
> <<<"indexed access">>>;
>
> {
>   lists.get(0) $ ArrayList @=> ArrayList list;
>   list.get(0) $ Element @=> Element e;
>   <<<"  ", e.name, e.value>>>;
> }
>
> <<<"iterator access">>>;
>
> lists.iterator() @=> Iterator outerIterator;
> while (outerIterator.hasNext())
> {
>   outerIterator.next() $ ArrayList @=> ArrayList list;
>   list.iterator() @=> Iterator innerIterator;
>   while (innerIterator.hasNext())
>   {
>     innerIterator.next() $ Element @=> Element e;
>     <<<"  ", e.name, e.value>>>;
>   }
> }
>
> <<<"foreach access">>>;
>
> class Inner extends UnaryProcedure {
>   fun void run(Object value) {
>     value $ Element @=> Element e;
>     <<<"  ", e.name, e.value>>>;
>   }
> }
>
> class Outer extends UnaryProcedure {
>   fun void run(Object value) {
>     value $ ArrayList @=> ArrayList list;
>     Inner inner;
>     list.forEach(inner);
>   }
> }
>
> Outer outer;
> lists.forEach(outer);
>
> <<<"done">>>;
>
> ---
>
> ...
> "indexed access" : (string)
>    foo 1
> "iterator access" : (string)
>    foo 1
>    bar 2
>    baz 3
>    qux 4
> "foreach access" : (string)
>    foo 1
>    bar 2
>    baz 3
>    qux 4
> "done" : (string)
>
>
> Hope this helps!
>
>    michael
>
>
> > On Jan 7, 2022, at 10:45 AM, Dana Batali <dana.batali at gmail.com> wrote:
> >
> > Fellow ChucK users:  can anyone comment on the possibility of achieving
> a ragged object array in ChucK?  By ragged array, I mean an array of arrays
> of Objects as opposed to a 2D array.
> >
> > The docs state that an array is an Object, so it's within the realm of
> possible, by my current guess is that it's not possible to cast an Object
> to an array so that I can invoke, eg, it's size method.
> >
> > Any comments appreciated!
> > Thanks in advance, Dana
> >
> > Here's some not-quite working code:
> >
> > // this part works.
> > Object @ meta[]; // an array of arrays of objects (ragged)
> > for(int i;i<10;i++)
> > {
> >     Object subarray[0];
> >     meta << subarray;
> > }
> >
> > // here's the problem:
> >
> > // Object  has no method size() (compile-time failure)
> > // <<< "element[2] size: " , meta[2].size() >>>;
> >
> > // so the solution must involve a cast operation
> > // but this produces a syntax error
> > (meta[2] $ Object[]) @=> Object a[];
> > <<< a.size() >>>;
> > _______________________________________________
> > chuck-users mailing list
> > chuck-users at lists.cs.princeton.edu
> > https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
> _______________________________________________
> chuck-users mailing list
> chuck-users at lists.cs.princeton.edu
> https://lists.cs.princeton.edu/mailman/listinfo/chuck-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cs.princeton.edu/pipermail/chuck-users/attachments/20220107/d093b0c3/attachment.html>


More information about the chuck-users mailing list