Edit Rename Changes History Upload Download Back to Top

nDo:

Since a BlockClosure can introspect on its number of arguments you can have it take slices off an array without the programmer creating the slices explicitly.

Contrast a typical initializer snippet like

    #(  #( 1 'one' )
        #( 2 'two' ) 
     )
        do: [ :pair | 
            dict at: pair first put: pair last]

with this one:

    #(  1 'one'
        2 'two'
     )
        nDo: [ :num :string | 
            dict at: num put: string]
Note a lot less '#(' noise and the intention revealing names of the block arguments.

So here's an implementation:

SequenceableCollection>>
nDo: aBlock
    | step |
    step := aBlock numArgs.
    self size \\ step == 0 ifFalse: [self noMatchError].
    0   to: self size - 1
        by: step
        do: [:i | 
            | slice |
            slice := Array new: step.
            1 to: step do: [:j | slice at: j put: (self at: i + j)].
            aBlock valueWithArguments: slice]

I need a better name for this method....
Edit Rename Changes History Upload Download Back to Top