[Newbies] A do with ONLY index? (plus,. a style question)

Todd Blanchard tblanchard at mac.com
Sun Feb 4 04:44:50 UTC 2007


If you just want the index, you would do something like:
1 to: collection size do: [:i | "stuff goes here" ].

If you want to fill an entire collection with a single value, try
collection atAllPut: #rock.

You can use one collection of numbers to index the other.

aCollection atAll: #(1 3 7 9 12 ) put: #rock.

You might look at Interval as well.  It pretends to be a collection  
and you can iterate over it.

(Interval from: 1 to: 18 by: 3) do: [:ea | Transcript show: ea  
asString; space]
produces the sequence 1 4 7 10 13 16

I would probably do the grid class idea.

Object subclass: #Grid
instanceVariables: 'rows'.....

initializeWithPoint: aPoint

	rows := (interval 1 to: aPoint x) collect: [:r | Array new: aPoint y].

atAllPut: anObject

	rows do: [:r | r atAllPut: anObject]

atRow: row column: col

	^(rows at: row) at: col

atRow: row column: col put: anObject

	^(rows at: row) at: col put: anObject

rowsDo: aBlock

	rows do: aBlock

cellsDo: aBlock

	self rowsDo: [:row | row do: aBlock]

etc....


On Feb 3, 2007, at 5:44 PM, Blake wrote:

> In a Sequencable collection, doWithIndex seems to just call  
> withIndexDo. Is there a "do" that passes just the index, not the  
> object?
>
> My son is working on a roguelike, and we're looking at various ways  
> of creating the map. This is a two-dimensional map that would fit  
> in a character-mode, and we have this so far:
>
> | map xBound yBound |
> xBound := 80.
> yBound := 24.
> map := Array new: xBound.
> i := 1.
> [i <= xBound] whileTrue: [
> 	map at:i put: (Array new:yBound).
> 	i := i + 1
> 	].
>
> map do: [ :col|
> 	col withIndexDo: [:obj :y|
> 		col at:y put:#stone.
> 		]
> 	].
>
> This seems to lack elegance. Better would be:
>
> map := 2DArray new: 80 at 25.
> map doXY: [:xy| map atXY:xy put:#stone].
>
> Or even:
>
> grid := Grid new: 80 at 25 fill: #stone.
>
> Marcus has his 2DArray but it's peppered with caveats.
>
> 	Thoughts?
>
> 	===Blake===
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners



More information about the Beginners mailing list