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

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

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

Casey Ransberger-2
Bert, yer trollin' me.

The method finder was a useless waste of time for me in this case; I tried that. Alan's question (paraphrased:) How does one find the sine function if one doesn't know what it's called?

The lingua franca for this operation is "slice," in Perl, Python, and Ruby. That's what people (at least on the west coast of north America, and apparently Japan) seem to call it anyway. Is this a regional dialectic? Seems like I've seen the term used in enough (english) programming books to call it colloquial (at least in my own linguistic context.)

Far be it from me to challenge the great and otherwise particularly ancient Smalltalk standard in favor of what has become (for better or for worse) the commonest sense... and yes, I appreciate that Balkanization of protocol between dialects is a problem. But! Dude. It's called a friggin slice...
 
All I'm saying is, it might help new people find the damned thing if we called it (or aliased/wrapped it as,) #slice:. Having a message one can send of that name would have made the (yes, Bert!) friggin method finder actually useful to me in this case.

I know this message may seem a bit rash / flamish, but you can't even fathom the number of times I've removed a four letter word starting with $f from it. To be clear: I'll implement #slice:, if people want it. I don't know about the range thing though, I've never done a binary selector, and I don't know if any one that might make sense can be done without messing with the parser/etc. 

Arm your photon torpedoes, ladies and gentlemen, and lock onto me;)

Fire.

On Aug 6, 2010, at 1:03 AM, Bert Freudenberg <[hidden email][hidden email][hidden email]> wrote:

On 06.08.2010, at 08:03, Casey Ransberger wrote:

Given that I have a collection like #(foo bar baz 1 2 3), and I want to get elements 2...4, what selector do I use?
I'm confused. Argh. Seems like I should be able to figure this out by now without what I think of being a pain. I guess I can just test the bounds, but yuck.

Halp! Invizibal #slice!

Can haz method finder?

'hello'. (2 to: 4). 'ell'

- Bert -




Reply | Threaded
Open this post in threaded view
|

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

Ralph Johnson
You want one of the copy methods from SequenceableCollection.  There
are a bunch of them, and I always have to look up the exact name.  In
your case, you want copyFrom:to: as in (anArray copyFrom: 2 to: 4)
will give you a 3 element slides of anArray.   Since this is in
SequenceableCollection, it will work with strings, arrays, ordered
collections, etc.

-Ralph

Reply | Threaded
Open this post in threaded view
|

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

Casey Ransberger-2
I get that, now, but thanks. What I really want, though, is to have a method in the system named something starting with 'slice,' because that's what I (and most of my friends, really,) would look for when trying to do the thing I was trying to do when I couldn't find a method that did what I wanted to do. When I think about grabbing a subset of a SequenceableCollection, I don't think to use the word 'copy' to find what I'm looking for.

On Aug 6, 2010, at 2:54 AM, Ralph Johnson <[hidden email]> wrote:

> You want one of the copy methods from SequenceableCollection.  There
> are a bunch of them, and I always have to look up the exact name.  In
> your case, you want copyFrom:to: as in (anArray copyFrom: 2 to: 4)
> will give you a 3 element slides of anArray.   Since this is in
> SequenceableCollection, it will work with strings, arrays, ordered
> collections, etc.
>
> -Ralph
>

Reply | Threaded
Open this post in threaded view
|

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

Ralph Johnson
On Fri, Aug 6, 2010 at 5:06 AM, Casey Ransberger
<[hidden email]> wrote:
> I get that, now, but thanks. What I really want, though, is to have a method in the system named something starting with 'slice,' because that's what I (and most of my friends, really,) would look for when trying to do the thing I was trying to do when I couldn't find a method that did what I wanted to do. When I think about grabbing a subset of a SequenceableCollection, I don't think to use the word 'copy' to find what I'm looking for.

