How can I get every other item of a list (Or every 3rd, 4th, etc.)?

Submitted by javelin on Sun, 2012-02-12 22:17

How can I get every other item of a list (Or every 3rd, 4th, etc.)?

Up to every 10th item can be gotten very easily, with the help of step().

For example:

&every_other foo=%1

think step(foo/every_other, a b c d e f, 2) 

=> b d f

If you want every other element starting with the first, use
%0 instead of %1.

Alternatively, you can use elements() and build the list of
positions using lnum() and vmul().

For example:

think elements(a b c d e f, vmul(lnum(0, 6), 2))

2001-Nov-13 6:44pm shawnw