# Cyclic Take function? [duplicate]

Is there some direct and simple "cyclic Take", sometimes also known as "overtake" in Mathematica. That is, if the take specification runs out of elements, it just cycles back to the beginning (as many times as necessary).

For example, if the functions were named OverTake, then:

OverTake[{1, 2}, 5]
(* {1, 2, 1, 2, 1} *)


(This would be a direct analog of the take functions in the array-processing languages APL and J.)

I can clumsily code a function to do that, but I was hoping for a simple use of something built in.

• Maybe PadRight[{}, 5, {1, 2}]? Nov 11, 2015 at 20:51
• @Szabolcs That's brilliant! It also naturally handles the "normal" Take usage when requesting fewer elements than the length of the list. Please post it as an answer! Nov 11, 2015 at 21:06
• I'm waiting for @Szabolcs to post his comment as answer, since that's the one I'd like to mark (as the simplest). Nov 11, 2015 at 22:10
• related/duplicate q/a: Generate cyclic list from a list?
– kglr
Nov 11, 2015 at 22:24
• @kglr I'd like to join murray in asking you to consider undeleting your answer. Although you do reference using "Periodic" with PadRight in the other answer, I don't see why it shouldn't be preserved here as well. Nov 11, 2015 at 23:04

A variation on Szabolcs's suggestion:

overTake = PadRight[##, "Periodic"] &;
overTake[{1, 2}, 5]


{1, 2, 1, 2, 1}

• Where did you lean about the "Periodic" argument to PadRight? I'm not seeing it at doc page ref/PadRight. Nov 11, 2015 at 21:52
• See Details for ArrayPad
– eldo
Nov 11, 2015 at 22:10
• @murray, afaik, the fact that PadRight and PadLeft work with a third argument just like ArrayPad is undocumented. I found out by pure luck -- wishing PadRight worked like ArrayPad, tried it and saw it worked:)
– kglr
Nov 11, 2015 at 22:28
Catenate @ Partition[{1, 2}, 5, 5, 1]


{1, 2, 1, 2, 1}

OverTake[list_, n_] /; Length @ list >= n := Take[list, n]

OverTake[list_, n_] /; Length @ list < n := Catenate @ Partition[list, n, n, 1]

OverTake[Range @ 5, 6]


{1, 2, 3, 4, 5, 1}

OverTake[Range @ 5, 4]


{1, 2, 3, 4}

Update

Faster than solutions with PadRight and Partition

l = 2;
n = 5;
list = Range @ l;

Flatten@ConstantArray[list, IntegerPart[n/l]]~Join~list[[;; Mod[n, l]]]


{1, 2, 1, 2, 1}

Also works for n < l

• See update -----
– eldo
Nov 11, 2015 at 21:06
• Excellent! Thanks for addressing that point! (+1) Nov 11, 2015 at 21:06

For historical reasons, let me mention the CycleValues function that originates before v6, and can be found in several standard packages. I copy the one from Packages\BarCharts\Charts.m with its description by the dev who included. (If you check however StatisticalPlots.m, it already uses the PadRight version.) Note, that it was meant to handle non-list arguments as well.

(* The following is a useful internal utility function to be
used when you have a list of values that need to be cycled to
some length (as PlotStyle works in assigning styles to lines
in a plot).  The list is the list of values to be cycled, the
integer is the number of elements you want in the final list. *)

CycleValues[{},_] := {}
CycleValues[list_List, n_Integer] := Module[{hold = list},
While[Length[hold] < n,hold = Join[hold,hold]];
Take[hold,n] /. None -> {}
]
CycleValues[item_,n_] := CycleValues[{item}, n];

CycleValues[0, 5]        (* ==> {0, 0, 0, 0, 0} *)
CycleValues[{1, 2}, 5]   (* ==> {1, 2, 1, 2, 1} *)

• I wonder where that None could come from...? Nov 25, 2015 at 11:58
• I dont' know but better skip that ;) CycleValues[None, 5]
– Kuba
Nov 25, 2015 at 12:05