There are lots of things in Smalltalk with odd names.  My favorite
example is inject:into: with ought to have "reduce" in its name.  Some
are new or little used and so not hard to change.   Some are well
established and so have become part of the language and so are hard to
change.  Of course, "hard" doesn't mean "impossible".  But it would
probably take a long and concerted effort to get people to change this
name, and the chance of failure is high.

It is easy to change Smalltalk for yourself.   It is hard to get other
people to change theirs.  Even if you got your methods into Squeak, a
lot of people would avoid using them because they would want their
code to be compatible with other Smalltalks.

-Ralph

Reply | Threaded
Open this post in threaded view
|

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

Casey Ransberger-2
As far as the range "operator," well, a kid can dream. But about #slice:

I'm talking about wrapping a method, not replacing it. I CC'd James Robertson, even though Cincom is unlikely to consider "yet another method."

It's literally adding a method to make the cuneiform smalltalk crap accessible to new people, who are liable to search on terms like 'slice.'

On Aug 6, 2010, at 3:19 AM, Ralph Johnson <[hidden email]> wrote:

> On Fri, Aug 6, 2010 at 5:06 AM, Casey Ransberger
> <[hidden email]> wrote:
>> I get that, now, but thanks. What I really want, though, is to have a method in the system named something starting with 'slice,' because that's what I (and most of my friends, really,) would look for when trying to do the thing I was trying to do when I couldn't find a method that did what I wanted to do. When I think about grabbing a subset of a SequenceableCollection, I don't think to use the word 'copy' to find what I'm looking for.
>
> There are lots of things in Smalltalk with odd names.  My favorite
> example is inject:into: with ought to have "reduce" in its name.  Some
> are new or little used and so not hard to change.   Some are well
> established and so have become part of the language and so are hard to
> change.  Of course, "hard" doesn't mean "impossible".  But it would
> probably take a long and concerted effort to get people to change this
> name, and the chance of failure is high.
>
> It is easy to change Smalltalk for yourself.   It is hard to get other
> people to change theirs.  Even if you got your methods into Squeak, a
> lot of people would avoid using them because they would want their
> code to be compatible with other Smalltalks.
>
> -Ralph
>

Reply | Threaded
Open this post in threaded view
|

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

Bert Freudenberg
In reply to this post by Casey Ransberger-2
On 06.08.2010, at 11:44, Casey Ransberger wrote:

> Bert, yer trollin' me.

Far from it.

> The method finder was a useless waste of time for me in this case; I tried that. Alan's question (paraphrased:) How does one find the sine function if one doesn't know what it's called?

You use the method finder. It works *not* by naming a function but giving it a (hopefully unique) example.

To find the sine function you could give

        60. 0.5

And to find your slice method you would give it

        'hello'. 2. 4. 'ell'

- Bert -


Reply | Threaded
Open this post in threaded view
|

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

laza
2010/8/6 Bert Freudenberg <[hidden email]>
To find the sine function you could give

       60. 0.5 

Not to confuse people that this will correctly list degreeCos. Since this is an artificial example

Float pi. 0.

used in the method finder will give you a hint for the sine function

Alex


Reply | Threaded
Open this post in threaded view
|

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

Bert Freudenberg

On 06.08.2010, at 13:04, Alexander Lazarević wrote:

> 2010/8/6 Bert Freudenberg <[hidden email]>
>> To find the sine function you could give
>>
>>        60. 0.5
>>
> Not to confuse people that this will correctly list degreeCos. Since this is an artificial example
>
> Float pi. 0.
>
> used in the method finder will give you a hint for the sine function
>
> Alex

Err, what he said. Trig is evil ;)

I first wanted to suggest

        90. 1.

(which I actually tried) but didn't because that lists two more candidates.

- Bert -



Reply | Threaded
Open this post in threaded view
|

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

Ralph Johnson
After I looked up the name for copyFrom:to:, I decided to find it with
method finder.  I don't use method finder much, which is my own
failing.  I had to play around with it a bit because Bert's original
input didn't work for me.   What worked was
  'Hello'. 2 . 4 . 'ell'
which gave me copyFrom:to: as the only answer

