chickadee » slice » slice

slice object #!optional from toprocedure

Slice the given object returning the slice from from to to.

If to is ommited and from is positive, return the slice from from to the last element of the given object.

If to is ommited and from is negative, return the slice from from, assuming from is the counted from the end of object, to the last element of the given object.

Examples:

(define v '#(1 2 3 4 5 6 7))

(slice v 0 0)     => #()
(slice v 1 0))    => #()
(slice v 0 1))    => #(1)
(slice v 1 3))    => #(2 3)
(slice v 10 10))  => #()
(slice v 0 10))   => #(1 2 3 4 5 6 7)
(slice v 10 0))   => #()
(slice v 0))      => #(1 2 3 4 5 6 7)
(slice v -1))     => #(7)
(slice v 10))     => #()
(slice v -10))    => #(1 2 3 4 5 6 7)
(slice v -4))     => #(4 5 6 7)
(slice v -4 -4))  => #()
(slice v -4 -2))  => #(4 5)
(slice v -4 -10)) => #()
(slice v -10 -4)) => #(1 2 3)

If from and to are ommited, the object argument is expected to be a procedure to be added to the set of slicers known by slice. The given procedure is an one-argument one which should check for the type of the object it is given and return a slicer procedure, which should accept an object, the from and to indexes.

Example:

(define s (make-custom-string "custom string"))

(slice (lambda (obj)
         (and (custom-string? obj)
              (lambda (obj from to)
                (handle-exceptions
                 exn
                 ""
                 (substring (custom-string-text obj) from to))))))

(slice s 0 1) => "c"