cutarr

function cutarr( arr, n ) --> left, right

Description

The function splits an array table at position n in two pieces. The element at n will be part of the left sub array table.

Parameters

arr

The array table that should be split.

n

The position in the array table where the cut happens.

Return Values

left

The left part of the array table.

right

The right part of the array table.

Code

--ZFUNC-cutarr-v1
local function cutarr( arr, n ) --> left, right
   local left = {}
   for i = 1, n do table.insert( left, arr[ i ] ) end
   local right = {}
   for i = n+1, #arr do table.insert( right, arr[ i ] ) end
   return left, right
end

return cutarr

Examples

local t = require( "tapered" )
local cutarr = require( "cutarr" )

left, right = cutarr( { "a", "b", "c" }, 2 )
t.same( { "a", "b" }, left )
t.same( { "c" }, right )

t.done()