[Q] Best way of doing ...

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[Q] Best way of doing ...

Edgar J. De Cleene
[Q] Best way of doing ... People:
I need a way for two collections of different size could be tested for all elements in both.

Example:

A  := #(a1 a2 a3).
B := #(b1 b2 b3 b4 b5).

Some similar to

with: otherCollection do: twoArgBlock
    "Evaluate twoArgBlock with corresponding elements from this collection
    and otherCollection."
    otherCollection size = self size

But for different size

Very thanks


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: [Q] Best way of doing ...

Randal L. Schwartz
>>>>> "Edgar" == Edgar J De Cleene <[hidden email]> writes:

Edgar> People:
Edgar> I need a way for two collections of different size could be tested for all
Edgar> elements in both.

Edgar> Example:

Edgar> A  := #(a1 a2 a3).
Edgar> B := #(b1 b2 b3 b4 b5).

Edgar> Some similar to

Edgar> with: otherCollection do: twoArgBlock
Edgar>     "Evaluate twoArgBlock with corresponding elements from this collection
Edgar>     and otherCollection."
Edgar>     otherCollection size = self size

Edgar> But for different size

When they are different sizes, what do you want done with
mismatched elements?  Call the block anyway, passing nil for the
shorter list?  Or not call the block at all?

And depending on your choice, you should name the method clearly:

withOverlappingElementsOf:do:
with:ifAbsent:do:

the latter, you could call as:

   a with: b ifAbsent: nil do: [ ... ]

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: [Q] Best way of doing ...

Klaus D. Witzel
In reply to this post by Edgar J. De Cleene
On Tue, 05 May 2009 15:29:30 +0200, Edgar J. De Cleene wrote:

> People:
> I need a way for two collections of different size could be tested for  
> all elements in both.

A quick and dirty (a prototype) is, to put both into the same new  
IdentitySet (or set, depends on your #=) and then just compare sizes.

Then you also don't need to sort, the set (of both) will free you from  
sorting. And #copyWithoutAll: can then give you things in B which are not  
in A, etc.

HTH.

> Example:
>
> A  := #(a1 a2 a3).
> B := #(b1 b2 b3 b4 b5).
>
> Some similar to
>
> with: otherCollection do: twoArgBlock
>     "Evaluate twoArgBlock with corresponding elements from this  
> collection
>     and otherCollection."
>     otherCollection size = self size
>
> But for different size
>
> Very thanks

--
"If at first, the idea is not absurd, then there is no hope for it".  
Albert Einstein

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners