Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

smalltalk: how to select the first n items from a collection

I have an collection c, I basically want to split the collection into two parts: the first n items into one list and the rest to another. Obviously, I can use do:, but is there a better way to do it?

like image 996
PeacefulPanda Avatar asked Feb 14 '23 07:02

PeacefulPanda


2 Answers

I don't know about the other dialects but pharo has this useful methods: first: last: allButFirst: allButLast:

So you can do something like:

firstPart := c first: 10.
secondPart := c allButFirst: 10
like image 121
Uko Avatar answered Feb 15 '23 20:02

Uko


You can use copyFrom: to:.

For example:

firstTen := myCollection copyFrom: 1 to: 10.
like image 28
Lorenzo Baracchi Avatar answered Feb 15 '23 21:02

Lorenzo Baracchi