Every time I use method finder I ask myself why I don't use it more!
"Intro to Squeak" documentation, including my own, should emphasize it
more.

But method finder has its limits.  'Hello'. (2 to: 4). 'ell' doesn't
show anything, because there is no method with that exact combination
of arguments.

-Ralph

Reply | Threaded
Open this post in threaded view
|

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

Bert Freudenberg

On 06.08.2010, at 15:02, Ralph Johnson wrote:

> After I looked up the name for copyFrom:to:, I decided to find it with
> method finder.  I don't use method finder much, which is my own
> failing.  I had to play around with it a bit because Bert's original
> input didn't work for me.   What worked was
>  'Hello'. 2 . 4 . 'ell'
> which gave me copyFrom:to: as the only answer
>
> Every time I use method finder I ask myself why I don't use it more!
> "Intro to Squeak" documentation, including my own, should emphasize it
> more.
>
> But method finder has its limits.  'Hello'. (2 to: 4). 'ell' doesn't
> show anything, because there is no method with that exact combination
> of arguments.
>
> -Ralph

Works for me, both in a trunk image and a 3.8-derived one. Where did you try?

- Bert -



Reply | Threaded
Open this post in threaded view
|

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

Randal L. Schwartz
In reply to this post by Casey Ransberger-2
>>>>> "Casey" == Casey Ransberger <[hidden email]> 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.'

It takes only a few hours with Smalltalk to learn that Smalltalk uses
its own terminology for a lot of things, and then how to search for such
things, regardless of their name.

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".

--
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

Reply | Threaded
Open this post in threaded view
|

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

Igor Stasenko
On 6 August 2010 17:45, Randal L. Schwartz <[hidden email]> wrote:

>>>>>> "Casey" == Casey Ransberger <[hidden email]> 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.'
>
> It takes only a few hours with Smalltalk to learn that Smalltalk uses
> its own terminology for a lot of things, and then how to search for such
> things, regardless of their name.
>
> 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 don't remember that i had a need for such behavior so often.
I always trying to avoid copying, because it kills performance.
The only thing, which is close to slicing is parsing a source
code/xml, where you
splitting a big text on a smaller parts by tokenizing them. But even
then, you don't need something like 'slice',
because you usually using streams to parse data, and read input sequentially.

> --
> 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
>
>



--
Best regards,
Igor Stasenko AKA sig.

Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Andreas.Raab
On 8/6/2010 6:20 PM, Igor Stasenko wrote:

> On 6 August 2010 17:45, Randal L. Schwartz<[hidden email]>  wrote:
>>>>>>> "Casey" == Casey Ransberger<[hidden email]>  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 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).

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:.

Cheers,
   - Andreas

Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Igor Stasenko
On 7 August 2010 06:56, Andreas Raab <[hidden email]> wrote:

> On 8/6/2010 6:20 PM, Igor Stasenko wrote:
>>
>> On 6 August 2010 17:45, Randal L. Schwartz<[hidden email]>  wrote:
>>>>>>>>
>>>>>>>> "Casey" == Casey Ransberger<[hidden email]>  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)

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.

Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Igor Stasenko
On 7 August 2010 08:54, Igor Stasenko <[hidden email]> wrote:

> On 7 August 2010 06:56, Andreas Raab <[hidden email]> wrote:
>> On 8/6/2010 6:20 PM, Igor Stasenko wrote:
>>>
>>> On 6 August 2010 17:45, Randal L. Schwartz<[hidden email]>  wrote:
>>>>>>>>>
>>>>>>>>> "Casey" == Casey Ransberger<[hidden email]>  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.

Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Bert Freudenberg
In reply to this post by Andreas.Raab

On 07.08.2010, at 05:56, Andreas Raab wrote:

> On 8/6/2010 6:20 PM, Igor Stasenko wrote:
>> On 6 August 2010 17:45, Randal L. Schwartz<[hidden email]>  wrote:
>>>>>>>> "Casey" == Casey Ransberger<[hidden email]>  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 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).
>
> 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:.

