Zulip Chat Archive

Stream: Is there code for X?

Topic: function to take every nth element from a List?


Scott Morrison (Mar 22 2023 at 20:29):

Presumably we have somewhere a function to take every nth element from a List. In particular I would like n=2. :-)

Matthew Ballard (Mar 22 2023 at 20:46):

Is there no filterIdx?

Scott Morrison (Mar 22 2023 at 20:47):

Doesn't appear to be.

Eric Wieser (Mar 22 2023 at 21:18):

You can filter list.enum

Kyle Miller (Mar 22 2023 at 21:27):

Is there a function that splits a list into groups of k?

The n=2 case can sometimes be useful too.

def List.pairs : List α  List (α × α)
  | x :: y :: xs => (x, y) :: xs.pairs
  | [] => []
  | _ => panic! "List.pairs given list of non-even length"

Last updated: Dec 20 2023 at 11:08 UTC