Noob question: array slice?

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

Noob question: array slice?

Casey Ransberger-2
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!
--
Casey Ransberger


Reply | Threaded
Open this post in threaded view
|

Re: Noob question: array slice?

Travis Griggs-4
On Aug 5, 2010, at 11:03 PM, 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.


#(foo bar baz 1 2 3) copyFrom: 2 to: 4

? is that what what you mean?

another way, more general would be

(2 to: 4) collect: [:n | original at: n]

In VisualWorks 7.7.1, we added a Subsequence object, that can act as a  
subrange facade to a sequence. It's very very handy for the Meyer diff  
algorithm, and doing Text differencing.

Subsequence sequence: #(foo bar baz 1 2 3) from: 2 to: 4

You can stack them of course.

--
Travis Griggs
Objologist
For every adage, there is an equal and contrary un-adage


Reply | Threaded
Open this post in threaded view
|

Re: Noob question: array slice?

Casey Ransberger-2
Thanks, man. I can use that for what I'm trying to do.

What with all the Ruby cats dying to jump ship (guilty!) maybe we should think about doing...

aCollection slice: 2..4 "new binary message?"

slice: anInterval
"don't have Squeak handy to assess what an implementation might look like"

Whacky idea, please forgive if it's over the top, beloved People of Squeak. I know you will, you've done it before:)

On Aug 6, 2010, at 12:08 AM, Travis Griggs <[hidden email]> wrote:

> On Aug 5, 2010, at 11:03 PM, 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.
>
>
> #(foo bar baz 1 2 3) copyFrom: 2 to: 4
>
> ? is that what what you mean?
>
> another way, more general would be
>
> (2 to: 4) collect: [:n | original at: n]
>
> In VisualWorks 7.7.1, we added a Subsequence object, that can act as a subrange facade to a sequence. It's very very handy for the Meyer diff algorithm, and doing Text differencing.
>
> Subsequence sequence: #(foo bar baz 1 2 3) from: 2 to: 4
>
> You can stack them of course.
>
> --
> Travis Griggs
> Objologist
> For every adage, there is an equal and contrary un-adage
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Noob question: array slice?

Bert Freudenberg
In reply to this post by Casey Ransberger-2
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 -