Skip to content

Instantly share code, notes, and snippets.

@neizod
Forked from visibletrap/reverse-interleave.clj
Created September 17, 2012 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neizod/3735676 to your computer and use it in GitHub Desktop.
Save neizod/3735676 to your computer and use it in GitHub Desktop.
# group_indices([1,2,3,4,5,6], 2) -> [[1, 3, 5], [2, 4, 6]]
group_indices = lambda l, n: [[e for j, e in enumerate(l) if j % n == i] for i in range(n)]
group_indices = lambda l, n: zip(*zip(*[iter(l)]*n))
-- groupIndices 5 [1..20] -> [[1,6,11,16],[2,7,12,17],[3,8,13,18],[4,9,14,19],[5,10,15,20]]
groupIndices n = map reverse . foldl (\a x -> [if length h /= length t then x:h else h | (h,t) <- zip a ((42:last a):init a)]) (take n $ repeat [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment