JanetDocsPlaygroundI'm feeling luckyGitHub sign in

array/slice



    cfunction
    src/core/array.c on line 215, column 1

    (array/slice arrtup &opt start end)

    Takes a slice of array or tuple from `start` to `end`. The range is 
    half open, [start, end). Indexes can also be negative, indicating 
    indexing from the end of the array. By default, `start` is 0 and 
    `end` is the length of the array. Note that index -1 is synonymous 
    with index `(length arrtup)` to allow a full negative slice range. 
    Returns a new array.


3 examplesSign in to add an example
Loading...
(def a @[1 2 3])
(def b (array/slice a))

#=> good way to clone array, a and b are different arrays
pepePlayground
(array/slice [1 2 3])

#=> @[1 2 3]
# good way to convert tuple to array
pepePlayground
(array/slice @[1 2 3] 0 0)  # => @[]
(array/slice @[1 2 3] 0 1)  # => @[1]
(array/slice @[1 2 3] 0 2)  # => @[1 2]
(array/slice @[1 2 3] 0 3)  # => @[1 2 3]
(array/slice @[1 2 3] 0 4)  # error: index out of range

(array/slice @[1 2 3] 1 1)  # => @[]
(array/slice @[1 2 3] 1 2)  # => @[2]

(array/slice @[1 2 3] 0 -1)  # => @[1 2 3]
(array/slice @[1 2 3] 0 -2)  # => @[1 2]
(array/slice @[1 2 3] 0 -3)  # => @[1]
(array/slice @[1 2 3] 0 -4)  # => @[]
(array/slice @[1 2 3] 0 -5)  # error: index out of range
cellularmitosisPlayground