[squeak-dev] Re: Want: #slice: (Re: Noob question: array slice?)

Igor Stasenko siguctua at gmail.com
Sat Aug 7 05:55:54 UTC 2010


On 7 August 2010 08:54, Igor Stasenko <siguctua at gmail.com> wrote:
> On 7 August 2010 06:56, Andreas Raab <andreas.raab at gmx.de> wrote:
>> On 8/6/2010 6:20 PM, Igor Stasenko wrote:
>>>
>>> On 6 August 2010 17:45, Randal L. Schwartz<merlyn at stonehenge.com>  wrote:
>>>>>>>>>
>>>>>>>>> "Casey" == Casey Ransberger<casey.obrien.r at gmail.com>  writes:
>>>>
>>>> Casey>  It's literally adding a method to make the cuneiform smalltalk
>>>> Casey>  crap accessible to new people, who are liable to search on terms
>>>> Casey>  like 'slice.'
>>>>
>>>> What you are asking for is something that would be useful for only
>>>> those first few hours, and then be a maintenance clutter for the
>>>> remaining years of programming.
>>>>
>>>> I vote "-1".
>>>>
>>> +1 to your -1.
>>
>> I vote -2 on your -1 which should leave us at +0 I think :-)=)
>>
>> No, seriously. I think there's actually a pretty good and generic
>> interpretation of slice if you consider it to be an operation that can
>> collect arbitrary sequences. For example:
>>
>> Collection>>slice: aCollection
>>        "Slice the receiver by aCollection, collecting all elements
>>        in the receiver that are keyed by aCollection."
>>
>>        ^aCollection collect:[:each| self at: each] as: self species.
>>
>> With a polymorphic implementation this would allow some interesting uses
>> that are quite difficult to implement otherwise, for example:
>>
>>        dd := Dictionary newFromPairs: {
>>                'firstName'. 'Joe'.
>>                'lastName'. 'Sixpack'
>>                'dob'. '01-01-2001'.
>>                'zip'. '12345'
>>        }.
>>        dd slice: #(firstName lastName).
>>
> this looks like form of #select: , where you selecting based on
> collection's keys, not values.
> (for arrayed collections a key is an element's index).
>
> So, it could be named #selectKeys:
>
> collection selectKeys: (1 to:10)
>
> or
>
> dictionary selectKeys: #(firstName lastName)
>
or #selectByKeys: ... whatever :)

> The problem is, that not all collections having a notion of keys. Sets
> don't have a keys. Bags too.
> Any meaningful example, what #slice would mean for Set or Bag?
>
> So, in this form its not generic enough for introducing in the root
> class. Only for classes, which having notion of keys.
>
>> This would return a dictionary with only firstName and lastName. And of
>> course, when used with intervals it would behave identical to #copyFrom:to:
>> just somewhat less efficient, but that's offset by, say, uses such as:
>>
>>        "Slice out all the odd elements"
>>        aCollection slice: (1 to: aCollection size by: 2).
>>
> err... did you meant following:
>
> aCollection select: [:each | each odd ]
>
> ?
>
>> Etc. So I think the argument for #slice: can be made and we should consider
>> it as a more generic operation instead of just a slower version of
>> #copyFrom:to:.
>>
>
> copying is slow and always will be. That's why i told that i trying to
> avoid it, if its possible.
>
>> Cheers,
>>  - Andreas
>>
>>
>
>
>
> --
> Best regards,
> Igor Stasenko AKA sig.
>



-- 
Best regards,
Igor Stasenko AKA sig.



More information about the Squeak-dev mailing list