I need help storing collections in an ArrayBuffer.

If you have any questions, remarks, ... if you need help... its here...

I need help storing collections in an ArrayBuffer.

Postby bobgillies » 15 Mar 2017 02:29

Hi Franck,

I'm trying to store collections of numbers into an ArrayBuffer like this:

Code: Select all
tvar: myList
ArrayBuffer new to myList

1 2 3 myList add
4 5 6 myList add
7 8 9 myList add

myList .

[[1,2,3] , [4,5,6] , [7,8,9]]


What is the word for creating a collection like this?

P.S. I just experimented with the REPL for a couple of minutes and this is what I needed:

Code: Select all
1 2 3 [ , , ] myList add
4 5 6 [ , , ] myList add
7 8 9 [ , , ] myList add

myList .

[[1,2,3] , [4,5,6] , [7,8,9]]


:)
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: I need help storing collections in an ArrayBuffer.

Postby Franck » 15 Mar 2017 16:56

:)

It works but why not just use the array syntax ?

Code: Select all
[ 1, 2, 3] myList add
[ 4, 5, 6] myList add
[ 7, 8, 9] myList add
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: I need help storing collections in an ArrayBuffer.

Postby bobgillies » 17 Mar 2017 01:03

Hi Franck, what I was trying to do was take three items off the stack as a set of three onto a Stack array , such as [[ x, y, z], [ . . . ] , [ . . . ]] so that I can backtrack to that set of three at a later time. I needed them and all future sets of three kept as separate records on a Stack.

In my Initialization method there would be a line creating a Stack array:

Code: Select all
Stack new to myList


Then in another method I would collect three parameters:

Code: Select all
foobar method: collectThree   \ x y z --
        top as ArrayBuffer  toggleThree apply
        self mylist pop drop [ , , ] self myList push
;


Then in another BEGIN UNTIL looping function I could apply a test on three parameters and either toggle them and exchange them with the top Stack set of three or delete the top Stack set of three:

Code: Select all
foobar method: crunchCollection   \ x y z --
       doWhile:
        [    self testThree apply
             ifTrue: [ self myList collectThree ]
             else:  [ self myList pop drop ]
             self myList top testThree apply
        ]
;


In these methods that I use to go about collecting parameters into a stack, perform a conditional function and conditional loop, I've been using [ , , ] to collect the three items at a time as a record set.

I tend to get tunnel vision though, and it would be nice to know of other ways that I can push three parameters as a set into an array.

Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26

Re: I need help storing collections in an ArrayBuffer.

Postby Franck » 17 Mar 2017 10:18

Ok.
What I don't see is why you need to translate an array of 3 into items on the stack, then collect them back to create an array of 3 again. But why not...

The word you are looking for is #arrayWith

arrayWith \ xi n -- [ xi ]

It takes n items on the stack and creates an array with them.

Code: Select all
1 2 3 3 arrayWith
Franck
 
Posts: 140
Joined: 29 Oct 2014 19:01

Re: I need help storing collections in an ArrayBuffer.

Postby bobgillies » 17 Mar 2017 19:04

I see, I overlooked that word in the Manual. :)

The example code I used to illustrate my goal doesn't convey the purpose for translating an array of items on the stack only to be re-created again on the stack.

The purpose in my arrangement for the three items in reality represent bit patterns for my left diagonal, right diagonal, and vertical lanes of threats. They are the nodes of a tree structure in a 8 Queens Problem I'm working on. And as nodes it's convenient to use Stack methods when I have to backtrack to an earlier node to explore other branches of the tree.

Bob
bobgillies
 
Posts: 60
Joined: 24 Jan 2017 06:26


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron