JanetDocsPlaygroundI'm feeling luckyGitHub sign in

slice



    cfunction
    src/core/corelib.c on line 413, column 1

    (slice x &opt start end)

    Extract a sub-range of an indexed data structure or byte sequence.


2 examplesSign in to add an example
Loading...
# slice with strings
(slice "Hello" 1)        # => "ello"
(slice "Playing" 0 -4)   # => "Play"
(slice "Playing" -4)     # => "ing"

# slice with keyword
(slice :hello 1)         # => "ello"
leobmPlayground
(slice [:a :b :c :d])  # => (:a :b :c :d)

(slice [:a :b :c :d] 0)  # => (:a :b :c :d)
(slice [:a :b :c :d] 1)  # => (:b :c :d)
(slice [:a :b :c :d] 3)  # => (:d)
(slice [:a :b :c :d] 4)  # => ()
(slice [:a :b :c :d] 5)  # error: index out of range

(slice [:a :b :c :d] -1)  # => ()
(slice [:a :b :c :d] -2)  # => (:d)
(slice [:a :b :c :d] -4)  # => (:b :c :d)
(slice [:a :b :c :d] -5)  # => (:a :b :c :d)
(slice [:a :b :c :d] -6)  # error: index out of range

(slice [:a :b :c :d] 0 0)  # => ()
(slice [:a :b :c :d] 0 1)  # => (:a)
(slice [:a :b :c :d] 0 4)  # => (:a :b :c :d)

(slice [:a :b :c :d] -1 -1)  # => ()
(slice [:a :b :c :d] -2 -1)  # => (:d)
(slice [:a :b :c :d] -5 -1)  # => (:a :b :c :d)

(slice [:a :b :c :d] 1 0)  # => ()
(slice [:a :b :c :d] 4 0)  # => ()
(slice [:a :b :c :d] -1 -2)  # => ()
(slice [:a :b :c :d] -1 -5)  # => ()
cellularmitosisPlayground