[squeak-dev] #collect: on strings

cdrick cdrick65 at gmail.com
Wed Jun 25 07:39:48 UTC 2008


Hi

2008/6/25 Damien Cassou <damien.cassou at gmail.com>:
> Hi,
>
> is it normal that the following code fails:
>
> 'something' collect: [ :letter | letter isVowel ].
>
> It fails because #collect: creates a new string and tries to put
> booleans in it. However, I don't think this is the desired behavior.
>

it seems normal to me

"Evaluate aBlock with each of the receiver's elements as the argument.
Collect the resulting values into a collection like the receiver. Answer
the new collection."

(Array withAll: 'something') collect: [ :letter | letter isVowel ].
#(false true false true false false true false false)

this one works (as with blank):
'something' collect: [ :letter | (letter isVowel ) ifTrue: [letter]
ifFalse: [$.]]. '.o.e..i..'

and you have #select: for the desired behavior
"Evaluate aBlock with each of the receiver's elements as the argument.
Collect into a new collection like the receiver, only those elements for
which aBlock evaluates to true. Answer the new collection."

My 2 cents

Cédrick




> --
> Damien Cassou
> Peter von der Ahé: «I'm beginning to see why Gilad wished us good
> luck». (http://blogs.sun.com/ahe/entry/override_snafu)
>
>


More information about the Squeak-dev mailing list