[Newbies] understanding selectors

Levente Uzonyi leves at elte.hu
Sun Jul 31 23:54:34 UTC 2011


Hi Jonathan,

On Sat, 30 Jul 2011, Jonathan Wright wrote:

snip

> perform: anOpperator with: aMatrix
> 	| newMatrix rowCount columnCount result |
> 	rowCount := self rowCount.
> 	columnCount := self columnCount.
> 	newMatrix := Matrix rows: rowCount columns: columnCount.
>
> 	1 to: rowCount do: [ :selectRow |
> 		1 to: columnCount do: [ :selectColumn |
> 		result := (self at: selectRow at: selectColumn)
> perform: anOpperator with: (aMatrix at: selectRow at: selectColumn).
> 		newMatrix at: selectRow at: selectColumn put: result.]].
> 	^newMatrix.

It's not such a good idea, to override #perform:with:, because your Matrix 
(and all other Matrix instances) will behave differently.

But you can use #with:collect: to achieve what you'd like to. A few 
examples:

In methods:

+ aMatrix

 	^self with: aMatrix collect: [ :each :other | each + other ]

- aMatrix

 	^self with: aMatrix collect: [ :each :other | each - other ]

Directly:

m1 with: m2 collect: [ :each :other | each + other ]

m1 with: m2 collect: [ :each :other | each - other ]

Or the tricky way: (which relies on Symbol >> #value:value:)

m1 with: m2 collect: #+

m1 with: m2 collect: #-


Levente

>
>
> where the subtraction/addition looks like this:
> subtractMatrix := matrixOne perform: #- with: matrixTwo.
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>


More information about the Beginners mailing list