Collection.st
changeset 16217 4b229d12de7f
parent 16215 77d4eff050f7
child 16219 d47d831c4584
equal deleted inserted replaced
16216:386578673083 16217:4b229d12de7f
  2447      #(1 2 3 4) detect: #odd     
  2447      #(1 2 3 4) detect: #odd     
  2448      #(2 4 6 8) detect: #odd  
  2448      #(2 4 6 8) detect: #odd  
  2449     "
  2449     "
  2450 !
  2450 !
  2451 
  2451 
  2452 detect:generatorBlock forWhich:testBlock ifNone:exceptionBlock
  2452 detect:generatorBlock forWhich:testBlock ifNone:exceptionValue
  2453     "evaluate generatorBlock for each element in the receiver until
  2453     "evaluate generatorBlock for each element in the receiver until
  2454      testBlock returns true for it; 
  2454      testBlock returns true for it; 
  2455      in this case return the value from generatorBlock, which caused the true evaluation.
  2455      in this case return the value from generatorBlock, which caused the true evaluation.
  2456      If none of the test evaluations returns true, return the result of the
  2456      If none of the test evaluations returns true, return the value from exceptionValue"
  2457      evaluation of the exceptionBlock"
       
  2458 
  2457 
  2459     self do:[:each |
  2458     self do:[:each |
  2460         |val|
  2459         |val|
  2461 
  2460 
  2462         val := generatorBlock value:each.
  2461         val := generatorBlock value:each.
  2463         (testBlock value:val) ifTrue:[^ val].
  2462         (testBlock value:val) ifTrue:[^ val].
  2464     ].
  2463     ].
  2465     ^ exceptionBlock value
  2464     ^ exceptionValue value
  2466 
  2465 
  2467     "
  2466     "
  2468      #(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry']    
  2467      #(2 3 4) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry']    
  2469      #( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry']    
  2468      #( 2 4 ) detect:[:n | n squared] forWhich:[:nsq | nsq odd] ifNone:['sorry']    
  2470 
  2469 
  2473         forWhich:[:fn | fn exists]
  2472         forWhich:[:fn | fn exists]
  2474         ifNone:nil    
  2473         ifNone:nil    
  2475     "
  2474     "
  2476 !
  2475 !
  2477 
  2476 
  2478 detect:aOneArgBlock ifNone:exceptionBlock
  2477 detect:aOneArgBlock ifNone:exceptionValue
  2479     "evaluate the argument, aBlock for each element in the receiver until
  2478     "evaluate the argument, aBlock for each element in the receiver until
  2480      the block returns true; in this case return the element which caused
  2479      the block returns true; in this case return the element which caused
  2481      the true evaluation.
  2480      the true evaluation.
  2482      If none of the evaluations returns true, return the result of the
  2481      If none of the evaluations returns true, return the value from exceptionValue"
  2483      evaluation of the exceptionBlock"
       
  2484 
  2482 
  2485     self do:[:each | 
  2483     self do:[:each | 
  2486         (aOneArgBlock value:each) ifTrue:[^ each].
  2484         (aOneArgBlock value:each) ifTrue:[^ each].
  2487     ].
  2485     ].
  2488     ^ exceptionBlock value
  2486     ^ exceptionValue value
  2489 
  2487 
  2490     "
  2488     "
  2491      #(1 2 3 4) detect:[:n | n odd] ifNone:['sorry']    
  2489      #(1 2 3 4) detect:[:n | n odd] ifNone:['sorry']    
  2492      #(2 4 6 8) detect:[:n | n odd] ifNone:['sorry']     
  2490      #(2 4 6 8) detect:[:n | n odd] ifNone:['sorry']     
  2493     "
  2491     "
  2541      #(1 2 3 4) detectLast:[:n | n odd]   
  2539      #(1 2 3 4) detectLast:[:n | n odd]   
  2542      #(2 4 6 8) detectLast:[:n | n odd]  
  2540      #(2 4 6 8) detectLast:[:n | n odd]  
  2543     "
  2541     "
  2544 !
  2542 !
  2545 
  2543 
  2546 detectLast:aBlock ifNone:exceptionBlock
  2544 detectLast:aBlock ifNone:exceptionValue
  2547     "evaluate the argument, aBlock for each element in the receiver until
  2545     "evaluate the argument, aBlock for each element in the receiver until
  2548      the block returns true; in this case return the element which caused
  2546      the block returns true; in this case return the element which caused
  2549      the true evaluation. The elements are processed in reverse order.
  2547      the true evaluation. The elements are processed in reverse order.
  2550      If none of the evaluations returns true, return the result of the
  2548      If none of the evaluations returns true, return the value from exceptionValue"
  2551      evaluation of the exceptionBlock"
       
  2552 
  2549 
  2553     self reverseDo:[:each | 
  2550     self reverseDo:[:each | 
  2554         (aBlock value:each) ifTrue:[^ each].
  2551         (aBlock value:each) ifTrue:[^ each].
  2555     ].
  2552     ].
  2556     ^ exceptionBlock value
  2553     ^ exceptionValue value
  2557 
  2554 
  2558     "
  2555     "
  2559      #(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry']    
  2556      #(1 2 3 4) detectLast:[:n | n odd] ifNone:['sorry']    
  2560      #(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry']     
  2557      #(2 4 6 8) detectLast:[:n | n odd] ifNone:['sorry']     
  2561     "
  2558     "
  3296     "
  3293     "
  3297 
  3294 
  3298     "Created: / 07-08-2010 / 16:26:15 / cg"
  3295     "Created: / 07-08-2010 / 16:26:15 / cg"
  3299 !
  3296 !
  3300 
  3297 
  3301 select:aBlock ifNone:exceptionBlock
  3298 select:aBlock ifNone:exceptionValue
  3302     "try a new collection with all elements from the receiver, for which
  3299     "try a new collection with all elements from the receiver, for which
  3303      the argument aBlock evaluates to true. If none of the elements passes
  3300      the argument aBlock evaluates to true. If none of the elements passes
  3304      the check of aBlock, return the result of evaluating exceptionBlock.
  3301      the check of aBlock, return the value from exceptionValue.
  3305      See also: #removeAllFoundIn: and #removeAllSuchThat:"
  3302      See also: #removeAllFoundIn: and #removeAllSuchThat:"
  3306 
  3303 
  3307     |newCollection|
  3304     |newCollection|
  3308 
  3305 
  3309     newCollection := self select:aBlock.
  3306     newCollection := self select:aBlock.
  3310     newCollection isEmpty ifTrue:[^ exceptionBlock value].
  3307     newCollection isEmpty ifTrue:[^ exceptionValue value].
  3311     ^ newCollection
  3308     ^ newCollection
  3312 
  3309 
  3313     "
  3310     "
  3314      #(1 2 3 4) select:[:e | e > 10] ifNone:['sorry']  
  3311      #(1 2 3 4) select:[:e | e > 10] ifNone:['sorry']  
  3315     "
  3312     "
  5544 ! !
  5541 ! !
  5545 
  5542 
  5546 !Collection class methodsFor:'documentation'!
  5543 !Collection class methodsFor:'documentation'!
  5547 
  5544 
  5548 version
  5545 version
  5549     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.333 2014-03-05 13:14:02 cg Exp $'
  5546     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.334 2014-03-05 13:19:49 cg Exp $'
  5550 !
  5547 !
  5551 
  5548 
  5552 version_CVS
  5549 version_CVS
  5553     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.333 2014-03-05 13:14:02 cg Exp $'
  5550     ^ '$Header: /cvs/stx/stx/libbasic/Collection.st,v 1.334 2014-03-05 13:19:49 cg Exp $'
  5554 ! !
  5551 ! !
  5555 
  5552 
  5556 
  5553 
  5557 Collection initialize!
  5554 Collection initialize!