Skip to main content
added 205 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
f0[{a, b, c, d}, {1, 1}](*append first and prepend last*)

{d, a, b, c, d, a}

f0[{a, b, c, d}, {1, -1}](* rotate right *)
f0[{a, b, c, d}, {0, 08}] (* repeat *)

{a, b, c, d, a, b, c, d, a, b, c, d}

f0[{a, -1b, ;;c, 1d}, ;;{9, -1]1}] (* reverserotate right and repeat *)

{d, a, b, c, d, a, b, c, d, a, b, c}

f0[{a, b, c, d}, {0-1, 89}] (* rotate left and repeat *)

{a, bb, c, d, a, b, c, d, a, b, c, d, a}

f0[{a, b, c, d}, {0, 0}, -1 ;; 1 ;; -1] (* reverse *)

{d, c, b, a}

f0[{a, b, c, d}, {1, -1}](* rotate right *)
f0[{a, b, c, d}, {0, 0}, -1 ;; 1 ;; -1] (* reverse *)

{d, c, b, a}

f0[{a, b, c, d}, {0, 8}] (* repeat *)

{a, b, c, d, a, b, c, d, a, b, c, d}

f0[{a, b, c, d}, {1, 1}](*append first and prepend last*)

{d, a, b, c, d, a}

f0[{a, b, c, d}, {1, -1}](* rotate right *)
f0[{a, b, c, d}, {0, 8}] (* repeat *)

{a, b, c, d, a, b, c, d, a, b, c, d}

f0[{a, b, c, d}, {9, -1}] (* rotate right and repeat *)

{d, a, b, c, d, a, b, c, d, a, b, c}

f0[{a, b, c, d}, {-1, 9}] (* rotate left and repeat *)

{b, c, d, a, b, c, d, a, b, c, d, a}

f0[{a, b, c, d}, {0, 0}, -1 ;; 1 ;; -1] (* reverse *)

{d, c, b, a}

added 831 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Update: We can combine ArrayPad and Part:

ClearAll[f0]
f0[a_, b_, c_: All] := ArrayPad[a, b, a][[c]]

Examples:

f0[{a, b, c, d}, {0, 1}](* append first *)

{a, b, c, d, a}

f0[{a, b, c, d}, {1, 0}] (* prepend last *)

{d, a, b, c, d}

f0[{a, b, c, d}, {1, -1}](* rotate right *)

{d, a, b, c}

f0[{a, b, c, d}, {-1, 1}] (* rotate left *)

{b, c, d, a}

f0[{a, b, c, d}, {0, 0}, -1 ;; 1 ;; -1] (* reverse *)

{d, c, b, a}

f0[{a, b, c, d}, {0, 8}] (* repeat *)

{a, b, c, d, a, b, c, d, a, b, c, d}

f0[{a, b, c, d}, {0, 8}, ;; ;; 2] (*repeat and take odd parts*)

{a, c, a, c, a, c}

f0[{a, b, c, d}, {0, 8}, {1, 3, 4, 7, 9}] (*repeat and take parts 1,3,4,7 and 9*)

{a, c, d, c, a}

Update: We can combine ArrayPad and Part:

ClearAll[f0]
f0[a_, b_, c_: All] := ArrayPad[a, b, a][[c]]

Examples:

f0[{a, b, c, d}, {0, 1}](* append first *)

{a, b, c, d, a}

f0[{a, b, c, d}, {1, 0}] (* prepend last *)

{d, a, b, c, d}

f0[{a, b, c, d}, {1, -1}](* rotate right *)

{d, a, b, c}

f0[{a, b, c, d}, {-1, 1}] (* rotate left *)

{b, c, d, a}

f0[{a, b, c, d}, {0, 0}, -1 ;; 1 ;; -1] (* reverse *)

{d, c, b, a}

f0[{a, b, c, d}, {0, 8}] (* repeat *)

{a, b, c, d, a, b, c, d, a, b, c, d}

f0[{a, b, c, d}, {0, 8}, ;; ;; 2] (*repeat and take odd parts*)

{a, c, a, c, a, c}

f0[{a, b, c, d}, {0, 8}, {1, 3, 4, 7, 9}] (*repeat and take parts 1,3,4,7 and 9*)

{a, c, d, c, a}

added 94 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

Perhaps ArrayPad with "Periodic" as the third argument?:

ClearAll[f1]
f1  = ArrayPad[##, "Periodic"]#] &[&;

Examples:

f1[{a, b, c, d}, {0, 1}]

{a, b, c, d, a}

ArrayPad[##, "Periodic"] &[f1[{a, b, c, d}, {1, 0}]

{d, a, b, c, d}

ArrayPad[##, "Periodic"] &[f1[{a, b, c, d}, {12, 0}]

{a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d}

Alternatively, you can use "Periodic" as the third argument in ArrayPad:

ClearAll[f2]
f2 = ArrayPad[##, "Periodic"] &

Perhaps ArrayPad with "Periodic" as the third argument?

ArrayPad[##, "Periodic"] &[{a, b, c, d}, {0, 1}]

{a, b, c, d, a}

ArrayPad[##, "Periodic"] &[{a, b, c, d}, {1, 0}]

{d, a, b, c, d}

ArrayPad[##, "Periodic"] &[{a, b, c, d}, {12, 0}]

{a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d}

Perhaps ArrayPad:

ClearAll[f1]
f1  = ArrayPad[##, #] &;

Examples:

f1[{a, b, c, d}, {0, 1}]

{a, b, c, d, a}

f1[{a, b, c, d}, {1, 0}]

{d, a, b, c, d}

f1[{a, b, c, d}, {12, 0}]

{a, b, c, d, a, b, c, d, a, b, c, d, a, b, c, d}

Alternatively, you can use "Periodic" as the third argument in ArrayPad:

ClearAll[f2]
f2 = ArrayPad[##, "Periodic"] &
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading