## Code Abbey -- Array Counters

Oforth examples. Feel free to post your own code.

### Code Abbey -- Array Counters

data input:
10 3
1 2 3 2 3 1 1 1 1 3

5 2 3

Which means count occurances of each of the numbers and output them in ascending order, we have 5 1's 2 2's and 3 3's

I was struggling a bit with the buffer lists, until I realized they are basically a pointer to an object, I kept loosing my reference to it.

Code: Select all
`: getLn { System.Console askln }                                                : getNumber { getLn asInteger }                                                 : pprint { apply(#.) printcr }: getInput { []   getNumber   #[ getLn + ] times }: parseLine { words map(#asInteger) }                                           : prepareBuffer { ListBuffer new swap #[ dup add(0)] times }: countOcurr(list, buf) { |n| list forEach: n [ n buf at 1 + n buf put ] buf dup freeze }getLn parseLine   second prepareBuffergetLn parseLine   countOcurrpprint`

With objects as pointers it really makes sense to save them as a local, so ten I can't really be my normal me and ignore locals
I'm also not completely sure about my way of initializing the 0's to the array, but it made it easy to use the same logic if there was not yet a count and if they already have some numbers counted.
sotolf

Posts: 55
Joined: 30 Jul 2015 15:53

### Re: Code Abbey -- Array Counters

To initialize a list buffer, you can use either #init or #initValue

#initValue ( aValue n ListBuffer )
Creates a new list buffer of size n all initialized with aValue

Example :
Code: Select all
`ListBuffer initValue(10, 0)`

So, your prepareBuffer can be :

Code: Select all
`: prepareBuffer { 0 swap ListBuffer initValue }`

Or :
Code: Select all
`: preparBuffer(n) { ListBuffer initValue(n, 0) }`

#init ( aBlock n ListBuffer )
Creates a new list buffer of size n all initialized with the result of aBlock.
The block is performed for each element to be initialized.

Example :
Code: Select all
`ListBuffer init(100, #[ 10 rand ])`
Franck

Posts: 171
Joined: 29 Oct 2014 19:01

### Re: Code Abbey -- Array Counters

To initialize a list buffer, you can use either #init or #initValue

I knew that there would be something that I had overlooked, there usually are Then I know for the next time I would need it, that's probably the function for initialize a StringBuffer as well then

Or :

I'm not sure how I manage to overlook so much stuff, maybe I should start working on some kind of documentation, so that I can get a bit more grip of things. But then it's about time again, and sadly there isn't that much of it lately that I have to use on the computer with work and kid and stuff
sotolf

Posts: 55
Joined: 30 Jul 2015 15:53