Krug.ArrayUtil (Krug v1.1.44) View Source

Utilitary module to handle array transformations.

Link to this section Summary

Functions

Rotates an array X positions in direction from right to left. A initial array [1,2,3,4] turn in [4,1,2,3] if rotate positions equal to 1.

Rotates an array X positions in direction from left to right. A initial array [1,2,3,4] turn in [2,3,4,1] if rotate positions equal to 1.

Link to this section Functions

Link to this function

rotate_left(array, positions, unsafe \\ false)

View Source (since 0.4.3)

Rotates an array X positions in direction from right to left. A initial array [1,2,3,4] turn in [4,1,2,3] if rotate positions equal to 1.

The parameter unsafe can be set to true when you have total sure that all parameters are not null/empty. This will improve some performance.

Examples

iex > Krug.ArrayUtil.rotate_left(nil,1)
nil
iex > Krug.ArrayUtil.rotate_left([],1)
[]
iex > Krug.ArrayUtil.rotate_left([1],2)
[1]
iex > Krug.ArrayUtil.rotate_left([1,2,3,4],1)
[4,1,2,3]
iex > Krug.ArrayUtil.rotate_left([1,2,3,4],3)
[2,3,4,1]
Link to this function

rotate_right(array, positions, unsafe \\ false)

View Source (since 0.4.3)

Rotates an array X positions in direction from left to right. A initial array [1,2,3,4] turn in [2,3,4,1] if rotate positions equal to 1.

The parameter unsafe can be set to true when you have total sure that all parameters are not null/empty. This will improve some performance.

Examples

iex > Krug.ArrayUtil.rotate_right(nil,1)
nil
iex > Krug.ArrayUtil.rotate_right([],1)
[]
iex > Krug.ArrayUtil.rotate_right([1],2)
[1]
iex > Krug.ArrayUtil.rotate_right([1,2,3,4],nil)
[1,2,3,4]
iex > Krug.ArrayUtil.rotate_right([1,2,3,4],1)
[2,3,4,1]
iex > Krug.ArrayUtil.rotate_right([1,2,3,4],3)
[4,1,2,3]