Is there a Squeak idiom for array slices?

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

Is there a Squeak idiom for array slices?

Tim Rowe-2
If, for example, variable a contains an array with 20 elements, but I
want to inject:into: just elements 3 through 17, is there an easier
way than step-by-step creating a new array, copying the required
elements of a into it, and then doing an inject:into: on that?

TIA,

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

Re: Is there a Squeak idiom for array slices?

Bert Freudenberg
On 18.12.2009, at 02:47, Tim Rowe wrote:
>
> If, for example, variable a contains an array with 20 elements, but I
> want to inject:into: just elements 3 through 17, is there an easier
> way than step-by-step creating a new array, copying the required
> elements of a into it, and then doing an inject:into: on that?

array atAll: (3 to: 17)

- Bert -


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

Re: Is there a Squeak idiom for array slices?

Bert Freudenberg
On 18.12.2009, at 02:51, Bert Freudenberg wrote:
>
> On 18.12.2009, at 02:47, Tim Rowe wrote:
>>
>> If, for example, variable a contains an array with 20 elements, but I
>> want to inject:into: just elements 3 through 17, is there an easier
>> way than step-by-step creating a new array, copying the required
>> elements of a into it, and then doing an inject:into: on that?
>
> array atAll: (3 to: 17)

Though that's not really an idiom, I don't think I've ever seen that used in code. #atAll: can provide an arbitrary mapping.

More efficient and more often used are these

        array copyFrom: 3 to: 17
        array first: 4
        array last: 5
        array allButFirst: 6
        array allButLast: 7

- Bert -


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

Re: Is there a Squeak idiom for array slices?

Tim Rowe-2
2009/12/18 Bert Freudenberg <[hidden email]>:

> On 18.12.2009, at 02:51, Bert Freudenberg wrote:
>>
>> On 18.12.2009, at 02:47, Tim Rowe wrote:
>>>
>>> If, for example, variable a contains an array with 20 elements, but I
>>> want to inject:into: just elements 3 through 17, is there an easier
>>> way than step-by-step creating a new array, copying the required
>>> elements of a into it, and then doing an inject:into: on that?
>>
>> array atAll: (3 to: 17)
>
> Though that's not really an idiom, I don't think I've ever seen that used in code. #atAll: can provide an arbitrary mapping.
>
> More efficient and more often used are these
>
>        array copyFrom: 3 to: 17
>        array first: 4
>        array last: 5
>        array allButFirst: 6
>        array allButLast: 7

Many thanks -- I'll certainly be able to o what I want to with
something from that list.

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