some collection observations

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

some collection observations

wernerk
Hi,

1. i do this in pharo 2.0:
b:=Bag newFrom: #(1 2).
OrderedCollection newFrom: b."works nicely"
Array newFrom: b."error"
the error happens because ArrayedCollection>>newFrom: inherited by Array
expects a SequenceableCollection that has 'at:' implemented. but the
first line of ArrayedCollection>>newFrom: says:
newFrom: aCollection
it would be more clear,if it would say:
newFrom: aSequenceableCollection

2. this also produces an error:
b as: Array.
because Object>>as: uses newFrom:. and since as: is a very general
method, i'd guess that ArrayedCollection>>newFrom: should have an
implementation that does not throw an exception. if changing a Bag to an
OrderedCollection works, this should also work with an Array. or does
this make much sense: <grin>
(b as: OrderedCollection) as:Array "this way it works"

3. withAll: does essentially the same as newFrom: and has the same
problem with the argument naming. but Array>>withAll: is much much
faster than Array>>newFrom:. try:
b:=Array new:100000.
[Array withAll: b]timeToRun.
[Array newFrom: b]timeToRun.
perhaps one could differentiate the two methods in such a way, that
withAll: is the occasionally faster method that for some collections
only accepts aSequenceableCollection as argument and newFrom: accepts
any aCollection. This way also Object>>as: would work as expected. if
one does not want to change this in my opinion slightly inconsistent
behaviour, then <grin> it would make sense to implement
Array>>newFrom: aSequenceableCollection
^Array withAll: aSequenceableCollection.
simply for speed reasons. and of course a consistent argument naming
would be nice.
werner

Reply | Threaded
Open this post in threaded view
|

Re: some collection observations

Sven Van Caekenberghe-2
Hi Werner,

I don't know what you are trying to achieve, but it looks like you want to do conversions. Have you considered this ?

  (Bag with: 1 with: 2) asArray.
  (Bag with: 1 with: 2) asOrderedCollection.

These both work. I personally never noticed or used #newFrom:

As for #as: some people consider it not the cleanest way to do things.

But since the API is there, it should work I guess.

Sven

On 05 Mar 2014, at 16:28, Werner Kassens <[hidden email]> wrote:

> Hi,
>
> 1. i do this in pharo 2.0:
> b:=Bag newFrom: #(1 2).
> OrderedCollection newFrom: b."works nicely"
> Array newFrom: b."error"
> the error happens because ArrayedCollection>>newFrom: inherited by Array expects a SequenceableCollection that has 'at:' implemented. but the first line of ArrayedCollection>>newFrom: says:
> newFrom: aCollection
> it would be more clear,if it would say:
> newFrom: aSequenceableCollection
>
> 2. this also produces an error:
> b as: Array.
> because Object>>as: uses newFrom:. and since as: is a very general method, i'd guess that ArrayedCollection>>newFrom: should have an implementation that does not throw an exception. if changing a Bag to an OrderedCollection works, this should also work with an Array. or does this make much sense: <grin>
> (b as: OrderedCollection) as:Array "this way it works"
>
> 3. withAll: does essentially the same as newFrom: and has the same problem with the argument naming. but Array>>withAll: is much much faster than Array>>newFrom:. try:
> b:=Array new:100000.
> [Array withAll: b]timeToRun.
> [Array newFrom: b]timeToRun.
> perhaps one could differentiate the two methods in such a way, that withAll: is the occasionally faster method that for some collections only accepts aSequenceableCollection as argument and newFrom: accepts any aCollection. This way also Object>>as: would work as expected. if one does not want to change this in my opinion slightly inconsistent behaviour, then <grin> it would make sense to implement
> Array>>newFrom: aSequenceableCollection
> ^Array withAll: aSequenceableCollection.
> simply for speed reasons. and of course a consistent argument naming would be nice.
> werner
>


Reply | Threaded
Open this post in threaded view
|

Re: some collection observations

wernerk
 > I don't know what you are trying to achieve, but it looks like you
want to do conversions. Have you considered this ?

Hi Sven,

yes, i have. i simply noticed this, when i tried to speed up a program
and thought, i should mention it.

werner