SequenceableCollection.st
changeset 24773 b004df08089a
parent 24750 8586d5461ade
child 24782 89f69eca61c3
equal deleted inserted replaced
24772:bcb134483fd9 24773:b004df08089a
     1 "{ Encoding: utf8 }"
       
     2 
       
     3 "
     1 "
     4  COPYRIGHT (c) 1989 by Claus Gittinger
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     5 	      All Rights Reserved
     3 	      All Rights Reserved
     6 
     4 
     7  This software is furnished under a license and may be used
     5  This software is furnished under a license and may be used
  5382     "
  5380     "
  5383 
  5381 
  5384     "Modified: / 27-10-2006 / 10:07:02 / cg"
  5382     "Modified: / 27-10-2006 / 10:07:02 / cg"
  5385 !
  5383 !
  5386 
  5384 
       
  5385 inGroupsOf:n detect:anNArgBlock thenDo:anotherNArgBlock ifNone:exceptionValue
       
  5386     "evaluate the argument, anNArgBlock for every group of n elements in the collection,
       
  5387      until the block returns true. Then deliver the to anotherNArgBlock and return
       
  5388      that block's result. If none matches, return the valeu from exceptionValue.
       
  5389      An error will be reported, if the number of elements in the receiver
       
  5390      is not a multiple of n.
       
  5391      This is similar to slicesOf:detect:, but here, an N-arg block is expected."
       
  5392 
       
  5393     |stop argVector|
       
  5394 
       
  5395     stop := self size.
       
  5396 
       
  5397     "/ the reason for inlining the cases for 2/3 args is to avoid the temporary object creation, and to    
       
  5398     "/ allow for the compiler (jitter) to generate better code for the block-call
       
  5399     n == 2 ifTrue:[
       
  5400         1 to:stop by:2 do:[:index |
       
  5401             |a1 a2|
       
  5402 
       
  5403             a1 := self at:index.
       
  5404             a2 := self at:index+1.
       
  5405             (anNArgBlock value:a1 value:a2) ifTrue:[
       
  5406                 ^ anotherNArgBlock value:a1 value:a2
       
  5407             ].
       
  5408         ].
       
  5409         ^ exceptionValue value.
       
  5410     ].
       
  5411     n == 3 ifTrue:[
       
  5412         1 to:stop by:3 do:[:index |
       
  5413             |a1 a2 a3|
       
  5414 
       
  5415             a1 := self at:index.
       
  5416             a2 := self at:index+1.
       
  5417             a3 := self at:index+2.
       
  5418             (anNArgBlock value:a1 value:a2 value:a3) ifTrue:[
       
  5419                 ^ anotherNArgBlock value:a1 value:a2 value:a3
       
  5420             ].
       
  5421         ].
       
  5422         ^ exceptionValue value.
       
  5423     ].
       
  5424 
       
  5425     argVector := Array new:n.
       
  5426     1 to:stop by:n do:[:index |
       
  5427         argVector replaceFrom:1 to:n with:self startingAt:index.
       
  5428         (anNArgBlock valueWithArguments:argVector) ifTrue:[
       
  5429             ^ anotherNArgBlock valueWithArguments:argVector
       
  5430         ].
       
  5431     ].
       
  5432     ^ exceptionValue value.
       
  5433 
       
  5434     "
       
  5435      #(1 one 2 two 3 three 4 four 5 five 6 six)
       
  5436          inGroupsOf:2 detect:[:num :sym | num > 3]
       
  5437          thenDo:[:num :sym | sym] ifNone:'ouch'   
       
  5438 
       
  5439      #(1 one 2 two 3 three 4 four 5 five 6 six)
       
  5440          inGroupsOf:2 detect:[:num :sym | num > 99]
       
  5441          thenDo:[:num :sym | sym] ifNone:'ouch'      
       
  5442     "
       
  5443 
       
  5444     "Modified: / 27-10-2006 / 10:07:02 / cg"
       
  5445 !
       
  5446 
  5387 inGroupsOf:n do:anNArgBlock
  5447 inGroupsOf:n do:anNArgBlock
  5388     "evaluate the argument, anNArgBlock for every group of n elements in the collection.
  5448     "evaluate the argument, anNArgBlock for every group of n elements in the collection.
  5389      The block is called with n arguments for group of n consecutive elements in the receiver.
  5449      The block is called with n arguments for group of n consecutive elements in the receiver.
  5390      An error will be reported, if the number of elements in the receiver
  5450      An error will be reported, if the number of elements in the receiver
  5391      is not a multiple of n.
  5451      is not a multiple of n.