But we have that already. It's called #atAll: which is nicely consistent with the rest of the collection protocols. We just need to add a more generic version in Collection.

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Bert Freudenberg

On 07.08.2010, at 12:29, Bert Freudenberg wrote:

>
> On 07.08.2010, at 05:56, Andreas Raab wrote:
>
>> On 8/6/2010 6:20 PM, Igor Stasenko wrote:
>>> On 6 August 2010 17:45, Randal L. Schwartz<[hidden email]>  wrote:
>>>>>>>>> "Casey" == Casey Ransberger<[hidden email]>  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 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).
>>
>> 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:.
>
> But we have that already. It's called #atAll: which is nicely consistent with the rest of the collection protocols. We just need to add a more generic version in Collection.
>
> - Bert -

That said, having a Subsequence object like Travis mentioned would warrant adding a new protocol. I seem to remember seeing something like this in Squeak before (MappedCollection maybe?)

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Stephen Pair
In reply to this post by Andreas.Raab
On Fri, Aug 6, 2010 at 11:56 PM, Andreas Raab <[hidden email]> wrote:
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:.

+1 ...I think Andreas makes a compelling argument without considering newbies from other languages that have a notion of array slicing.  But, I also think this would be great for newbies to Squeak when they do use slice and then dig a little deeper to find that squeak's slice is implemented with a ginsu knife.

- Stephen


Reply | Threaded
Open this post in threaded view
|

Re: Want: #slice: (Re: Noob question: array slice?)

Travis Griggs-4
In reply to this post by Bert Freudenberg
On Aug 7, 2010, at 3:34 AM, Bert Freudenberg wrote:

>
> On 07.08.2010, at 12:29, Bert Freudenberg wrote:
>
>>
>> On 07.08.2010, at 05:56, Andreas Raab wrote:
>>
>>> On 8/6/2010 6:20 PM, Igor Stasenko wrote:
>>>> On 6 August 2010 17:45, Randal L.  
>>>> Schwartz<[hidden email]>  wrote:
>>>>>>>>>> "Casey" == Casey Ransberger<[hidden email]>  
>>>>>>>>>> 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 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).
>>>
>>> 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:.
>>
>> But we have that already. It's called #atAll: which is nicely  
>> consistent with the rest of the collection protocols. We just need  
>> to add a more generic version in Collection.
>>
>> - Bert -
>
> That said, having a Subsequence object like Travis mentioned would  
> warrant adding a new protocol. I seem to remember seeing something  
> like this in Squeak before (MappedCollection maybe?)

I didn't actually add a new protocol. Per se. I admit to adding some  
particular "helpers." Instead, I placed the burden on the Subsequence  
class. The reason I did this, was that we actually use a couple  
different kinds of Subsequences in the differencing engine. You can  
have a general purpose Subsequence class that does the general things  
that SequenceableCollection (SC) can do, but then what happens when  
you want refined behavior on a subsequence?  Or you want to  
differentiate thru polymorphic types how you interact with different  
"slices." So in VW for example, there is a TextFragment subclass of  
Subsequence. It's really meant to work with CharacterArrays. And then  
there's 3 different subclasses for difference differentiation:  
InsertSequence, DeleteSequence, MatchSequence. If I had tried to put  
the burden of creating these different kinds of subsequences on SC, it  
would be a potential method explosion. It was simpler easier, and even  
more clear (to me) to make it clear what kind of slice I was creating.

At Key Tech, we had something similar to the Subsequence class, called  
a ReindexedCollection. It used an Interval object for the mapping.  
Which was actually *very* useful for image processing standard argb32  
images. You could write code like:

Bag withAll: (image bitsInstVar from: 2 by: 4)

And that would give you a histogram of the red channel of an image,  
or...

(image bitsInstVar from: 3 by: 4) median

to get the median green value in an image.

--
Travis Griggs
Objologist
"I did not have time to write you a short program, so I wrote you a  
long one instead."