Collection.st
changeset 20212 e9d7cf16d560
parent 20211 c02d61712505
child 20220 874c664d2cbd
child 20226 53626d2fd697
equal deleted inserted replaced
20211:c02d61712505 20212:e9d7cf16d560
  3545         (aBlock value:each) ifTrue:[newCollection add:each].
  3545         (aBlock value:each) ifTrue:[newCollection add:each].
  3546     ].
  3546     ].
  3547     ^ newCollection
  3547     ^ newCollection
  3548 
  3548 
  3549     "
  3549     "
  3550      #(1 2 3 4) select:[:e | e odd]   
  3550      #(1 2 3 4) select:[:e | e odd]
  3551      (1 to:10) select:[:e | e even]     
  3551      (1 to:10) select:[:e | e even]
  3552     "
  3552     "
  3553 
  3553 
  3554     "Modified: / 07-08-2010 / 16:26:40 / cg"
  3554     "Modified: / 07-08-2010 / 16:26:40 / cg"
  3555 !
  3555 !
  3556 
  3556 
  3649 !
  3649 !
  3650 
  3650 
  3651 select:selectBlock thenDo:doBlock
  3651 select:selectBlock thenDo:doBlock
  3652     "combination of select followed by do.
  3652     "combination of select followed by do.
  3653      The same as if two separate select:+do: messages were sent,
  3653      The same as if two separate select:+do: messages were sent,
  3654      but avoids the creation of intermediate collections, 
  3654      but avoids the creation of intermediate collections,
  3655      so this is nicer for big collections."
  3655      so this is nicer for big collections."
  3656 
  3656 
  3657     self do:[:eachElement |
  3657     self do:[:eachElement |
  3658         (selectBlock value:eachElement) ifTrue:[
  3658         (selectBlock value:eachElement) ifTrue:[
  3659             doBlock value:eachElement
  3659             doBlock value:eachElement
  3664      #(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i]
  3664      #(1 2 3 4 5 6 7) select:[:i | i even] thenDo:[:i | Transcript showCR:i]
  3665     "
  3665     "
  3666 !
  3666 !
  3667 
  3667 
  3668 selectWithIndex:aTwoArgBlock
  3668 selectWithIndex:aTwoArgBlock
  3669     "return a new collection with all elements from the receiver, 
  3669     "return a new collection with all elements from the receiver,
  3670      for which the argument aBlock evaluates to true.
  3670      for which the argument aBlock evaluates to true.
  3671      aTwoArgBlock is called with value and index as arguments."
  3671      aTwoArgBlock is called with value and index as arguments."
  3672 
  3672 
  3673     |newCollection|
  3673     |newCollection|
  3674 
  3674 
  3677         (aTwoArgBlock value:eachValue value:eachKey) ifTrue:[newCollection add:eachValue].
  3677         (aTwoArgBlock value:eachValue value:eachKey) ifTrue:[newCollection add:eachValue].
  3678     ].
  3678     ].
  3679     ^ newCollection
  3679     ^ newCollection
  3680 
  3680 
  3681     "
  3681     "
  3682      #(10 20 30 40) selectWithIndex:[:e :i | i odd]   
  3682      #(10 20 30 40) selectWithIndex:[:e :i | i odd]
  3683      #(10 20 30 40) selectWithIndex:[:e :i | i even]   
  3683      #(10 20 30 40) selectWithIndex:[:e :i | i even]
  3684     "
  3684     "
  3685 !
  3685 !
  3686 
  3686 
  3687 triplesDo:aThreeArgBlock
  3687 triplesDo:aThreeArgBlock
  3688     "evaluate the argument, aThreeArgBlock for every element in the collection,
  3688     "evaluate the argument, aThreeArgBlock for every element in the collection,
  3691 
  3691 
  3692     self do:[:aTriple |
  3692     self do:[:aTriple |
  3693         aThreeArgBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3).
  3693         aThreeArgBlock value:(aTriple at:1) value:(aTriple at:2) value:(aTriple at:3).
  3694     ]
  3694     ]
  3695     "
  3695     "
  3696      #( 
  3696      #(
  3697         (1 one eins) 
  3697         (1 one eins)
  3698         (2 two zwei) 
  3698         (2 two zwei)
  3699         (3 three drei) 
  3699         (3 three drei)
  3700         (4 four vier) 
  3700         (4 four vier)
  3701         (5 five #'fuenf') 
  3701         (5 five #'fuenf')
  3702         (6 six sechs)
  3702         (6 six sechs)
  3703      ) 
  3703      )
  3704      triplesDo:[:num :sym1 :sym2 | 
  3704      triplesDo:[:num :sym1 :sym2 |
  3705                     Transcript show:num; space; show:sym1; space; showCR:sym2
  3705                     Transcript show:num; space; show:sym1; space; showCR:sym2
  3706                ]
  3706                ]
  3707     "
  3707     "
  3708 
  3708 
  3709     "Modified: 10.5.1997 / 14:15:43 / cg"
  3709     "Modified: 10.5.1997 / 14:15:43 / cg"
  3731     "
  3731     "
  3732 
  3732 
  3733     "Modified: 10.5.1997 / 14:15:43 / cg"
  3733     "Modified: 10.5.1997 / 14:15:43 / cg"
  3734 !
  3734 !
  3735 
  3735 
  3736 with:aCollection andDefault:defaultElement collect:aTwoArgBlock 
  3736 with:aCollection andDefault:defaultElement collect:aTwoArgBlock
  3737     "like with:colelct:, but use defaultElement for missing elements in aCollection
  3737     "like with:collect:, but use defaultElement for missing elements in aCollection
  3738      (i.e. if the receiver is longer)
  3738      (i.e. if the receiver is longer)
  3739      The third argument, aBlock must be a two-argument block, which is
  3739      The third argument, aBlock must be a two-argument block, which is
  3740      evaluated for each element-pair.
  3740      evaluated for each element-pair.
  3741      Collect the results and return a collection containing them.
  3741      Collect the results and return a collection containing them.
  3742      This method fails if neither the receiver nor aCollection is
  3742      This method fails if neither the receiver nor aCollection is
  3750         newCollection add:(aTwoArgBlock value:el1 value:el2).
  3750         newCollection add:(aTwoArgBlock value:el1 value:el2).
  3751     ].
  3751     ].
  3752     ^ newCollection as:self species
  3752     ^ newCollection as:self species
  3753 
  3753 
  3754     "
  3754     "
  3755      (1 to:3) with:#(one two) andDefault:'xxx' collect:[:num :sym | (num->sym)] 
  3755      (1 to:3) with:#(one two) andDefault:'xxx' collect:[:num :sym | (num->sym)]
  3756      #(1 2 3) with:#(10 20) andDefault:99 collect:[:x :y | (x@y)]  
  3756      #(1 2 3) with:#(10 20) andDefault:99 collect:[:x :y | (x@y)]
  3757     "
  3757     "
  3758 !
  3758 !
  3759 
  3759 
  3760 with:aCollection andDefault:defaultElement do:aTwoArgBlock
  3760 with:aCollection andDefault:defaultElement do:aTwoArgBlock
  3761     "evaluate the argument, aBlock for successive elements from
  3761     "evaluate the argument, aBlock for successive elements from
  3808         newCollection add:(aTwoArgBlock value:el1 value:el2).
  3808         newCollection add:(aTwoArgBlock value:el1 value:el2).
  3809     ].
  3809     ].
  3810     ^ newCollection as:self species
  3810     ^ newCollection as:self species
  3811 
  3811 
  3812     "
  3812     "
  3813      (1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)] 
  3813      (1 to:3) with:#(one two three) collect:[:num :sym | (num->sym)]
  3814      #(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)]  
  3814      #(1 2 3) with:#(10 20 30) collect:[:x :y | (x@y)]
  3815     "
  3815     "
  3816 !
  3816 !
  3817 
  3817 
  3818 with:aCollection conform:aTwoArgBlock
  3818 with:aCollection conform:aTwoArgBlock
  3819     "evaluate the argument, aBlock for successive elements from
  3819     "evaluate the argument, aBlock for successive elements from
  3827     ].
  3827     ].
  3828     ^ true.
  3828     ^ true.
  3829 
  3829 
  3830     "
  3830     "
  3831      (1 to:3) with:#(1 2 3 4) conform:[:a :b | a = b]   --- raises an error
  3831      (1 to:3) with:#(1 2 3 4) conform:[:a :b | a = b]   --- raises an error
  3832      (1 to:3) with:#(1 22 3) conform:[:a :b | a = b]  
  3832      (1 to:3) with:#(1 22 3) conform:[:a :b | a = b]
  3833      (1 to:3) with:#(1 2 3) conform:[:a :b | a = b]  
  3833      (1 to:3) with:#(1 2 3) conform:[:a :b | a = b]
  3834     "
  3834     "
  3835 !
  3835 !
  3836 
  3836 
  3837 with:aCollection contains:aTwoArgBlock
  3837 with:aCollection contains:aTwoArgBlock
  3838     "evaluate the argument, aBlock for successive elements from
  3838     "evaluate the argument, aBlock for successive elements from